This repository has been archived by the owner on Nov 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmodtracker.go
315 lines (294 loc) · 8.65 KB
/
modtracker.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
//Copyright 2016 Capital One Services, LLC
//
// SPDX-License-Identifier: Apache-2.0
// SPDX-Copyright: Copyright (c) Capital One Services, LLC
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and limitations under the License.
package modtracker
import (
"bytes"
"encoding/json"
"fmt"
"github.com/buger/jsonparser"
"github.com/pkg/errors"
"io"
"reflect"
"strings"
)
// Modifiable is implemented by struct types that contain a list of their fields that were populated from JSON.
// If a value for a field, even null, was provided in the JSON, the name of the field appears in the slice of strings.
type Modifiable interface {
GetModified() []string
}
// An Unmarshaler takes in JSON in the first parameter, a pointer to a struct in the second parameter, populates the
// struct with the JSON and returns the modified fields as a slice of strings. In case of error, the struct might be
// partially populated. If there is an error, the modified field slice will be nil.
type Unmarshaler func([]byte, interface{}) ([]string, error)
// UnmarshalJSON provides the default implementation of the Unmarshaler type. It will rediscover the fields in the structure
// each time it is called; to improve performance, use BuildJSONUnmarshaler to create an Unmarshaler instance with the
// struct fields pre-calculated.
func UnmarshalJSON(data []byte, s interface{}) ([]string, error) {
fm, err := buildJSONFieldMap(s)
if err != nil {
return nil, errors.Wrap(err, "Failure during UnmarshalJSON")
}
return unmarshalJSONInner(fm, data, s)
}
// BuildJSONUnmarshaler generates a custom implementation of the Unmarshaler type for the type of the provided struct.
// The preferred way to use BuildJSONUnmarshaler is to create a package-level variable and assign it in init with a
// nil instance of the type:
//
// type Sample struct {
// FirstName *string
// LastName *string
// Age *int
// Inner *struct {
// Address string
// }
// Pet string
// Company string `json:"company"`
// modified []string
// }
//
// var sampleUnmarshaler modtracker.Unmarshaler
//
// func init() {
// var err error
// sampleUnmarshaler, err = modtracker.BuildJSONUnmarshaler((*Sample)(nil))
// if err != nil {
// panic(err)
// }
// }
//
// func (s *Sample) UnmarshalJSON(data []byte) error {
// modified, err := sampleUnmarshaler(data, s)
// if err != nil {
// return err
// }
// s.modified = modified
// return nil
// }
//
func BuildJSONUnmarshaler(s interface{}) (func([]byte, interface{}) ([]string, error), error) {
fm, err := buildJSONFieldMap(s)
if err != nil {
return nil, errors.Wrap(err, "Failure during UnmarshalJSON")
}
return func(data []byte, s interface{}) ([]string, error) {
return unmarshalJSONInner(fm, data, s)
}, nil
}
type errorList []error
func (el errorList) innerErr(verb rune, plusFlag bool) string {
var b bytes.Buffer
b.WriteString(fmt.Sprintf("%d Errors found:\n", len(el)))
for _, v := range el {
switch verb {
case 'v':
if plusFlag {
b.WriteString(fmt.Sprintf("%+v\n", v))
} else {
b.WriteString(fmt.Sprintf("%v\n", v))
}
case 's':
b.WriteString(fmt.Sprintf("%s\n", v))
}
}
return b.String()
}
func (el errorList) Error() string {
return el.innerErr('s', false)
}
func (el errorList) Format(s fmt.State, verb rune) {
msg := el.innerErr(verb, s.Flag('+'))
io.WriteString(s, msg)
}
func validateType(nt reflect.Type, typeKind reflect.Kind, n string, validKind reflect.Kind, jsonType string) error {
if typeKind != validKind {
return errors.Errorf("Invalid type in JSON, expected %s for field %s, got %s", nt, n, jsonType)
}
return nil
}
var (
unmarshalerType = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()
)
func unmarshalJSONInner(fm fieldMap, data []byte, s interface{}) ([]string, error) {
modified := make([]string, 0, len(fm.names))
var el errorList
se := reflect.ValueOf(s).Elem()
jsonparser.EachKey(data, func(idx int, value []byte, vt jsonparser.ValueType, err error) {
var fv reflect.Value
fValue := fm.values[idx]
t := fValue.t
n := fValue.name
fv = reflect.New(fValue.internalType)
switch vt {
case jsonparser.String:
if fValue.unmarshaler {
b := make([]byte, len(value)+2)
b[0] = 34
b[len(b)-1] = 34
copy(b[1:], value)
err = json.Unmarshal(b, fv.Interface())
if err != nil {
el = append(el, errors.Wrap(err, "JSON unmarshaling"))
return
}
} else {
err := validateType(fValue.internalType, fValue.internalKind, n, reflect.String, "String")
if err != nil {
el = append(el, err)
return
}
s, _ := jsonparser.ParseString(value)
fv.Elem().SetString(s)
}
case jsonparser.Number:
switch {
case fValue.intType:
i, _ := jsonparser.ParseInt(value)
fv.Elem().SetInt(i)
case fValue.uintType:
i, _ := jsonparser.ParseInt(value)
fv.Elem().SetUint(uint64(i))
case fValue.floatType:
f, _ := jsonparser.ParseFloat(value)
fv.Elem().SetFloat(f)
default:
el = append(el, errors.Errorf("Invalid type in JSON, expected %s for field %s, got Number", fValue.internalType, n))
return
}
case jsonparser.Object, jsonparser.Array:
err = json.Unmarshal(value, fv.Interface())
if err != nil {
el = append(el, errors.Wrap(err, "JSON unmarshaling"))
return
}
case jsonparser.Boolean:
err := validateType(fValue.internalType, fValue.internalKind, n, reflect.Bool, "Boolean")
if err != nil {
el = append(el, err)
return
}
b, _ := jsonparser.ParseBoolean(value)
fv.Elem().SetBool(b)
case jsonparser.Null:
if fValue.pointerType {
fv = reflect.Zero(t)
} else {
el = append(el, errors.Errorf("Invalid type in JSON, cannot assign null to field %s", n))
return
}
default:
el = append(el, (errors.Errorf("Unexpected jsonparser value type %d", vt)))
return
}
target := se.FieldByName(n)
switch fValue.kind {
case reflect.Ptr:
target.Set(fv)
case reflect.Slice, reflect.Map:
if vt == jsonparser.Null {
target.Set(fv)
} else {
target.Set(fv.Elem())
}
default:
target.Set(fv.Elem())
}
modified = append(modified, n)
}, fm.names...)
if el == nil {
return modified, nil
}
return nil, el
}
type fieldMap struct {
names [][]string
values []fieldValue
}
type fieldValue struct {
kind reflect.Kind
internalType reflect.Type
internalKind reflect.Kind
t reflect.Type //type in struct
name string //name in struct
pointerType bool
unmarshaler bool
intType bool
uintType bool
floatType bool
}
func buildJSONFieldMap(s interface{}) (fieldMap, error) {
st := reflect.TypeOf(s)
if st.Kind() != reflect.Ptr {
return fieldMap{}, errors.New("Only works on pointers to structs")
}
stInner := st.Elem()
if stInner.Kind() != reflect.Struct {
return fieldMap{}, errors.New("Only works on pointers to structs")
}
out := fieldMap{}
out.names = make([][]string, stInner.NumField())
out.values = make([]fieldValue, stInner.NumField())
for i := 0; i < stInner.NumField(); i++ {
sf := stInner.Field(i)
//skip over any chan fields or func fields
if sf.Type.Kind() == reflect.Func || sf.Type.Kind() == reflect.Chan {
continue
}
var fieldName string
if name := sf.Tag.Get("json"); len(name) > 0 {
fieldName = strings.Split(name, ",")[0]
}
if fieldName == "-" {
continue
}
if fieldName == "" {
fieldName = sf.Name
}
t := sf.Type
k := t.Kind()
it := t
if k == reflect.Ptr {
it = t.Elem()
}
itk := it.Kind()
um := (t.Implements(unmarshalerType) || reflect.PtrTo(t).Implements(unmarshalerType))
pt := t.Kind() == reflect.Slice || t.Kind() == reflect.Map || t.Kind() == reflect.Ptr
intType := false
uintType := false
floatType := false
switch itk {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
intType = true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
uintType = true
case reflect.Float32, reflect.Float64:
floatType = true
}
out.names[i] = []string{fieldName}
out.values[i] = fieldValue{
t: t,
name: sf.Name,
kind: k,
internalType: it,
unmarshaler: um,
internalKind: itk,
pointerType: pt,
intType: intType,
uintType: uintType,
floatType: floatType,
}
}
return out, nil
}