-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
289 lines (281 loc) · 6.36 KB
/
json.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
package sieve
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"sort"
"strings"
)
type H map[string]interface{}
type MarshalerJSON interface {
MarshalSieveJSON(opts Options) ([]byte, error)
}
func marshalJSON(v interface{}, s *sieve) ([]byte, error) {
if s == nil {
return json.Marshal(v)
}
t := reflect.TypeOf(v)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
k := t.Kind()
if k == reflect.Slice || k == reflect.Array {
empty, buff := true, bytes.Buffer{}
buff.WriteByte('[')
if val := reflect.Indirect(reflect.ValueOf(v)); !val.IsNil() && val.IsValid() {
for i := 0; i < val.Len(); i++ {
b, err := marshalJSON(val.Index(i).Interface(), s)
if err != nil {
return nil, err
}
if b != nil && len(b) > 0 {
if !empty {
buff.WriteByte(',')
} else {
empty = false
}
buff.Write(b)
}
}
}
buff.WriteByte(']')
return buff.Bytes(), nil
}
if i, ok := v.(MarshalerJSON); ok {
if i == nil {
return nil, nil
}
return i.MarshalSieveJSON(BuildOptions(s.scopes, nil))
}
if _, ok := v.(json.Marshaler); ok {
return json.Marshal(v)
}
if k == reflect.Struct {
obj, err := convertValueToMap(reflect.Indirect(reflect.ValueOf(v)), s, nil)
if err != nil {
return nil, err
}
return json.Marshal(obj)
}
if k == reflect.Map {
obj, err := bustValueMap(reflect.Indirect(reflect.ValueOf(v)), s, nil)
if err != nil {
return nil, err
}
return json.Marshal(obj)
}
return json.Marshal(v)
}
func bustValue(val reflect.Value, s *sieve, exportKeys []string) (interface{}, error) {
if !val.IsValid() {
return nil, nil
}
if !val.CanInterface() {
return nil, nil
}
if s == nil {
return val.Interface(), nil
}
if i, ok := val.Interface().(json.Marshaler); ok {
return i, nil
}
if i, ok := val.Interface().(MarshalerJSON); ok {
b, err := i.MarshalSieveJSON(BuildOptions(s.scopes, exportKeys))
if err != nil {
return nil, err
}
return json.RawMessage(b), nil
}
kind := val.Kind()
if kind == reflect.Interface {
val = reflect.Indirect(val.Elem())
if !val.IsValid() {
return nil, nil
}
if !val.CanInterface() {
return nil, nil
}
kind = val.Kind()
}
if kind == reflect.Array || kind == reflect.Slice {
return bustValueSlice(val, s, exportKeys)
}
if kind == reflect.Map {
return bustValueMap(val, s, exportKeys)
}
if kind == reflect.Struct {
return convertValueToMap(val, s, exportKeys)
}
return val.Interface(), nil
}
func bustValueSlice(val reflect.Value, s *sieve, exportKeys []string) (interface{}, error) {
if !val.IsValid() {
return nil, nil
}
if !val.CanInterface() {
return nil, nil
}
if s == nil || val.Len() == 0 {
return val.Interface(), nil
}
if exportKeys != nil && len(exportKeys) > 0 {
list := make([]interface{}, 0)
for index := 0; index < val.Len(); index++ {
i, err := bustValue(reflect.Indirect(val.Index(index)), s, exportKeys)
if err != nil {
return nil, err
}
if i != nil {
list = append(list, i)
}
}
return list, nil
}
list := make([]interface{}, val.Len(), val.Len())
for index := 0; index < val.Len(); index++ {
item, err := bustValue(reflect.Indirect(val.Index(index)), s, exportKeys)
if err != nil {
return nil, err
}
list[index] = item
}
return list, nil
}
func bustValueMap(val reflect.Value, s *sieve, exportKeys []string) (interface{}, error) {
if !val.IsValid() {
return nil, nil
}
if !val.CanInterface() {
return nil, nil
}
if s == nil || val.Len() == 0 {
return val.Interface(), nil
}
m, exporting, oneKey := make(H), len(exportKeys) > 0, len(exportKeys) == 1
for _, key := range val.MapKeys() {
if !key.IsValid() {
continue
}
if !key.CanInterface() {
continue
}
keyStr := strings.TrimSpace(fmt.Sprint(key.Interface()))
if len(keyStr) < 1 {
continue
}
if exporting {
idx := sort.SearchStrings(exportKeys, keyStr)
if idx < 0 || idx >= len(exportKeys) {
continue
}
if exportKeys[idx] != keyStr {
continue
}
if oneKey {
return bustValue(val.MapIndex(key), s, nil)
}
}
obj, err := bustValue(val.MapIndex(key), s, nil)
if err != nil {
return nil, err
}
m[keyStr] = obj
}
return m, nil
}
func convertValueToMap(val reflect.Value, s *sieve, exportKeys []string) (interface{}, error) {
if !val.IsValid() {
return nil, nil
}
if !val.CanInterface() {
return nil, nil
}
if s == nil || !val.IsValid() {
return val.Interface(), nil
}
t, exporting := val.Type(), false
if count := len(exportKeys); count > 0 {
exporting = true
if count == 1 {
if s, ok := t.FieldByName(exportKeys[0]); ok && !s.Anonymous {
if c := val.FieldByName(exportKeys[0]); c.IsValid() && c.CanInterface() {
return reflect.Indirect(c).Interface(), nil
}
}
return nil, nil
}
}
m := make(H)
for index := 0; index < t.NumField(); index++ {
field := t.Field(index)
if field.Anonymous {
if field.Type.Kind() == reflect.Struct {
if sub, err := convertValueToMap(val.Field(index), s, exportKeys); err == nil && sub != nil {
// Merge map and sub map
if subM, ok := sub.(H); ok {
for subK, subV := range subM {
m[subK] = subV
}
}
}
}
continue
}
if exporting {
idx := sort.SearchStrings(exportKeys, field.Name)
if idx < 0 || idx >= len(exportKeys) {
continue
}
if exportKeys[idx] != field.Name {
continue
}
}
fieldName, omitempty := field.Name, false
if tag, ok := field.Tag.Lookup("json"); ok {
if idx := strings.Index(tag, ","); idx != -1 {
omitempty = strings.Contains(tag[idx+1:], "omitempty")
if name := strings.TrimSpace(tag[:idx]); len(name) > 0 {
tag = name
}
}
if tag = strings.TrimSpace(tag); len(tag) > 0 {
fieldName = tag
}
}
if len(fieldName) < 1 || fieldName == "-" {
continue
}
opts := parseTag(field.Tag.Get("sieve"))
if opts.HasScopes() {
if !s.HasAnyScope(opts.Scopes()...) {
continue
}
}
fieldValue := reflect.Indirect(val.Field(index))
if !fieldValue.IsValid() {
continue
}
if !fieldValue.CanInterface() {
continue
}
if omitempty && isEmptyValue(fieldValue) {
continue
}
if opts.HasExclusions() {
if opts.CheckByExclusions(fieldValue, val) {
continue
}
}
ns := s
if opts.HasNextScopes() {
ns = &sieve{v: s.v, scopes: opts.NextScopes()}
}
obj, err := bustValue(fieldValue, ns, opts.ExportKeys())
if err != nil {
continue
}
m[fieldName] = obj
}
return m, nil
}