Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"fmt"
"os"
"reflect"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -38,6 +39,76 @@ func init() {
funcMap = CreateFuncs(context.Background())
}

// ListAllFuncs returns the sorted list of built-in template function names.
func ListAllFuncs() []string {
names := make(map[string]struct{}, len(funcMap))
for name, fn := range funcMap {
if isNamespaceFactory(fn) {
addNamespaceFuncs(names, name, fn)
continue
}
names[name] = struct{}{}
}

out := make([]string, 0, len(names))
for name := range names {
out = append(out, name)
}
sort.Strings(out)
return out
}

func addNamespaceFuncs(names map[string]struct{}, namespace string, fn any) {
val := reflect.ValueOf(fn)
if val.Kind() != reflect.Func {
return
}
typ := val.Type()
if typ.NumIn() != 0 || typ.NumOut() != 1 {
return
}
if typ.Out(0).Kind() != reflect.Interface {
return
}

var res reflect.Value
func() {
defer func() {
if recover() != nil {
res = reflect.Value{}
}
}()
res = val.Call(nil)[0]
}()
if !res.IsValid() {
return
}
if res.Kind() == reflect.Interface {
if res.IsNil() {
return
}
res = res.Elem()
}

resType := res.Type()
for i := 0; i < resType.NumMethod(); i++ {
method := resType.Method(i)
if method.PkgPath != "" {
continue
}
names[namespace+"."+method.Name] = struct{}{}
}
}

func isNamespaceFactory(fn any) bool {
val := reflect.ValueOf(fn)
if val.Kind() != reflect.Func {
return false
}
typ := val.Type()
return typ.NumIn() == 0 && typ.NumOut() == 1 && typ.Out(0).Kind() == reflect.Interface
}

type Template struct {
Template string `yaml:"template,omitempty" json:"template,omitempty"` // Go template
JSONPath string `yaml:"jsonPath,omitempty" json:"jsonPath,omitempty"`
Expand Down
Loading