-
Notifications
You must be signed in to change notification settings - Fork 0
/
crack.go
417 lines (387 loc) · 10.8 KB
/
crack.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package jsoncrack
import (
"encoding/json"
"fmt"
"github.com/fwhezfwhez/errorx"
"reflect"
"strings"
)
const (
MAP = 1 + iota
ARRAY
BYTES
)
type JsonMarshaller interface {
Marshal(interface{}) ([]byte, error)
Unmarshal([]byte, interface{}) error
}
type JsonCracker struct {
Json JsonMarshaller
}
// a jsonMarshaller realization via the officials
type Jsoner struct {
}
func (j Jsoner) Marshal(dest interface{}) ([]byte, error) {
return json.Marshal(dest)
}
func (j Jsoner) Unmarshal(data []byte, dest interface{}) error {
return json.Unmarshal(data, dest)
}
// json marshal via its jsoner realization
func (jc JsonCracker) Marshal(dest interface{}) ([]byte, error) {
return jc.Json.Marshal(dest)
}
// json unmarshal via its jsoner realization
func (jc JsonCracker) Unmarshal(data []byte, dest interface{}) error {
return jc.Json.Unmarshal(data, dest)
}
// New a json-cracker instance
// jsoner == nil , official json marshaler will be put to use,otherwise use jsoner as specific
func NewCracker(jsoner JsonMarshaller) JsonCracker {
if jsoner == nil {
jsoner = Jsoner{}
}
return JsonCracker{
Json: jsoner,
}
}
// set cracker's jsoner
// make sure the function executing while app is init, don't set while app is running.
// good:
// func init(){
// jc.SetJsoner(yourJsoner)
// }
//
// bad:
// func service() {
// jc.Marshal()
// ...
// jc.SetJsoner(yourJsoner)
// ...
// }
//
func (jc *JsonCracker) SetJsoner(jsoner JsonMarshaller) {
jc.Json = jsoner
}
// update a json raw-message with a new key-value pair
// assume data:
//{
// "class1": {
// "master": {
// "name": "Li Hua",
// "Age": 28
// },
// "students": [
// {
// "name": "Li Lei",
// "Age": 12
// },
// {
// "name": "Tom",
// "Age": 11
// }
// ]
// }
//}
// after exec jc.Update([]byte(data), "sub_name", "Li li", "class1", "master")
// returns:
//{
// "class1": {
// "master": {
// "name": "Li Hua",
// "Age": 28,
// "sub_name":"Li li"
// },
// "students": [
// {
// "name": "Li Lei",
// "Age": 12
// },
// {
// "name": "Tom",
// "Age": 11
// }
// ]
// }
//}
func (jc JsonCracker) Update(data []byte, k string, v interface{}, keys ...string) ([]byte, error) {
return jc.MustUpdate(data, k, v, keys...)
}
// add k-v pair into a json []byte
func (jc JsonCracker) Add(data []byte, k string, v interface{}, keys ...string) ([]byte, error) {
return jc.Update(data, k, v, keys ...)
}
// When exists the key 'k' , it replace the origin value with v
func (jc JsonCracker) MustUpdate(data []byte, k string, v interface{}, keys ...string) ([]byte, error) {
var dest map[string]interface{}
var err error
if err = jc.Unmarshal(data, &dest); err != nil {
return nil, errorx.NewFromString(fmt.Sprintf("data is not well format as json serial,error: '%s'", err.Error()))
}
keys = append(keys, k)
if err = updateParse(false, dest, v, keys...); err != nil {
return nil, errorx.NewFromString(fmt.Sprintf("error: '%s'", err.Error()))
}
return jc.Marshal(dest)
}
// when exists the key 'k', it throws an error without replacing
func (jc JsonCracker) SafeUpdate(data []byte, k string, v interface{}, keys ...string) ([]byte, error) {
var dest map[string]interface{}
var err error
if err = jc.Unmarshal(data, &dest); err != nil {
return nil, errorx.NewFromString(fmt.Sprintf("data is not well format as json serial,error: '%s'", err.Error()))
}
keys = append(keys, k)
if err = updateParse(true, dest, v, keys...); err != nil {
return nil, errorx.NewFromString(fmt.Sprintf("error: '%s'", err.Error()))
}
return jc.Marshal(dest)
}
// get a value via keys through a json []byte.
// value can specifc type as jsoncrack.BYTES or jsoncrack.MAP.The formmer returns json []bytes boxing in interface{},the
// other is a map[string]interface{} boxing in interface{}
func (jc JsonCracker) Get(vtype int, data []byte, keys ...string) (interface{}, error) {
var dest map[string]interface{}
er := jc.Unmarshal(data, &dest)
if er != nil {
return nil, errorx.NewFromString(fmt.Sprintf("'data' is not well json format,err: %s", er.Error()))
}
var result interface{}
er = getParse(&result, dest, keys ...)
if er != nil {
return nil, errorx.NewFromString(fmt.Sprintf("get parse err: %s", er.Error()))
}
if vtype == MAP {
return result, nil
} else {
return jc.Marshal(result)
}
}
func GetString(data []byte, keys ...string) string{
jc := NewCracker(nil)
bI, e :=jc.Get(BYTES, data, keys...)
if e!=nil {
panic(e)
}
var v string
e = jc.Unmarshal(bI.([]byte), &v)
if e!=nil {
panic(e)
}
return v
}
// vtype: jsoncrack.MAP,jsoncrack.Array,jsoncrack.BYTES
// When vtype is 'jsoncrack.Map', it returns (map[string]interface{}, error),
// otherwise it returns (json.RawMessage,error) or ([]byte, error)
//
// when safe is true, delete the specific key while not existed, throws an error.
// when safe is false, do nothing if not existed keys
//
// Delete() returns the data modified already, while keeping the former data unchanged.
// When data is input , function will marshal its unmarshal object again to get a copy of data
func (jc JsonCracker) Delete(vtype int, safe bool, data []byte, keys ...string) (interface{}, error) {
var dest map[string]interface{}
er := jc.Unmarshal(data, &dest)
if er != nil {
return nil, errorx.NewFromString(fmt.Sprintf("'data' is not well json format,err: %s", er.Error()))
}
er = deleteParse(safe, dest, keys ...)
if er != nil {
return nil, errorx.NewFromString(fmt.Sprintf("get parse err: %s", er.Error()))
}
if vtype == MAP {
return dest, nil
} else {
return jc.Marshal(dest)
}
}
func (jc JsonCracker) generateRawJson(k string, v interface{}, keySrials ...string) []byte {
var iterator = make(map[string]interface{}, 0)
var empty = make(map[string]interface{}, 0)
for i := len(keySrials) - 1; i >= 0; i-- {
var tmp = CopyFrom(empty)
if i == len(keySrials)-1 {
iterator[k] = v
}
tmp[keySrials[i]] = CopyFrom(iterator)
iterator = CopyFrom(tmp)
if i == 0 {
buf, _ := jc.Marshal(tmp)
return buf
}
}
return nil
}
// copy a map to another
func CopyFrom(m map[string]interface{}) map[string]interface{} {
var m2 = make(map[string]interface{}, 0)
for k, v := range m {
m2[k] = v
}
return m2
}
// parse the 'target' typed map/struct via 'keys' and add/update 'val' into the target.
// while 'safe' is true, it throws error when the last key of keys exist, otherwise replace it if existed
func updateParse(safe bool, target interface{}, val interface{}, keys ...string) error {
return updateParseDetail(safe, reflect.ValueOf(&target), keys, reflect.ValueOf(val))
}
// detail of parse
func updateParseDetail(safe bool, target reflect.Value, sel []string, val reflect.Value) error {
if len(sel) == 0 {
return errorx.NewFromString("invalid sel")
}
v := reflect.Indirect(target)
//v := reflect.ValueOf(target)
L:
switch v.Kind() {
case reflect.Struct:
nam := strings.Title(sel[0])
if len(sel) == 1 {
f := v.FieldByName(nam)
if f.IsValid() {
if f.CanSet() {
f.Set(val)
return nil
}
}
} else {
f := v.FieldByName(nam)
return updateParseDetail(safe, f, sel[1:], val)
}
case reflect.Map:
// nam := reflect.ValueOf(strings.Title(sel[0]))
nam := reflect.ValueOf(sel[0])
if len(sel) == 1 {
//v.SetMapIndex(nam, val)
vvvv := v.Interface().(map[string]interface{})
if !safe {
vvvv[nam.String()] = val.Interface()
} else {
if _, ok := vvvv[nam.String()]; ok {
return errorx.NewFromString(fmt.Sprintf("key '%s' already exist", nam.String()))
}
}
//f := v.MapIndex(nam)
//if f.IsValid() {
// if f.CanSet() {
// f.Set(val)
// return nil
// } else {
// f = f.Elem()
// f.Set(val)
// return nil
// }
//} else {
// v.SetMapIndex(nam, val)
//}
} else {
f := v.MapIndex(nam)
return updateParseDetail(safe, f, sel[1:], val)
}
case reflect.Interface:
vv := v.Interface()
if vvv, ok := vv.(map[string]interface{}); ok {
v = reflect.ValueOf(vvv)
goto L
}
fallthrough
default:
return errorx.NewFromString("invalid type, must struct/map")
}
return nil
}
// get the value from 'target', typed map/struct, indexed by keys...
func getParse(result *interface{}, target interface{}, keys ...string) error {
return getParseDetail(result, reflect.ValueOf(&target), keys...)
}
// detail of getParse
func getParseDetail(result *interface{}, target reflect.Value, sel ...string) error {
v := reflect.Indirect(target)
if len(sel) == 0 {
return nil
}
L:
switch v.Kind() {
case reflect.Array, reflect.Slice:
nam := strings.Title(sel[0])
if len(sel) == 1 {
f := v.FieldByName(nam)
*result = f.Interface()
} else {
return errorx.NewFromString(fmt.Sprintf("key '%s' is a array/slice,however keys is not end", nam))
}
case reflect.Struct:
nam := strings.Title(sel[0])
if len(sel) == 1 {
f := v.FieldByName(nam)
*result = f.Interface()
} else {
f := v.FieldByName(nam)
return getParseDetail(result, f, sel[1:]...)
}
case reflect.Map:
// nam := reflect.ValueOf(strings.Title(sel[0]))
nam := reflect.ValueOf(sel[0])
if len(sel) == 1 {
*result = v.Interface().(map[string]interface{})[sel[0]]
} else {
f := v.MapIndex(nam)
return getParseDetail(result, f, sel[1:]...)
}
case reflect.Interface:
vv := v.Interface()
if vvv, ok := vv.(map[string]interface{}); ok {
_, ok2 := vvv[sel[0]]
if !ok2 {
return errorx.NewFromString(fmt.Sprintf("key '%s' not found", sel[0]))
}
v = reflect.ValueOf(vvv)
goto L
}
fallthrough
default:
return errorx.NewFromString("invalid type, must struct/map")
}
return nil
}
// delete a key-value pair of a target via keys indexing
func deleteParse(safe bool, target interface{}, keys ...string) error {
return deleteParseDetail(safe, reflect.ValueOf(&target), keys ...)
}
func deleteParseDetail(safe bool, target reflect.Value, keys ...string) error {
if len(keys) == 0 {
return errorx.NewFromString("invalid sel")
}
v := reflect.Indirect(target)
//v := reflect.ValueOf(target)
L:
switch v.Kind() {
case reflect.Map:
// nam := reflect.ValueOf(strings.Title(sel[0]))
nam := reflect.ValueOf(keys[0])
if len(keys) == 1 {
//v.SetMapIndex(nam, val)
vvvv := v.Interface().(map[string]interface{})
if !safe {
delete(vvvv, nam.String())
} else {
if _, ok := vvvv[nam.String()]; !ok {
return errorx.NewFromString(fmt.Sprintf("key '%s' not exist", nam.String()))
}
}
} else {
f := v.MapIndex(nam)
return deleteParseDetail(safe, f, keys[1:]...)
}
case reflect.Interface:
vv := v.Interface()
if vvv, ok := vv.(map[string]interface{}); ok {
v = reflect.ValueOf(vvv)
goto L
}
fallthrough
default:
return errorx.NewFromString("invalid type, must struct/map")
}
return nil
}