-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgojq.go
232 lines (213 loc) · 4.94 KB
/
gojq.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package gojq
import (
"encoding/json"
"fmt"
"strconv"
)
// JNode is the node for tree representation of underlying JSON.
type JNode struct {
value interface{}
key string
parent *JNode
parray bool
isArray bool
Err error
}
// FromBytes constructs value of JNode from a JSON bytes string.
func FromBytes(jsonStr []byte) *JNode {
jn := &JNode{}
err := json.Unmarshal(jsonStr, &jn.value)
if err != nil {
jn.Err = err
}
return jn
}
// FromInterface creates jq object around the given value.
func FromInterface(val interface{}) *JNode {
return &JNode{value: val}
}
// String returns string representation of JNode's contents.
func (jn *JNode) String() string {
bjson, err := json.Marshal(jn.value)
if err != nil {
jn.Err = err
return fmt.Sprintf("[JNode Error: %v]", err)
}
return fmt.Sprintf("%s", bjson)
}
// As unmarshals value JSON into the given variable.
func (jn *JNode) As(v interface{}) error {
if jn.Err != nil {
return jn.Err
}
bstr, err := json.Marshal(jn.value)
if err != nil {
return err
}
return json.Unmarshal(bstr, v)
}
// Select walks down the JSON tree to the node at specified path.
func (jn *JNode) Select(path ...string) *JNode {
ret, err := recursiveWalk(jn, path)
if err != nil {
jn.Err = err
return jn
}
return ret
}
func recursiveWalk(jn *JNode, path []string) (*JNode, error) {
if len(path) == 0 {
return jn, jn.Err
}
key := path[0]
if len(path) == 1 {
var val interface{}
var arr bool
switch jn.value.(type) {
case string, float64, bool, nil: // simple value
return nil, fmt.Errorf("Can't index into primitive value [%s]", key)
case []interface{}: // array indexing
arr = true
idx, err := strconv.Atoi(key)
if err != nil {
return nil, err
}
val = jn.value.([]interface{})[idx]
case map[string]interface{}: // map walk
val = jn.value.(map[string]interface{})[key]
arr = false
}
ret := &JNode{
value: val,
key: key,
parent: jn,
parray: jn.isArray,
isArray: arr,
}
return ret, nil
}
rest := path[1:]
switch jn.value.(type) {
case []interface{}:
idx, err := strconv.Atoi(key)
if err != nil {
return nil, err
}
node := JNode{
value: jn.value.([]interface{})[idx],
key: key,
parent: jn,
parray: jn.isArray,
}
return recursiveWalk(&node, rest)
case map[string]interface{}:
node := JNode{
value: jn.value.(map[string]interface{})[key],
key: key,
parent: jn,
parray: false,
}
return recursiveWalk(&node, rest)
}
// if the value is a primitive
if len(rest) > 0 {
return jn, fmt.Errorf("Can not index into primitive value [%s]", rest[0])
}
return jn, nil
}
// Set sets the value of the current JNode in the json tree
func (jn *JNode) Set(val interface{}) *JNode {
if jn.parent != nil {
// update parent
switch jn.parent.value.(type) {
case []interface{}:
idx, err := strconv.Atoi(jn.key)
if err != nil {
jn.Err = err
return jn
}
jn.parent.value.([]interface{})[idx] = val
case map[string]interface{}:
jn.parent.value.(map[string]interface{})[jn.key] = val
}
}
// update its own value
jn.value = val
return jn
}
// Delete remove the current JNode in the json tree
func (jn *JNode) Delete() *JNode {
jn.value = nil
if jn.parent != nil {
// update parent
switch jn.parent.value.(type) {
case []interface{}:
idx, err := strconv.Atoi(jn.key)
if err != nil {
jn.Err = err
return jn
}
arr := jn.parent.value.([]interface{})
parent := jn.parent
parent.value = append(arr[:idx], arr[idx+1:]...)
if parent.parent != nil {
parent.parent.Select(parent.key).Set(parent.value)
}
case map[string]interface{}:
delete(jn.parent.value.(map[string]interface{}), jn.key)
}
}
return jn
}
// Exists checks to see if a property is present
func (jn *JNode) Exists(path ...string) bool {
q, err := recursiveWalk(jn, path)
if err != nil {
jn.Err = err
return false
}
if q.value != nil {
return true
}
return false
}
// Value return the value of the current JNode
func (jn *JNode) Value() interface{} {
return jn.value
}
// Iterator returns an iterator object for an array property
func (jn *JNode) Iterator() *JNIterator {
// TODO make sure it is an array
return &JNIterator{i: -1, data: jn.value.([]interface{})}
}
// JNIterator is iterator for array fields in a JNode
type JNIterator struct {
i int
data []interface{}
parent *JNode
}
// Next advances the iterator and returns if there is a next element
func (it *JNIterator) Next() bool {
it.i++
if it.i < len(it.data) {
return true
}
return false
}
// Value returns value of the current element
func (it *JNIterator) Value() interface{} {
return it.data[it.i]
}
// Node returns the current element wrapped in a JNode
func (it *JNIterator) Node() *JNode {
return &JNode{
value: it.data[it.i],
key: strconv.Itoa(it.i),
parent: it.parent,
parray: true,
}
}
// Count returns the number of elements in the iterator
func (it *JNIterator) Count() int {
return len(it.data)
}