Skip to content

Commit

Permalink
feat: add support for custom functions
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Oct 30, 2023
1 parent 482422c commit fabf606
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 14 deletions.
47 changes: 40 additions & 7 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
_ "github.com/flanksource/gomplate/v3/js"
"github.com/flanksource/mapstructure"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/pkg/errors"
"github.com/robertkrimen/otto"
"github.com/robertkrimen/otto/registry"
Expand All @@ -25,18 +27,35 @@ func init() {
}

type Template struct {
Template string `yaml:"template,omitempty" json:"template,omitempty"` // Go template
JSONPath string `yaml:"jsonPath,omitempty" json:"jsonPath,omitempty"`
Expression string `yaml:"expr,omitempty" json:"expr,omitempty"` // A cel-go expression
Javascript string `yaml:"javascript,omitempty" json:"javascript,omitempty"`
Template string `yaml:"template,omitempty" json:"template,omitempty"` // Go template
JSONPath string `yaml:"jsonPath,omitempty" json:"jsonPath,omitempty"`
Expression string `yaml:"expr,omitempty" json:"expr,omitempty"` // A cel-go expression
Javascript string `yaml:"javascript,omitempty" json:"javascript,omitempty"`
Functions map[string]func() any `yaml:"-" json:"-"`
}

func (t Template) IsEmpty() bool {
return t.Template == "" && t.JSONPath == "" && t.Expression == "" && t.Javascript == ""
}

func RunExpression(environment map[string]any, template Template) (any, error) {
env, err := cel.NewEnv(GetCelEnv(environment)...)
funcs := GetCelEnv(environment)

for name, fn := range template.Functions {
_name := name
_fn := fn
funcs = append(funcs, cel.Function(_name, cel.Overload(
_name,
nil,
cel.AnyType,
cel.FunctionBinding(func(values ...ref.Val) ref.Val {
out := _fn()
return types.DefaultTypeAdapter.NativeToValue(out)
}),
)))
}

env, err := cel.NewEnv(funcs...)
if err != nil {
return "", err
}
Expand All @@ -57,7 +76,7 @@ func RunExpression(environment map[string]any, template Template) (any, error) {

out, _, err := prg.Eval(data)
if err != nil {
return nil, errors.Wrapf(err, "error evaluating expression %s: %s, %v", template.Expression, err, data)
return nil, errors.Wrapf(err, "error evaluating expression %s: %s, %+v", template.Expression, err, data)
}
return out.Value(), nil

Expand Down Expand Up @@ -88,7 +107,21 @@ func RunTemplate(environment map[string]any, template Template) (string, error)
// gotemplate
if template.Template != "" {
tpl := gotemplate.New("")
tpl, err := tpl.Funcs(funcMap).Parse(template.Template)
funcs := make(map[string]any)
if len(template.Functions) > 0 {
for k, v := range funcMap {
funcs[k] = v
}
for k, v := range template.Functions {
funcs[k] = v
}
} else {
funcs = funcMap
}
for k, v := range template.Functions {
funcs[k] = v
}
tpl, err := tpl.Funcs(funcs).Parse(template.Template)
if err != nil {
return "", err
}
Expand Down
28 changes: 21 additions & 7 deletions tests/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ type Test struct {
out string
}

func TestCel(t *testing.T) {

tests := []Test{}

runTests(t, tests)
}

func runTests(t *testing.T, tests []Test) {
for _, tc := range tests {
t.Run(tc.expression, func(t *testing.T) {
Expand All @@ -48,6 +41,27 @@ func runTests(t *testing.T, tests []Test) {
}
}

func TestFunctions(t *testing.T) {
funcs := map[string]func() any{
"fn": func() any {
return map[string]any{
"a": "b",
"c": 1,
}
},
}

out, err := gomplate.RunTemplate(map[string]interface{}{
"hello": "hi",
}, gomplate.Template{
Expression: "hello + ' ' + fn().a",
Functions: funcs,
})

assert.ErrorIs(t, nil, err)
assert.Equal(t, "hi b", out)
}

func TestRegex(t *testing.T) {
runTests(t, []Test{
{nil, `' asdsa A123 asdsd'.find(r"A\d{3}")`, "A123"},
Expand Down
24 changes: 24 additions & 0 deletions tests/gomplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ import (
"github.com/stretchr/testify/assert"
)

func TestGomplateFunctions(t *testing.T) {
funcs := map[string]func() any{
"fn": func() any {
return map[string]any{
"a": "b",
"c": 1,
}
},
"fn1": func() any {
return "c"
},
}

out, err := gomplate.RunTemplate(map[string]interface{}{
"hello": "hi",
}, gomplate.Template{
Template: "{{.hello}} {{fn1}}",
Functions: funcs,
})

assert.ErrorIs(t, nil, err)
assert.Equal(t, "hi c", out)
}

func TestGomplate(t *testing.T) {
tests := []struct {
env map[string]interface{}
Expand Down

0 comments on commit fabf606

Please sign in to comment.