-
-
Notifications
You must be signed in to change notification settings - Fork 439
/
node.go
263 lines (232 loc) · 4.34 KB
/
node.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package jsonx
import (
"strconv"
jsonpath "github.com/antonmedv/fx/path"
)
type Node struct {
Prev, Next, End *Node
directParent *Node
indirectParent *Node
Collapsed *Node
Depth uint8
Key []byte
Value []byte
Size int
Chunk []byte
ChunkEnd *Node
Comma bool
Index int
}
// append ands a node as a child to the current node (body of {...} or [...]).
func (n *Node) append(child *Node) {
if n.End == nil {
n.End = n
}
n.End.Next = child
child.Prev = n.End
if child.End == nil {
n.End = child
} else {
n.End = child.End
}
}
// adjacent adds a node as a sibling to the current node ({}{}{} or [][][]).
func (n *Node) adjacent(child *Node) {
end := n.End
if end == nil {
end = n
}
end.Next = child
child.Prev = end
}
func (n *Node) insertChunk(chunk *Node) {
if n.ChunkEnd == nil {
n.insertAfter(chunk)
} else {
n.ChunkEnd.insertAfter(chunk)
}
n.ChunkEnd = chunk
}
func (n *Node) insertAfter(child *Node) {
if n.Next == nil {
n.Next = child
child.Prev = n
} else {
old := n.Next
n.Next = child
child.Prev = n
child.Next = old
old.Prev = child
}
}
func (n *Node) dropChunks() {
if n.ChunkEnd == nil {
return
}
n.Chunk = nil
n.Next = n.ChunkEnd.Next
if n.Next != nil {
n.Next.Prev = n
}
n.ChunkEnd = nil
}
func (n *Node) HasChildren() bool {
return n.End != nil
}
func (n *Node) Parent() *Node {
if n.directParent == nil {
return nil
}
parent := n.directParent
if parent.indirectParent != nil {
parent = parent.indirectParent
}
return parent
}
func (n *Node) IsWrap() bool {
return n.Value == nil && n.Chunk != nil
}
func (n *Node) IsCollapsed() bool {
return n.Collapsed != nil
}
func (n *Node) Collapse() *Node {
if n.End != nil && !n.IsCollapsed() {
n.Collapsed = n.Next
n.Next = n.End.Next
if n.Next != nil {
n.Next.Prev = n
}
}
return n
}
func (n *Node) CollapseRecursively() {
var at *Node
if n.IsCollapsed() {
at = n.Collapsed
} else {
at = n.Next
}
for at != nil && at != n.End {
if at.HasChildren() {
at.CollapseRecursively()
at.Collapse()
}
at = at.Next
}
}
func (n *Node) Expand() {
if n.IsCollapsed() {
if n.Next != nil {
n.Next.Prev = n.End
}
n.Next = n.Collapsed
n.Collapsed = nil
}
}
func (n *Node) ExpandRecursively(level, maxLevel int) {
if level >= maxLevel {
return
}
if n.IsCollapsed() {
n.Expand()
}
it := n.Next
for it != nil && it != n.End {
if it.HasChildren() {
it.ExpandRecursively(level+1, maxLevel)
it = it.End.Next
} else {
it = it.Next
}
}
}
func (n *Node) FindChildByKey(key string) *Node {
it := n.Next
for it != nil && it != n.End {
if it.Key != nil {
k, err := strconv.Unquote(string(it.Key))
if err != nil {
return nil
}
if k == key {
return it
}
}
if it.ChunkEnd != nil {
it = it.ChunkEnd.Next
} else if it.End != nil {
it = it.End.Next
} else {
it = it.Next
}
}
return nil
}
func (n *Node) FindChildByIndex(index int) *Node {
for at := n.Next; at != nil && at != n.End; {
if at.Index == index {
return at
}
if at.End != nil {
at = at.End.Next
} else {
at = at.Next
}
}
return nil
}
func (n *Node) paths(prefix string, paths *[]string, nodes *[]*Node) {
it := n.Next
for it != nil && it != n.End {
var path string
if it.Key != nil {
quoted := string(it.Key)
unquoted, err := strconv.Unquote(quoted)
if err == nil && jsonpath.Identifier.MatchString(unquoted) {
path = prefix + "." + unquoted
} else {
path = prefix + "[" + quoted + "]"
}
} else if it.Index >= 0 {
path = prefix + "[" + strconv.Itoa(it.Index) + "]"
}
*paths = append(*paths, path)
*nodes = append(*nodes, it)
if it.HasChildren() {
it.paths(path, paths, nodes)
it = it.End.Next
} else {
it = it.Next
}
}
}
func (n *Node) Children() ([]string, []*Node) {
if !n.HasChildren() {
return nil, nil
}
var paths []string
var nodes []*Node
var it *Node
if n.IsCollapsed() {
it = n.Collapsed
} else {
it = n.Next
}
for it != nil && it != n.End {
if it.Key != nil {
key := string(it.Key)
unquoted, err := strconv.Unquote(key)
if err == nil {
key = unquoted
}
paths = append(paths, key)
nodes = append(nodes, it)
}
if it.HasChildren() {
it = it.End.Next
} else {
it = it.Next
}
}
return paths, nodes
}