-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
180 lines (166 loc) · 3.18 KB
/
util.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
package astjson
import (
"bytes"
"unsafe"
)
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func s2b(s string) (b []byte) {
return unsafe.Slice(unsafe.StringData(s), len(s))
}
const maxStartEndStringLen = 80
func startEndString(s string) string {
if len(s) <= maxStartEndStringLen {
return s
}
start := s[:40]
end := s[len(s)-40:]
return start + "..." + end
}
var (
NullValue = MustParse(`null`)
)
func MergeValues(a, b *Value) (*Value, bool) {
if a == nil {
return b, true
}
if b == nil {
return a, false
}
if a.Type() != b.Type() {
return a, false
}
switch a.Type() {
case TypeObject:
ao, _ := a.Object()
bo, _ := b.Object()
ao.Visit(func(key []byte, l *Value) {
sKey := b2s(key)
r := bo.Get(sKey)
if r == nil {
return
}
merged, changed := MergeValues(l, r)
if changed {
ao.Set(b2s(key), merged)
}
})
bo.Visit(func(key []byte, r *Value) {
sKey := b2s(key)
if ao.Get(sKey) != nil {
return
}
ao.Set(sKey, r)
})
return a, false
case TypeArray:
aa, _ := a.Array()
ba, _ := b.Array()
for i := 0; i < len(ba); i++ {
a.SetArrayItem(len(aa)+i, ba[i])
}
return a, false
case TypeFalse:
if b.Type() == TypeTrue {
return b, true
}
return a, false
case TypeTrue:
if b.Type() == TypeFalse {
return b, true
}
return a, false
case TypeNull:
if b.Type() != TypeNull {
return b, true
}
return a, false
case TypeNumber:
af, _ := a.Float64()
bf, _ := b.Float64()
if af != bf {
return b, true
}
return a, false
case TypeString:
as, _ := a.StringBytes()
bs, _ := b.StringBytes()
if !bytes.Equal(as, bs) {
return b, true
}
return a, false
default:
return b, true
}
}
func MergeValuesWithPath(a, b *Value, path ...string) (*Value, bool) {
if len(path) == 0 {
return MergeValues(a, b)
}
root := MustParseBytes([]byte(`{}`))
current := root
for i := 0; i < len(path)-1; i++ {
current.Set(path[i], MustParseBytes([]byte(`{}`)))
current = current.Get(path[i])
}
current.Set(path[len(path)-1], b)
return MergeValues(a, root)
}
func AppendToArray(array, value *Value) {
if array.Type() != TypeArray {
return
}
items, _ := array.Array()
array.SetArrayItem(len(items), value)
}
func SetValue(v *Value, value *Value, path ...string) {
for i := 0; i < len(path)-1; i++ {
parent := v
v = v.Get(path[i])
if v == nil {
child := MustParse(`{}`)
parent.Set(path[i], child)
v = child
}
}
v.Set(path[len(path)-1], value)
}
func SetNull(v *Value, path ...string) {
SetValue(v, MustParse(`null`), path...)
}
func ValueIsNonNull(v *Value) bool {
if v == nil {
return false
}
if v.Type() == TypeNull {
return false
}
return true
}
func ValueIsNull(v *Value) bool {
return !ValueIsNonNull(v)
}
func DeduplicateObjectKeysRecursively(v *Value) {
if v.Type() == TypeArray {
a := v.GetArray()
for _, e := range a {
DeduplicateObjectKeysRecursively(e)
}
}
if v.Type() != TypeObject {
return
}
o, _ := v.Object()
seen := make(map[string]struct{})
o.Visit(func(k []byte, v *Value) {
key := string(k)
if _, ok := seen[key]; ok {
o.Del(key)
return
} else {
seen[key] = struct{}{}
}
DeduplicateObjectKeysRecursively(v)
})
}