helper

package
v0.10.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package helper provides common methods used by the various template functions

Index

Constants

View Source
const TestTemplateName = "test template"

TestTemplateName is the template name used for testing

Variables

View Source
var Now = time.Now

Now is defined as a variable so that it can be overridden as required (e.g. unit testing)

Functions

func Apply added in v0.10.3

func Apply[T, U any](a []T, fn func(T) (U, error)) ([]U, error)

Apply a function to each element of a slice, returning a translated slice

func AsStringList

func AsStringList(list any) ([]string, error)

AsStringList converts a list to a list of strings

func Combine added in v0.10.3

func Combine[M ~map[K]V, K comparable, V any](maps ...M) M

Combine a slice of maps into a single map, with key collisions, later overwrites earlier

func ItemForList

func ItemForList(item any) reflect.Value

ItemForList returns the item as a reflect.Value that can be inserted into the list. This is because nil requires special casing

func ListInfo

func ListInfo(list any) (slice reflect.Value, length int, err error)

ListInfo returns basic info that we need for most list processing, ie it's reflect.Value (converted to the generic []any type), length, or error message if it's not a list

func NewTemplate

func NewTemplate(t *testing.T, tpl string, fns template.FuncMap) *template.Template

NewTemplate creates a new template

func PtrTo added in v0.10.3

func PtrTo[T any](t T) *T

PtrTo returns a pointer to a copy of the given parameter

func Reduce added in v0.10.3

func Reduce[T, U any](a []T, initial U, fn Reducer[T, U]) U

Reduce an array to a single value through successive calls to a function that takes the previous value and the next element of the array

func TemplateTest

func TemplateTest(test TestSet, fns template.FuncMap) func(t *testing.T)

TemplateTest generates a test function for a given TestSet. fns is a set of functions needed by the template to be rendered

func ToFloat added in v0.10.3

func ToFloat(a any) (float64, error)

ToFloat converts the supplied argument to a float, if it can be

Types

type Reducer added in v0.10.3

type Reducer[T, U any] func(a U, b T) U

Reducer is the signature for a reduction method used by Reduce

type TestArgs

type TestArgs map[string]any

TestArgs are the arguments used when testing a template

type TestSet

type TestSet struct {
	Name     string
	Template string
	Args     TestArgs
	Want     string
	WantErr  bool
}

TestSet is used for specifying a template function test instance

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL