-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathexists.go
47 lines (40 loc) · 1.06 KB
/
exists.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
package json
import (
"fmt"
"strings"
"github.com/oliveagle/jsonpath"
"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/expression/function"
)
func init() {
_ = function.Register(&fnExists{})
}
type fnExists struct {
}
// Name returns the name of the function
func (fnExists) Name() string {
return "exists"
}
// Sig returns the function signature
func (fnExists) Sig() (paramTypes []data.Type, isVariadic bool) {
return []data.Type{data.TypeAny, data.TypeString}, false
}
// Eval executes the function
func (fnExists) Eval(params ...interface{}) (interface{}, error) {
expression, ok := params[1].(string)
if !ok {
return false, fmt.Errorf("The JSON key/path must be a string")
}
//tmp fix to take $loop as $. for now
if strings.HasPrefix(strings.TrimSpace(expression), "$loop.") {
expression = strings.Replace(expression, "$loop", "$", -1)
}
if !strings.HasPrefix(expression, "$.") {
expression = "$." + expression
}
_, err := jsonpath.JsonPathLookup(params[0], expression)
if err != nil {
return false, nil
}
return true, nil
}