-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrootselector.go
More file actions
119 lines (110 loc) · 3.31 KB
/
Copy pathrootselector.go
File metadata and controls
119 lines (110 loc) · 3.31 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
package jsonframer
import (
"encoding/json"
"errors"
"github.com/itchyny/gojq"
"github.com/tidwall/gjson"
jsonata "github.com/xiatechs/jsonata-go"
)
func ApplyRootSelector(jsonString string, rootSelector string, framerType FramerType) (string, error) {
if rootSelector == "" {
return jsonString, nil
}
if framerType == FramerTypeGJSON {
return ApplyRootSelectorUsingGJSON(jsonString, rootSelector)
}
if framerType == FramerTypeJsonata {
return ApplyRootSelectorUsingJSONata(jsonString, rootSelector)
}
if framerType == FramerTypeJQ {
return ApplyRootSelectorUsingJQ(jsonString, rootSelector)
}
return ApplyRootSelectorUsingWithGuess(jsonString, rootSelector)
}
func ApplyRootSelectorUsingGJSON(jsonString string, rootSelector string) (string, error) {
r := gjson.Get(jsonString, rootSelector)
if r.Exists() {
return r.String(), nil
}
return jsonString, ErrInvalidRootSelector
}
func ApplyRootSelectorUsingJSONata(jsonString string, rootSelector string) (string, error) {
expr, err := jsonata.Compile(rootSelector)
if err != nil {
return "", errors.Join(ErrInvalidRootSelector, err)
}
if expr == nil {
return "", errors.Join(ErrInvalidRootSelector)
}
return ApplyRootSelectorUsingJSONataExpression(jsonString, expr)
}
func ApplyRootSelectorUsingJSONataExpression(jsonString string, expr *jsonata.Expr) (string, error) {
var data any
err := json.Unmarshal([]byte(jsonString), &data)
if err != nil {
return "", errors.Join(ErrInvalidJSONContent, err)
}
res, err := expr.Eval(data)
if err != nil {
return "", errors.Join(ErrEvaluatingJSONata, err)
}
r2, err := json.Marshal(res)
if err != nil {
return "", errors.Join(ErrInvalidJSONContent, err)
}
return string(r2), nil
}
func ApplyRootSelectorUsingJQ(jsonString string, rootSelector string) (string, error) {
query, err := gojq.Parse(rootSelector)
if err != nil {
return "", errors.Join(ErrInvalidJQSelector, err)
}
return ApplyRootSelectorUsingJQQuery(jsonString, query)
}
func ApplyRootSelectorUsingJQQuery(jsonString string, query *gojq.Query) (string, error) {
var data any
err := json.Unmarshal([]byte(jsonString), &data)
if err != nil {
return "", errors.Join(ErrUnMarshalingJSON, err)
}
iter := query.Run(data)
out := []any{}
for {
v, ok := iter.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
if err, ok := err.(*gojq.HaltError); ok && err.Value() == nil {
break
}
return "", errors.Join(ErrExecutingJQ, err)
}
out = append(out, v)
}
if len(out) == 1 {
if v, ok := out[0].([]any); ok {
outStr, err := json.Marshal(v)
if err != nil {
return "", errors.Join(ErrMarshalingJSON, err)
}
return string(outStr), nil
}
}
outStr, err := json.Marshal(out)
if err != nil {
return "", errors.Join(ErrMarshalingJSON, err)
}
return string(outStr), nil
}
// ApplyRootSelectorUsingWithGuess method try to guess the root selector (for legacy reasons)
// First it will attempt to GJSON based selector (used by many users)
// If the GJSON based selection failed, it will default fallback to JSONata based selection
// Intentionally we are not falling back to JQ yet
func ApplyRootSelectorUsingWithGuess(jsonString string, rootSelector string) (string, error) {
r := gjson.Get(jsonString, rootSelector)
if r.Exists() {
return r.String(), nil
}
return ApplyRootSelectorUsingJSONata(jsonString, rootSelector)
}