forked from spyzhov/ajson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_mutations.go
373 lines (341 loc) · 8.12 KB
/
node_mutations.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package ajson
import (
"strconv"
"sync/atomic"
)
// IsDirty is the flag that shows, was node changed or not
func (n *Node) IsDirty() bool {
return n.dirty
}
// Set updates current node value with the value of any type
func (n *Node) Set(value interface{}) error {
if value == nil {
return n.SetNull()
}
switch result := value.(type) {
case float64, float32, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
if tValue, err := numeric2float64(value); err != nil {
return err
} else {
return n.SetNumeric(tValue)
}
case string:
return n.SetString(result)
case bool:
return n.SetBool(result)
case []*Node:
return n.SetArray(result)
case map[string]*Node:
return n.SetObject(result)
case *Node:
return n.SetNode(result)
default:
return unsupportedType(value)
}
}
// SetNull updates current node value with Null value
func (n *Node) SetNull() error {
return n.update(Null, nil)
}
// SetNumeric updates current node value with Numeric value
func (n *Node) SetNumeric(value float64) error {
return n.update(Numeric, value)
}
// SetString updates current node value with String value
func (n *Node) SetString(value string) error {
return n.update(String, value)
}
// SetBool updates current node value with Bool value
func (n *Node) SetBool(value bool) error {
return n.update(Bool, value)
}
// SetArray updates current node value with Array value
func (n *Node) SetArray(value []*Node) error {
return n.update(Array, value)
}
// SetObject updates current node value with Object value
func (n *Node) SetObject(value map[string]*Node) error {
return n.update(Object, value)
}
// SetNode updates current node value with the clone of the given Node value
// NB! The result will be the clone of the given Node!
func (n *Node) SetNode(value *Node) error {
if n == value {
// Attempt to set current node as the value: node.SetNode(node)
return nil
}
if n.isParentOrSelfNode(value) {
return errorRequest("attempt to create infinite loop")
}
node := value.Clone()
node.setReference(n.parent, n.key, n.index)
n.setReference(nil, nil, nil)
*n = *node
if n.parent != nil {
n.parent.mark()
}
return nil
}
// AppendArray appends current Array node values with Node values
func (n *Node) AppendArray(value ...*Node) error {
if !n.IsArray() {
return errorType()
}
for _, val := range value {
if err := n.appendNode(nil, val); err != nil {
return err
}
}
n.mark()
return nil
}
// AppendObject appends current Object node value with key:value
func (n *Node) AppendObject(key string, value *Node) error {
if !n.IsObject() {
return errorType()
}
err := n.appendNode(&key, value)
if err != nil {
return err
}
n.mark()
return nil
}
// DeleteNode removes element child
func (n *Node) DeleteNode(value *Node) error {
return n.remove(value)
}
// DeleteKey removes element from Object, by it's key
func (n *Node) DeleteKey(key string) error {
node, err := n.GetKey(key)
if err != nil {
return err
}
return n.remove(node)
}
// PopKey removes element from Object, by it's key and return it
func (n *Node) PopKey(key string) (node *Node, err error) {
node, err = n.GetKey(key)
if err != nil {
return
}
return node, n.remove(node)
}
// DeleteIndex removes element from Array, by it's index
func (n *Node) DeleteIndex(index int) error {
node, err := n.GetIndex(index)
if err != nil {
return err
}
return n.remove(node)
}
// PopIndex removes element from Array, by it's index and return it
func (n *Node) PopIndex(index int) (node *Node, err error) {
node, err = n.GetIndex(index)
if err != nil {
return
}
return node, n.remove(node)
}
// Delete removes element from parent. For root - do nothing.
func (n *Node) Delete() error {
if n.parent == nil {
return nil
}
return n.parent.remove(n)
}
// Clone creates full copy of current Node. With all child, but without link to the parent.
func (n *Node) Clone() *Node {
node := n.clone()
node.setReference(nil, nil, nil)
return node
}
func (n *Node) clone() *Node {
node := &Node{
parent: n.parent,
children: make(map[string]*Node, len(n.children)),
key: n.key,
index: n.index,
_type: n._type,
data: n.data,
borders: n.borders,
value: n.value,
dirty: n.dirty,
}
for key, value := range n.children {
node.children[key] = value.clone()
}
return node
}
// update method updates stored value, with validations
func (n *Node) update(_type NodeType, value interface{}) error {
// validate
err := n.validate(_type, value)
if err != nil {
return err
}
// update
n.mark()
n.clear()
atomic.StoreInt32((*int32)(&n._type), int32(_type))
n.value = atomic.Value{}
if value != nil {
switch _type {
case Array:
nodes := value.([]*Node)
n.children = make(map[string]*Node, len(nodes))
for _, node := range nodes {
if err = n.appendNode(nil, node); err != nil {
return err
}
}
case Object:
nodes := value.(map[string]*Node)
n.children = make(map[string]*Node, len(nodes))
for key, node := range nodes {
if err = n.appendNode(&key, node); err != nil {
return err
}
}
}
n.value.Store(value)
}
return nil
}
// validate method validates stored value, before update
func (n *Node) validate(_type NodeType, value interface{}) error {
if n == nil {
return errorUnparsed()
}
switch _type {
case Null:
if value != nil {
return errorType()
}
case Numeric:
if _, ok := value.(float64); !ok {
return errorType()
}
case String:
if _, ok := value.(string); !ok {
return errorType()
}
case Bool:
if _, ok := value.(bool); !ok {
return errorType()
}
case Array:
if value != nil {
if _, ok := value.([]*Node); !ok {
return errorType()
}
}
case Object:
if value != nil {
if _, ok := value.(map[string]*Node); !ok {
return errorType()
}
}
}
return nil
}
// remove method removes value from current container
func (n *Node) remove(value *Node) error {
if !n.isContainer() {
return errorType()
}
if value.parent != n {
return errorRequest("wrong parent")
}
n.mark()
if n.IsArray() {
delete(n.children, strconv.Itoa(*value.index))
n.dropindex(*value.index)
} else {
delete(n.children, *value.key)
}
value.parent = nil
return nil
}
// dropindex: internal method to reindexing current array value
func (n *Node) dropindex(index int) {
for i := index + 1; i <= len(n.children); i++ {
previous := i - 1
if current, ok := n.children[strconv.Itoa(i)]; ok {
current.index = &previous
n.children[strconv.Itoa(previous)] = current
}
delete(n.children, strconv.Itoa(i))
}
}
// appendNode appends current Node node value with new Node value, by key or index
func (n *Node) appendNode(key *string, value *Node) error {
if n.isParentOrSelfNode(value) {
return errorRequest("attempt to create infinite loop")
}
if value.parent != nil {
if err := value.parent.remove(value); err != nil {
return err
}
}
value.parent = n
value.key = key
if key != nil {
if old, ok := n.children[*key]; ok {
if old != value {
if err := n.remove(old); err != nil {
return err
}
}
}
n.children[*key] = value
} else {
index := len(n.children)
value.index = &index
n.children[strconv.Itoa(index)] = value
}
return nil
}
// mark node as dirty, with all parents (up the tree)
func (n *Node) mark() {
node := n
for node != nil && !node.dirty {
node.dirty = true
node = node.parent
}
}
// clear current value of node
func (n *Node) clear() {
n.data = nil
n.borders[1] = 0
for key := range n.children {
n.children[key].parent = nil
}
n.children = nil
}
// isParentOrSelfNode check if current node is the same as given one of parents
func (n *Node) isParentOrSelfNode(node *Node) bool {
return n == node || n.isParentNode(node)
}
// isParentNode check if current node is one of the parents
func (n *Node) isParentNode(node *Node) bool {
if n != nil {
for current := n.parent; current != nil; current = current.parent {
if current == node {
return true
}
}
}
return false
}
// setReference updates references of current node
func (n *Node) setReference(parent *Node, key *string, index *int) {
n.parent = parent
if key == nil {
n.key = nil
} else {
temp := *key
n.key = &temp
}
n.index = index
}