summaryrefslogtreecommitdiff
path: root/internal/funcs/coll.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2024-01-25 20:11:31 -0500
committerGitHub <noreply@github.com>2024-01-25 20:11:31 -0500
commitebb97fb7367fb983cffc1935a8fb57e4b80f5249 (patch)
tree43ef6cd01f629f60f59efe1e5b003f7c8e3a1257 /internal/funcs/coll.go
parentf1d9158ea99abbe556251c1ff2fe970f3b460ee9 (diff)
Move funcs package to internal (#1977)
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'internal/funcs/coll.go')
-rw-r--r--internal/funcs/coll.go218
1 files changed, 218 insertions, 0 deletions
diff --git a/internal/funcs/coll.go b/internal/funcs/coll.go
new file mode 100644
index 00000000..c8cb4dcc
--- /dev/null
+++ b/internal/funcs/coll.go
@@ -0,0 +1,218 @@
+package funcs
+
+import (
+ "context"
+ "fmt"
+ "reflect"
+
+ "github.com/hairyhenderson/gomplate/v4/conv"
+ "github.com/hairyhenderson/gomplate/v4/internal/deprecated"
+ "github.com/hairyhenderson/gomplate/v4/internal/texttemplate"
+
+ "github.com/hairyhenderson/gomplate/v4/coll"
+)
+
+// CollNS -
+//
+// Deprecated: don't use
+func CollNS() *CollFuncs {
+ return &CollFuncs{}
+}
+
+// AddCollFuncs -
+//
+// Deprecated: use CreateCollFuncs instead
+func AddCollFuncs(f map[string]interface{}) {
+ for k, v := range CreateCollFuncs(context.Background()) {
+ f[k] = v
+ }
+}
+
+// CreateCollFuncs -
+func CreateCollFuncs(ctx context.Context) map[string]interface{} {
+ f := map[string]interface{}{}
+
+ ns := &CollFuncs{ctx}
+ f["coll"] = func() interface{} { return ns }
+
+ f["has"] = ns.Has
+ f["slice"] = ns.deprecatedSlice
+ f["dict"] = ns.Dict
+ f["keys"] = ns.Keys
+ f["values"] = ns.Values
+ f["append"] = ns.Append
+ f["prepend"] = ns.Prepend
+ f["uniq"] = ns.Uniq
+ f["reverse"] = ns.Reverse
+ f["merge"] = ns.Merge
+ f["sort"] = ns.Sort
+ f["jsonpath"] = ns.JSONPath
+ f["jq"] = ns.JQ
+ f["flatten"] = ns.Flatten
+ return f
+}
+
+// CollFuncs -
+type CollFuncs struct {
+ ctx context.Context
+}
+
+// Slice -
+func (CollFuncs) Slice(args ...interface{}) []interface{} {
+ return coll.Slice(args...)
+}
+
+// deprecatedSlice -
+// Deprecated: use coll.Slice instead
+func (f *CollFuncs) deprecatedSlice(args ...interface{}) []interface{} {
+ deprecated.WarnDeprecated(f.ctx, "the 'slice' alias for coll.Slice is deprecated - use coll.Slice instead")
+ return coll.Slice(args...)
+}
+
+// GoSlice - same as text/template's 'slice' function
+func (CollFuncs) GoSlice(item reflect.Value, indexes ...reflect.Value) (reflect.Value, error) {
+ return texttemplate.GoSlice(item, indexes...)
+}
+
+// Has -
+func (CollFuncs) Has(in interface{}, key string) bool {
+ return coll.Has(in, key)
+}
+
+// Index returns the result of indexing the last argument with the preceding
+// index keys. This is similar to the `index` built-in template function, but
+// the arguments are ordered differently for pipeline compatibility. Also, this
+// function is more strict, and will return an error when the value doesn't
+// contain the given key.
+func (CollFuncs) Index(args ...interface{}) (interface{}, error) {
+ if len(args) < 2 {
+ return nil, fmt.Errorf("wrong number of args: wanted at least 2, got %d", len(args))
+ }
+
+ item := args[len(args)-1]
+ indexes := args[:len(args)-1]
+
+ return coll.Index(item, indexes...)
+}
+
+// Dict -
+func (CollFuncs) Dict(in ...interface{}) (map[string]interface{}, error) {
+ return coll.Dict(in...)
+}
+
+// Keys -
+func (CollFuncs) Keys(in ...map[string]interface{}) ([]string, error) {
+ return coll.Keys(in...)
+}
+
+// Values -
+func (CollFuncs) Values(in ...map[string]interface{}) ([]interface{}, error) {
+ return coll.Values(in...)
+}
+
+// Append -
+func (CollFuncs) Append(v interface{}, list interface{}) ([]interface{}, error) {
+ return coll.Append(v, list)
+}
+
+// Prepend -
+func (CollFuncs) Prepend(v interface{}, list interface{}) ([]interface{}, error) {
+ return coll.Prepend(v, list)
+}
+
+// Uniq -
+func (CollFuncs) Uniq(in interface{}) ([]interface{}, error) {
+ return coll.Uniq(in)
+}
+
+// Reverse -
+func (CollFuncs) Reverse(in interface{}) ([]interface{}, error) {
+ return coll.Reverse(in)
+}
+
+// Merge -
+func (CollFuncs) Merge(dst map[string]interface{}, src ...map[string]interface{}) (map[string]interface{}, error) {
+ return coll.Merge(dst, src...)
+}
+
+// Sort -
+func (CollFuncs) Sort(args ...interface{}) ([]interface{}, error) {
+ var (
+ key string
+ list interface{}
+ )
+ if len(args) == 0 || len(args) > 2 {
+ return nil, fmt.Errorf("wrong number of args: wanted 1 or 2, got %d", len(args))
+ }
+ if len(args) == 1 {
+ list = args[0]
+ }
+ if len(args) == 2 {
+ key = conv.ToString(args[0])
+ list = args[1]
+ }
+ return coll.Sort(key, list)
+}
+
+// JSONPath -
+func (CollFuncs) JSONPath(p string, in interface{}) (interface{}, error) {
+ return coll.JSONPath(p, in)
+}
+
+// JQ -
+func (f *CollFuncs) JQ(jqExpr string, in interface{}) (interface{}, error) {
+ return coll.JQ(f.ctx, jqExpr, in)
+}
+
+// Flatten -
+func (CollFuncs) Flatten(args ...interface{}) ([]interface{}, error) {
+ if len(args) == 0 || len(args) > 2 {
+ return nil, fmt.Errorf("wrong number of args: wanted 1 or 2, got %d", len(args))
+ }
+ list := args[0]
+ depth := -1
+ if len(args) == 2 {
+ depth = conv.ToInt(args[0])
+ list = args[1]
+ }
+ return coll.Flatten(list, depth)
+}
+
+func pickOmitArgs(args ...interface{}) (map[string]interface{}, []string, error) {
+ if len(args) <= 1 {
+ return nil, nil, fmt.Errorf("wrong number of args: wanted 2 or more, got %d", len(args))
+ }
+
+ m, ok := args[len(args)-1].(map[string]interface{})
+ if !ok {
+ return nil, nil, fmt.Errorf("wrong map type: must be map[string]interface{}, got %T", args[len(args)-1])
+ }
+
+ keys := make([]string, len(args)-1)
+ for i, v := range args[0 : len(args)-1] {
+ k, ok := v.(string)
+ if !ok {
+ return nil, nil, fmt.Errorf("wrong key type: must be string, got %T (%+v)", args[i], args[i])
+ }
+ keys[i] = k
+ }
+ return m, keys, nil
+}
+
+// Pick -
+func (CollFuncs) Pick(args ...interface{}) (map[string]interface{}, error) {
+ m, keys, err := pickOmitArgs(args...)
+ if err != nil {
+ return nil, err
+ }
+ return coll.Pick(m, keys...), nil
+}
+
+// Omit -
+func (CollFuncs) Omit(args ...interface{}) (map[string]interface{}, error) {
+ m, keys, err := pickOmitArgs(args...)
+ if err != nil {
+ return nil, err
+ }
+ return coll.Omit(m, keys...), nil
+}