Repository navigation
Expand file tree
/
Copy pathcel_functions.go
More file actions
122 lines (113 loc) · 4.04 KB
/
Copy pathcel_functions.go
File metadata and controls
122 lines (113 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package gomplate
import (
"fmt"
"sort"
"strings"
"cel.dev/cel-go/cel"
"cel.dev/cel-go/common/types"
"cel.dev/cel-go/common/types/ref"
"github.com/flanksource/gomplate/v4/celvalues"
)
// CelFunction declares a CEL function whose implementation is supplied on each evaluation, so
// the compiled environment and program can be cached while Impl closes over per-call state
// (a request context, a database handle, ...).
type CelFunction struct {
Name string
Overloads []CelOverload
// Impl serves every overload; overloadID names the one that matched the call.
Impl cel.LateBoundFunction
}
// CelOverload is one signature of a CelFunction. Member declares a receiver-style overload,
// whose receiver is Args[0].
type CelOverload struct {
ID string
Member bool
Args []*cel.Type
Result *cel.Type
}
// celFunctions returns the template's CelFunctions followed by its zero-argument Functions,
// which are declared the same way so they no longer stop the program from being cached.
func (t Template) celFunctions() []CelFunction {
functions := make([]CelFunction, 0, len(t.CelFunctions)+len(t.Functions))
functions = append(functions, t.CelFunctions...)
names := make([]string, 0, len(t.Functions))
for name := range t.Functions {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
functions = append(functions, zeroArgCelFunction(name, t.Functions[name]))
}
return functions
}
func zeroArgCelFunction(name string, function any) CelFunction {
return CelFunction{
Name: name,
Overloads: []CelOverload{{ID: name, Result: cel.AnyType}},
Impl: func(string, ...ref.Val) ref.Val {
fn, ok := function.(func() any)
if !ok {
return types.WrapErr(fmt.Errorf("%s is expected to be of type func() any", name))
}
return celvalues.NewAdapter(types.DefaultTypeAdapter).NativeToValue(fn())
},
}
}
func celFunctionDeclarations(functions []CelFunction) []cel.EnvOption {
options := make([]cel.EnvOption, 0, len(functions))
for _, function := range functions {
overloads := make([]cel.FunctionOpt, 0, len(function.Overloads))
for _, overload := range function.Overloads {
declare := cel.Overload
if overload.Member {
declare = cel.MemberOverload
}
overloads = append(overloads, declare(overload.ID, overload.Args, overload.Result, cel.LateFunctionBinding()))
}
options = append(options, cel.Function(function.Name, overloads...))
}
return options
}
// celFunctionImpls validates the declarations and returns the implementations to bind for
// one evaluation.
func celFunctionImpls(functions []CelFunction) (map[string]cel.LateBoundFunction, error) {
if len(functions) == 0 {
return nil, nil
}
impls := make(map[string]cel.LateBoundFunction, len(functions))
for _, function := range functions {
if function.Impl == nil {
return nil, fmt.Errorf("cel function %s has no implementation", function.Name)
}
if len(function.Overloads) == 0 {
return nil, fmt.Errorf("cel function %s declares no overloads", function.Name)
}
for _, overload := range function.Overloads {
if overload.ID == "" || overload.Result == nil {
return nil, fmt.Errorf("cel function %s has an overload without an id or result type", function.Name)
}
}
if _, duplicate := impls[function.Name]; duplicate {
return nil, fmt.Errorf("cel function %s is declared more than once", function.Name)
}
impls[function.Name] = function.Impl
}
return impls, nil
}
// celFunctionSignature identifies the declarations, never the implementations, so templates
// that declare the same functions share an environment and a program.
func celFunctionSignature(functions []CelFunction) string {
var signature strings.Builder
for _, function := range functions {
signature.WriteString(function.Name)
for _, overload := range function.Overloads {
fmt.Fprintf(&signature, "|%s:%t:", overload.ID, overload.Member)
for _, arg := range overload.Args {
signature.WriteString(arg.String() + ",")
}
signature.WriteString("->" + overload.Result.String())
}
signature.WriteString(";")
}
return signature.String()
}