-
Notifications
You must be signed in to change notification settings - Fork 3
/
unmarshal.go
275 lines (230 loc) · 6.3 KB
/
unmarshal.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
package jsonry
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"code.cloudfoundry.org/jsonry/internal/path"
"code.cloudfoundry.org/jsonry/internal/tree"
)
// Unmarshal parses the specified JSON into the specified Go struct receiver.
// The receiver must be a pointer to a Go struct containing only fields of the type:
// string, bool, int*, uint*, float*, map, slice or struct. JSONry is recursive.
//
// If a field implements the json.Unmarshaler interface, then the UnmarshalJSON() method will be called.
func Unmarshal(data []byte, receiver interface{}) error {
target := reflect.ValueOf(receiver)
if target.Kind() != reflect.Ptr {
return errors.New("receiver must be a pointer to a struct, got a non-pointer")
}
target = target.Elem()
if target.Kind() != reflect.Struct {
return fmt.Errorf("receiver must be a pointer to a struct type, got: %s", target.Type())
}
var source map[string]interface{}
d := json.NewDecoder(bytes.NewBuffer(data))
d.UseNumber()
if err := d.Decode(&source); err != nil {
return fmt.Errorf("error parsing JSON: %w", err)
}
return unmarshalIntoStruct(target, true, source)
}
func unmarshalIntoStruct(target reflect.Value, found bool, source interface{}) error {
if !found || source == nil {
return nil
}
src, ok := source.(map[string]interface{})
if !ok {
return newConversionError(source)
}
target = allocateIfNeeded(target)
for i := 0; i < target.NumField(); i++ {
field := target.Type().Field(i)
if public(field) {
p := path.ComputePath(field)
s, found := tree.Tree(src).Fetch(p)
if err := unmarshal(target.Field(i), found, s); err != nil {
return wrapErrorWithFieldContext(err, field.Name, field.Type)
}
}
}
return nil
}
func unmarshal(target reflect.Value, found bool, source interface{}) error {
kind := underlyingType(target).Kind()
var err error
switch {
case reflect.PointerTo(target.Type()).Implements(reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()):
err = unmarshalIntoJSONUnmarshaler(target, found, source)
case basicType(kind), kind == reflect.Interface:
err = unmarshalInfoLeaf(target, found, source)
case kind == reflect.Struct:
err = unmarshalIntoStruct(target, found, source)
case kind == reflect.Slice:
err = unmarshalIntoSlice(target, found, source)
case kind == reflect.Map:
err = unmarshalIntoMap(target, found, source)
default:
err = newUnsupportedTypeError(target.Type())
}
return err
}
func unmarshalInfoLeaf(target reflect.Value, found bool, source interface{}) error {
if !found {
return nil
}
switch target.Kind() {
case reflect.Ptr:
switch source {
case nil:
return setZeroValue(target)
default:
return unmarshalInfoLeaf(allocateIfNeeded(target), found, source)
}
case reflect.String:
switch s := source.(type) {
case string:
target.SetString(s)
return nil
case nil:
return nil
}
case reflect.Bool:
switch b := source.(type) {
case bool:
target.SetBool(b)
return nil
case nil:
return nil
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
switch n := source.(type) {
case json.Number:
if i, err := strconv.ParseInt(n.String(), 10, 64); err == nil {
target.SetInt(i)
return nil
}
case nil:
return nil
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
switch n := source.(type) {
case json.Number:
if i, err := strconv.ParseUint(n.String(), 10, 64); err == nil {
target.SetUint(i)
return nil
}
case nil:
return nil
}
case reflect.Float32, reflect.Float64:
switch n := source.(type) {
case json.Number:
if f, err := strconv.ParseFloat(n.String(), 64); err == nil {
target.SetFloat(f)
return nil
}
case nil:
return nil
}
case reflect.Interface:
switch source {
case nil:
return setZeroValue(target)
default:
target.Set(reflect.ValueOf(convertNumbers(source)))
}
return nil
}
return newConversionError(source)
}
func unmarshalIntoSlice(target reflect.Value, found bool, source interface{}) error {
if !found || source == nil {
return nil
}
src, ok := source.([]interface{})
if !ok {
return newConversionError(source)
}
slice := reflect.MakeSlice(underlyingType(target), len(src), len(src))
allocateIfNeeded(target).Set(slice)
for i := range src {
elem := slice.Index(i)
if err := unmarshal(elem, true, src[i]); err != nil {
return wrapErrorWithIndexContext(err, i, elem.Type())
}
}
return nil
}
func unmarshalIntoMap(target reflect.Value, found bool, source interface{}) error {
targetType := underlyingType(target)
if targetType.Key().Kind() != reflect.String {
return newUnsupportedKeyTypeError(targetType.Key())
}
if !found || source == nil {
return nil
}
src, ok := source.(map[string]interface{})
if !ok {
return newConversionError(source)
}
m := reflect.MakeMap(targetType)
allocateIfNeeded(target).Set(m)
for k, v := range src {
targetValue := reflect.New(targetType.Elem()).Elem()
if err := unmarshal(targetValue, true, v); err != nil {
return wrapErrorWithKeyContext(err, k, targetValue.Type())
}
m.SetMapIndex(reflect.ValueOf(k).Convert(targetType.Key()), targetValue)
}
return nil
}
func unmarshalIntoJSONUnmarshaler(target reflect.Value, found bool, source interface{}) error {
if !found {
return nil
}
json, err := json.Marshal(source)
if err != nil {
return fmt.Errorf("error creating JSON for UnmarshalJSON(): %w", err)
}
elem := reflect.New(target.Type())
s := elem.MethodByName("UnmarshalJSON").Call([]reflect.Value{reflect.ValueOf(json)})
if err := checkForError(s[0]); err != nil {
return newForeignError("error from UnmarshalJSON() call", err)
}
target.Set(elem.Elem())
return nil
}
func setZeroValue(target reflect.Value) error {
target.Set(reflect.Zero(target.Type()))
return nil
}
func allocateIfNeeded(target reflect.Value) reflect.Value {
if target.Kind() != reflect.Ptr {
return target
}
n := reflect.New(target.Type().Elem())
target.Set(n)
return n.Elem()
}
func convertNumbers(input interface{}) interface{} {
n, ok := input.(json.Number)
if !ok {
return input
}
if i, err := n.Int64(); err == nil {
return int(i)
}
if f, err := n.Float64(); err == nil {
return f
}
return n.String()
}
func underlyingType(v reflect.Value) reflect.Type {
if v.Kind() == reflect.Ptr {
return v.Type().Elem()
}
return v.Type()
}