-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
executable file
·130 lines (111 loc) · 2.66 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
package gjson
import (
"bytes"
"encoding/json"
"fmt"
"github.com/bitly/go-simplejson"
"github.com/davidforest123/goutil/basic/gerrors"
"github.com/tidwall/gjson"
"reflect"
"time"
)
type JsonValue gjson.Result
func Get(json, path string) JsonValue {
return JsonValue(gjson.Get(json, path))
}
func Set(jsonstr string, path []string, val interface{}) (string, error) {
js, err := simplejson.NewJson([]byte(jsonstr))
if err != nil {
return "", err
}
js.SetPath(path, val)
b, err := js.MarshalJSON()
return string(b), err
}
func (v JsonValue) Exists() bool {
return gjson.Result(v).Exists()
}
func (v JsonValue) String() string {
return gjson.Result(v).String()
}
func (v JsonValue) Time() time.Time {
return gjson.Result(v).Time()
}
func (v JsonValue) Bool() bool {
return gjson.Result(v).Bool()
}
func (v JsonValue) Float() float64 {
return gjson.Result(v).Float()
}
func (v JsonValue) Int64() int64 {
return int64(gjson.Result(v).Float())
}
func MarshalAndPrintln(x interface{}) {
buf, err := json.Marshal(x)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(buf))
}
type (
IterFn = func(key string, val interface{}) (newVal interface{}, modified bool, err error)
)
func Iterate(jsonStr *string, indent bool, iterFn IterFn) error {
if jsonStr == nil {
return gerrors.New("can't iterate nil jsonStr")
}
var jsonIF interface{}
if err := json.Unmarshal([]byte(*jsonStr), &jsonIF); err != nil {
return err
}
jsonMap := jsonIF.(map[string]interface{})
if err := iterateMap(&jsonMap, iterFn); err != nil {
return err
}
newJsonStr, err := MarshalString(jsonMap, indent)
if err != nil {
return err
}
*jsonStr = newJsonStr
return nil
}
func iterateMap(jsonMap *map[string]interface{}, iterFn IterFn) error {
for k, v := range *jsonMap {
if reflect.TypeOf(v) != nil && reflect.TypeOf(v).Kind() == reflect.Map {
vMap := v.(map[string]interface{})
if err := iterateMap(&vMap, iterFn); err != nil {
return err
}
} else if reflect.TypeOf(v) != nil && reflect.TypeOf(v).Kind() == reflect.Slice {
vSlice := v.([]interface{})
for _, vEntry := range vSlice {
if reflect.TypeOf(vEntry).Kind() == reflect.Map {
vMap := vEntry.(map[string]interface{})
if err := iterateMap(&vMap, iterFn); err != nil {
return err
}
}
}
} else {
newVal, modified, err := iterFn(k, v)
if err != nil {
return err
}
if modified {
(*jsonMap)[k] = newVal
}
}
}
return nil
}
func IsValid(json string) bool {
return gjson.Valid(json)
}
func FormatIndent(p []byte) ([]byte, error) {
var out bytes.Buffer
err := json.Indent(&out, p, "", "\t")
if err != nil {
return nil, err
}
return out.Bytes(), nil
}