-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpatch.go
74 lines (64 loc) · 1.88 KB
/
patch.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
package collectjs
import (
"bytes"
"fmt"
"github.com/tidwall/gjson"
"github.com/tkeel-io/collectjs/pkg/json/jsonparser"
)
func Get(raw []byte, path string) ([]byte, jsonparser.ValueType, error) {
keys := path2JSONPARSER(path)
if value, dataType, _, err := jsonparser.Get(raw, keys...); err == nil {
return warpValue(dataType, value), dataType, nil
}
path = path2GJSON(path)
ret := gjson.GetBytes(raw, path)
if gjson.Null == ret.Type {
return nil, jsonparser.NotExist, jsonparser.KeyPathNotFoundError
}
return []byte(ret.String()), convertType(ret), nil
}
func warpValue(dataType jsonparser.ValueType, value []byte) []byte {
switch dataType {
case jsonparser.String:
return bytes.Join([][]byte{
[]byte("\""), value, []byte("\""),
}, []byte{})
default:
return value
}
return nil
}
func Set(raw []byte, path string, value []byte) ([]byte, error) {
keys := path2JSONPARSER(path)
return jsonparser.Set(raw, value, keys...)
}
func Append(raw []byte, path string, value []byte) ([]byte, error) {
keys := path2JSONPARSER(path)
return jsonparser.Append(raw, value, keys...)
}
func Del(raw []byte, path ...string) []byte {
for _, v := range path {
keys := path2JSONPARSER(v)
raw = jsonparser.Delete(raw, keys...)
}
return raw
}
func ForEach(raw []byte, datatype jsonparser.ValueType, fn func(key []byte, value []byte, dataType jsonparser.ValueType)) []byte {
// dispose object.
if datatype == jsonparser.Object {
jsonparser.ObjectEach(raw, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
fn(key, warpValue(dataType, value), dataType)
return nil
})
}
// dispose array.
if datatype == jsonparser.Array {
idx := 0
jsonparser.ArrayEach(raw, func(value []byte, dataType jsonparser.ValueType, offset int) error {
fn(Byte(fmt.Sprintf("[%d]", idx)), warpValue(dataType, value), dataType)
idx++
return nil
})
}
return raw
}