-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathjq.go
65 lines (53 loc) · 1.33 KB
/
jq.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package json
import (
"fmt"
"github.com/itchyny/gojq"
"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/coerce"
"github.com/project-flogo/core/data/expression/function"
)
func init() {
function.Register(&jq{})
}
type jq struct {
}
// Name returns the name of the function
func (jq) Name() string {
return "jq"
}
// Sig returns the function signature
func (jq) Sig() (paramTypes []data.Type, isVariadic bool) {
return []data.Type{data.TypeAny, data.TypeString}, false
}
// Eval executes the function
func (jq) Eval(params ...interface{}) (interface{}, error) {
var err error
var inputJSON interface{}
//1st parameter will be the input json
inputJSON, err = coerce.ToAny(params[0])
if err != nil {
return nil, fmt.Errorf("unable to coerce input JSON [%+v]: %s", params[0], err.Error())
}
//2nd parameter will be the query
inputQuery, err := coerce.ToString(params[1])
if err != nil {
return nil, fmt.Errorf("unable to coerce the query to string: %s", err.Error())
}
query, err := gojq.Parse(inputQuery)
if err != nil {
return nil, err
}
var result []interface{}
iter := query.Run(inputJSON)
for {
v, ok := iter.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
return nil, fmt.Errorf("error running the query : %s", err)
}
result = append(result, v)
}
return result, nil
}