From 28a70704ee20085de04b613087f4e9c6ffac4e5c Mon Sep 17 00:00:00 2001 From: GUO YANKE Date: Fri, 9 Aug 2024 11:48:48 +0800 Subject: [PATCH] feat(mtmpl): add slice and dict --- internal/mtmpl/funcs.go | 47 +++++++++++++++++++++++++++--------- internal/mtmpl/funcs_test.go | 16 ++++++++++++ 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/internal/mtmpl/funcs.go b/internal/mtmpl/funcs.go index a80d369..6563223 100644 --- a/internal/mtmpl/funcs.go +++ b/internal/mtmpl/funcs.go @@ -10,6 +10,8 @@ import ( "strings" ) +//go:generate python3 funcs.gen.py + // Funcs provided funcs for render var Funcs = map[string]any{ "netResolveIPAddr": net.ResolveIPAddr, @@ -63,18 +65,20 @@ var Funcs = map[string]any{ "strconvAoti": strconv.Atoi, "strconvItoa": strconv.Itoa, - "add": add, - "neg": neg, + "add": funcAdd, + "neg": funcNeg, + "dict": funcDict, + "slice": funcSlice, // deprecated - "intAdd": add, - "intNeg": neg, - "int64Add": add, - "int64Neg": neg, - "float32Add": add, - "float32Neg": neg, - "float64Add": add, - "float64Neg": neg, + "intAdd": funcAdd, + "intNeg": funcNeg, + "int64Add": funcAdd, + "int64Neg": funcNeg, + "float32Add": funcAdd, + "float32Neg": funcNeg, + "float64Add": funcAdd, + "float64Neg": funcNeg, "k8sStatefulSetID": osHostnameSequenceID, } @@ -87,7 +91,7 @@ func netResolveIP(s string) (ip string, err error) { return } -func add(a, b any) (any, error) { +func funcAdd(a, b any) (any, error) { switch a.(type) { case bool: return a.(bool) || b.(bool), nil @@ -129,7 +133,7 @@ func add(a, b any) (any, error) { return nil, errors.New("add: type not supported: " + reflect.TypeOf(a).String()) } -func neg(a any) (any, error) { +func funcNeg(a any) (any, error) { switch a := a.(type) { case bool: return !a, nil @@ -167,6 +171,25 @@ func neg(a any) (any, error) { return nil, errors.New("neg: type not supported: " + reflect.TypeOf(a).String()) } +func funcDict(items ...any) (map[string]any, error) { + if len(items)%2 != 0 { + return nil, errors.New("dict: odd number of items") + } + m := map[string]any{} + for i := 0; i < len(items); i += 2 { + k, ok := items[i].(string) + if !ok { + return nil, errors.New("dict: key is not a string") + } + m[k] = items[i+1] + } + return m, nil +} + +func funcSlice(args ...any) []any { + return args +} + func osHostnameSequenceID() (id int, err error) { var hostname string if hostname = os.Getenv("HOSTNAME"); hostname == "" { diff --git a/internal/mtmpl/funcs_test.go b/internal/mtmpl/funcs_test.go index f9ba310..a8edcd5 100644 --- a/internal/mtmpl/funcs_test.go +++ b/internal/mtmpl/funcs_test.go @@ -31,3 +31,19 @@ func TestFuncNeg(t *testing.T) { doTestTemplate(t, `-1`, `{{neg 1}}`, nil) doTestTemplate(t, `false`, `{{neg true}}`, nil) } + +func TestFuncDic(t *testing.T) { + doTestTemplate(t, `bar`, ` + {{define "hello"}} + {{.foo}} + {{end}} + {{template "hello" (dict "foo" "bar")}} + `, nil) +} + +func TestFuncSlice(t *testing.T) { + doTestTemplate(t, `1 2 3`, `{{range $i, $v := slice 1 2 3}}{{$v}} {{end}}`, nil) + doTestTemplate(t, `3`, ` + {{index (slice 1 2 3) (add 1 1)}} + `, nil) +}