forked from jmespath/go-jmespath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserfn.go
51 lines (47 loc) · 1.33 KB
/
userfn.go
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
package jmespath
import (
"fmt"
"strings"
)
type ExpressionEvaluator func(value interface{}) (interface{}, error)
func NewExpressionEvaluator(intrArg interface{}, expArg interface{}) ExpressionEvaluator {
intr := intrArg.(*treeInterpreter)
node := expArg.(expRef).ref
return func(value interface{}) (interface{}, error) {
return intr.Execute(node, value)
}
}
func (jp *JMESPath) RegisterFunction(name string, args string, variadic bool, handler func([]interface{}) (interface{}, error)) error {
hasExpRef := false
var arguments []argSpec
for _, arg := range strings.Split(args, ",") {
var argTypes []jpType
for _, argType := range strings.Split(arg, "|") {
switch t := jpType(argType); t {
case jpExpref:
hasExpRef = true
fallthrough
case jpNumber, jpString, jpArray, jpObject, jpArrayNumber, jpArrayString, jpAny:
argTypes = append(argTypes, t)
default:
return fmt.Errorf("unknown argument type: %s", argType)
}
}
arguments = append(arguments, argSpec{
types: argTypes,
})
}
if variadic {
if len(arguments) == 0 {
return fmt.Errorf("variadic functions require at least one argument")
}
arguments[len(arguments)-1].variadic = true
}
jp.intr.fCall.functionTable[name] = functionEntry{
name: name,
arguments: arguments,
handler: handler,
hasExpRef: hasExpRef,
}
return nil
}