-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
218 lines (202 loc) · 5.62 KB
/
tree.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
// Tideland Go Dynamic JSON
//
// Copyright (C) 2019-2023 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package dynaj // import "tideland.dev/go/dynaj"
//--------------------
// IMPORTS
//--------------------
import (
"fmt"
)
//--------------------
// TREE FUNCTIONS
//--------------------
// insertValue recursively inserts a value at the end of the keys list.
func insertValue(element Element, keys Keys, value Value) (Element, error) {
if len(keys) == 0 {
return value, nil
}
switch tnode := element.(type) {
case nil:
return createValue(keys, value)
case Object:
return insertValueInObject(tnode, keys, value)
case Array:
return insertValueInArray(tnode, keys, value)
default:
return nil, fmt.Errorf("document is not a valid JSON structure")
}
}
// createValue creates a value at the end of the keys list.
func createValue(keys Keys, value Value) (Element, error) {
// Check if we are at the end of the keys list.
if len(keys) == 0 {
return value, nil
}
h, t := headTail(keys)
// Check for array index first.
index, ok := asIndex(h)
if ok {
// It's an array index.
arr := make(Array, index+1)
element, err := createValue(t, value)
if err != nil {
return nil, err
}
arr[index] = element
return arr, nil
}
// It's an object key.
obj := Object{h: nil}
element, err := createValue(t, value)
if err != nil {
return nil, err
}
obj[h] = element
return obj, nil
}
// insertValueInObject inserts a value in a JSON object at the end of the keys list.
func insertValueInObject(obj Object, keys Keys, value Value) (Element, error) {
h, t := headTail(keys)
// Create object if keys list has only one element.
if len(t) == 0 {
if isObjectOrArray(obj[h]) {
return nil, fmt.Errorf("cannot insert value at %v: would corrupt document", keys)
}
_, ok := asIndex(h)
if ok {
return nil, fmt.Errorf("cannot insert value at %v: index %q in object", keys, h)
}
obj[h] = value
return obj, nil
}
// Insert value in element.
element := obj[h]
if isValue(element) {
return nil, fmt.Errorf("cannot insert value at %v: would corrupt document", keys)
}
newElement, err := insertValue(element, t, value)
if err != nil {
return nil, err
}
obj[h] = newElement
return obj, nil
}
// insertValueInArray inserts a value in an array at a given path.
func insertValueInArray(arr Array, keys Keys, value Value) (Element, error) {
h, t := headTail(keys)
// Convert path head into index.
index, ok := asIndex(h)
switch {
case !ok:
return nil, fmt.Errorf("cannot insert value at %v: invalid index %q", keys, index)
case index < 0:
return nil, fmt.Errorf("cannot insert value at %v: negative index %d", keys, index)
case index >= len(arr):
tmp := make(Array, index+1)
copy(tmp, arr)
arr = tmp
}
// Insert value if last element in path.
if len(t) == 0 {
if isObjectOrArray(arr[index]) {
return nil, fmt.Errorf("cannot insert value at %v: would corrupt document", keys)
}
arr[index] = value
return arr, nil
}
// Insert value in element.
element := arr[index]
if isValue(element) {
return nil, fmt.Errorf("cannot insert value at %v: would corrupt document", keys)
}
newElement, err := insertValue(element, t, value)
if err != nil {
return nil, err
}
arr[index] = newElement
return arr, nil
}
// deleteElement recursively deletes a element at the end of the keys list.
func deleteElement(element Element, keys Keys, deep bool) (Element, error) {
if len(keys) == 0 {
return nil, nil
}
switch tnode := element.(type) {
case nil:
return nil, fmt.Errorf("cannot delete value at %v: invalid path", keys)
case Object:
return deleteElementInObject(tnode, keys, deep)
case Array:
return deleteElementInArray(tnode, keys, deep)
default:
return nil, fmt.Errorf("cannot delete value at %v: path too long", keys)
}
}
// deleteElementInObject deletes a value in a JSON object at the end of the keys list.
func deleteElementInObject(obj Object, keys Keys, deep bool) (Element, error) {
h, t := headTail(keys)
// Delete object if keys list has only one element.
if len(t) == 0 {
if deep {
// Delete all.
delete(obj, h)
return obj, nil
}
// Not deep, so delete only if value.
if isObjectOrArray(obj[h]) {
return nil, fmt.Errorf("cannot delete value at %v: is no value", keys)
}
delete(obj, h)
return obj, nil
}
// Delete element.
element := obj[h]
newElement, err := deleteElement(element, t, deep)
if err != nil {
return nil, err
}
obj[h] = newElement
return obj, nil
}
// deleteElementInArray deletes a value in a JSON array at a given path.
func deleteElementInArray(arr Array, keys Keys, deep bool) (Element, error) {
h, t := headTail(keys)
// Convert head in index.
index, ok := asIndex(h)
switch {
case !ok:
return nil, fmt.Errorf("cannot delete value at %v: invalid index %q", keys, h)
case index < 0:
return nil, fmt.Errorf("cannot delete value at %v: negative index %d", keys, index)
case index >= len(arr):
return nil, fmt.Errorf("cannot delete value at %v: index %d out of range", keys, index)
}
// Delete object if keys list has only one element.
if len(t) == 0 {
if deep {
// Delete all.
copy(arr[index:], arr[index+1:])
arr = arr[:len(arr)-1]
}
// Not deep, so delete only if value.
if isObjectOrArray(arr[index]) {
return nil, fmt.Errorf("cannot delete value at %v: is no value", keys)
}
copy(arr[index:], arr[index+1:])
arr = arr[:len(arr)-1]
return arr, nil
}
// Delete value in element.
element := arr[index]
newElement, err := deleteElement(element, t, deep)
if err != nil {
return nil, err
}
arr[index] = newElement
return arr, nil
}
// EOF