This repository was archived by the owner on Sep 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiterators.go
198 lines (168 loc) · 3.92 KB
/
iterators.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
/*
Written by Daniel Krom
2018
// added ability to ignore lock
*/
package jonson
// iterates on slice
func (jsn *JSON) SliceForEach(cb func(jsn *JSON, index int)) *JSON {
isSlice, slice := jsn.GetSlice()
if !isSlice {
return jsn
}
jsn.rwMutex.RLock()
defer jsn.rwMutex.RUnlock()
for i, v := range slice {
cb(v, i)
}
return jsn
}
// iterates on slice with map callback, transforming the slice to new slice
func (jsn *JSON) SliceMap(cb func(jsn *JSON, index int) *JSON) *JSON {
isSlice, slice := jsn.GetSlice()
if !isSlice {
return jsn
}
jsn.rwMutex.RLock()
mappedArr := make([]*JSON, len(slice))
for i, v := range slice {
mappedArr[i] = cb(v, i)
}
jsn.rwMutex.RUnlock()
jsn.rwMutex.Lock()
defer jsn.rwMutex.Unlock()
jsn.value = mappedArr
return jsn
}
// iterates on slice with filter callback, removes values that callback returned false
func (jsn *JSON) SliceFilter(cb func(jsn *JSON, index int) (shouldKeep bool)) *JSON {
isSlice, slice := jsn.GetSlice()
if !isSlice {
return jsn
}
jsn.rwMutex.RLock()
filteredArr := make([]*JSON, 0)
for i, v := range slice {
if cb(v, i) {
filteredArr = append(filteredArr, slice[i])
}
}
jsn.rwMutex.RUnlock()
jsn.rwMutex.Lock()
defer jsn.rwMutex.Unlock()
jsn.value = filteredArr
return jsn
}
// iterates on object
func (jsn *JSON) ObjectForEach(cb func(jsn *JSON, key string)) *JSON {
isMap, hMap := jsn.GetMap()
if !isMap {
return jsn
}
jsn.rwMutex.RLock()
defer jsn.rwMutex.RUnlock()
for k, v := range hMap {
cb(v, k)
}
return jsn
}
// iterates on object, replacing each value with new returned value
func (jsn *JSON) ObjectMap(cb func(jsn *JSON, key string) *JSON) *JSON {
isMap, hMap := jsn.GetMap()
if !isMap {
return jsn
}
jsn.rwMutex.RLock()
res := make(map[string]*JSON)
for k, v := range hMap {
res[k] = cb(v, k)
}
jsn.rwMutex.RUnlock()
jsn.rwMutex.Lock()
defer jsn.rwMutex.Unlock()
jsn.value = res
return jsn
}
// iterates on object, removing each value that cb returns false
func (jsn *JSON) ObjectFilter(cb func(jsn *JSON, key string) (shouldKeep bool)) *JSON {
isMap, hMap := jsn.GetMap()
if !isMap {
return jsn
}
jsn.rwMutex.RLock()
res := make(map[string]*JSON)
for k, v := range hMap {
if cb(v, k) {
res[k] = v
}
}
jsn.rwMutex.RUnlock()
jsn.rwMutex.Lock()
defer jsn.rwMutex.Unlock()
jsn.value = res
return jsn
}
func EqualsDeep(left *JSON, right *JSON) bool {
if left.kind != right.kind {
return false
}
if left.isPrimitive && right.isPrimitive {
lByte := left.ToUnsafeJSON()
rByte := right.ToUnsafeJSON()
if len(lByte) != len(rByte) {
return false
}
// if they are the same type, let's just compare the bytes, easiest way to do it
for i := range lByte {
if lByte[i] != rByte[i] {
return false
}
}
return true
}
if left.IsSlice() && right.IsSlice() {
lSlice := left.GetUnsafeSlice()
rSlice := right.GetUnsafeSlice()
if len(lSlice) != len(rSlice) {
return false
}
for i := range lSlice {
if !EqualsDeep(lSlice[i], rSlice[i]) {
return false
}
}
return true
}
if left.IsMap() && right.IsMap() {
lKeys := left.GetObjectKeys()
rKeys := right.GetObjectKeys()
if len(lKeys) != len(rKeys) {
return false
}
// it is usually faster to first validate tha each key exists on the other, and only then compare
// because compare might be long recursive and we can find already that the next key doesn't
// exists on the other so we didn't need the long recursive run
lMap := left.GetUnsafeMap()
rMap := right.GetUnsafeMap()
for i := range lKeys {
lKey := lKeys[i]
rKey := rKeys[i]
_, hasLKeyOnRightMap := rMap[lKey]
if !hasLKeyOnRightMap {
return false
}
_, hasRKeyOnLeftMap := lMap[rKey]
if !hasRKeyOnLeftMap {
return false
}
}
// on this loop, we already know that the keys are exactly the same
for key := range lMap {
if !EqualsDeep(lMap[key], rMap[key]) {
return false
}
}
return true
}
return false
}