-
Notifications
You must be signed in to change notification settings - Fork 3
/
jsonmask.go
257 lines (231 loc) · 4.51 KB
/
jsonmask.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
package jsonmask
import (
"bytes"
"encoding/json"
"fmt"
)
const (
eRaw = iota
eObj
eAry
eOther
)
var jsonNull = []byte("null")
// Mask selects the specific parts of an JSON string, according to the mask "fields".
func Mask(doc []byte, fields string) ([]byte, error) {
if !json.Valid(doc) {
return nil, fmt.Errorf("invalid json string")
}
sl, err := Compile(fields)
if err != nil {
return nil, err
}
return sl.Mask(doc)
}
func (sl Selection) Mask(doc []byte) ([]byte, error) {
if len(doc) == 0 || len(sl) == 0 {
return doc, nil
}
raw := json.RawMessage(doc)
src := newLazyNode(&raw)
dst := newLazyNode(nil)
if err := copyLazyNode(dst, src, sl); err != nil {
return nil, err
}
return json.Marshal(dst)
}
// for testing purposes only.
func jsonDeepEqual(a, b []byte) bool {
rawa := json.RawMessage(a)
rawb := json.RawMessage(b)
return newLazyNode(&rawa).deepEqual(newLazyNode(&rawb))
}
func newLazyNode(raw *json.RawMessage) *lazyNode {
return &lazyNode{raw: raw}
}
type lazyNode struct {
raw *json.RawMessage
obj *partialObj
ary partialArray
which int
}
func (n *lazyNode) MarshalJSON() ([]byte, error) {
switch n.which {
case eObj:
return json.Marshal(n.obj)
case eAry:
return json.Marshal(n.ary)
default:
if n.raw != nil {
return *n.raw, nil
}
return jsonNull, nil
}
}
func (n *lazyNode) UnmarshalJSON(data []byte) error {
dest := json.RawMessage(data)
n.raw = &dest
n.which = eRaw
return nil
}
func (n *lazyNode) unmarshal() error {
if n.which != eRaw {
return nil
}
n.which = eOther
if n.raw == nil {
return nil
}
switch checkWhich(*n.raw) {
case eObj:
err := json.Unmarshal(*n.raw, &n.obj)
if err != nil {
return err
}
n.which = eObj
case eAry:
err := json.Unmarshal(*n.raw, &n.ary)
if err != nil {
return nil
}
n.which = eAry
}
return nil
}
// for testing purposes only.
func (n *lazyNode) deepEqual(other *lazyNode) bool {
if err := n.unmarshal(); err != nil {
return false
}
if err := other.unmarshal(); err != nil {
return false
}
if n.which != other.which {
return false
}
switch n.which {
case eObj:
return n.obj.deepEqual(other.obj)
case eAry:
if other.ary == nil {
return false
}
if len(n.ary) != len(other.ary) {
return false
}
for i, v := range n.ary {
if !v.deepEqual(other.ary[i]) {
return false
}
}
return true
}
if n.raw == nil {
return other.raw == nil
}
if other.raw == nil {
return false
}
var nb, otherb bytes.Buffer
if err := json.Compact(&nb, *n.raw); err != nil {
return false
}
if err := json.Compact(&otherb, *other.raw); err != nil {
return false
}
return bytes.Equal(nb.Bytes(), otherb.Bytes())
}
type partialArray []*lazyNode
type partialObj struct {
obj map[string]*lazyNode
}
// for testing purposes only.
func (n *partialObj) deepEqual(other *partialObj) bool {
if other == nil {
return false
}
if len(n.obj) != len(other.obj) {
return false
}
for k, v := range n.obj {
ov, ok := other.obj[k]
if !ok {
return false
}
if !v.deepEqual(ov) {
return false
}
}
return true
}
func (n *partialObj) MarshalJSON() ([]byte, error) {
return json.Marshal(n.obj)
}
func (n *partialObj) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &n.obj)
}
func checkWhich(buf []byte) int {
for _, c := range buf {
switch c {
case ' ':
case '\n':
case '\t':
case '[':
return eAry
case '{':
return eObj
default:
return eOther
}
}
return eOther
}
func copyLazyNode(dst, src *lazyNode, sl Selection) error {
err := src.unmarshal()
if err != nil {
return err
}
dst.which = src.which
switch src.which {
case eObj:
if len(sl) == 0 {
dst.obj = src.obj
return nil
}
dst.obj = &partialObj{obj: make(map[string]*lazyNode)}
for sk, sv := range sl {
if sk == "*" {
for nk, nv := range src.obj.obj {
dst.obj.obj[nk] = newLazyNode(nil)
if err := copyLazyNode(dst.obj.obj[nk], nv, sv); err != nil {
return err
}
}
} else if nv := src.obj.obj[sk]; nv != nil {
dst.obj.obj[sk] = newLazyNode(nil)
if err := copyLazyNode(dst.obj.obj[sk], nv, sv); err != nil {
return err
}
}
}
case eAry:
if len(sl) == 0 {
dst.ary = src.ary
return nil
}
dst.ary = make([]*lazyNode, len(src.ary))
for i := range src.ary {
dst.ary[i] = newLazyNode(nil)
if err := copyLazyNode(dst.ary[i], src.ary[i], sl); err != nil {
return err
}
}
default:
if len(sl) == 0 {
dst.raw = src.raw
return nil
}
return fmt.Errorf("can not select: not a object or array")
}
return nil
}