-
Notifications
You must be signed in to change notification settings - Fork 1
/
deserialize.go
408 lines (370 loc) · 8.94 KB
/
deserialize.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package tahwil
import (
"fmt"
"reflect"
)
type UnmapperError struct {
text string
}
func (e *UnmapperError) Error() string {
return "tahwil.FromValue: " + e.text
}
// An InvalidUnmapperKindError describes an invalid argument passed to FromValue.
type InvalidUnmapperKindError struct {
Expected string
Kind string
}
func (e *InvalidUnmapperKindError) Error() string {
if e.Expected == "" {
return "tahwil.FromValue: unsupported kind (" + e.Kind + ")"
}
return "tahwil.FromValue: unexpected kind (expected: " + e.Expected + ", got: " + e.Kind + ")"
}
type valueUnmapper struct {
// refs contains pointers to reference values during deserialization
// can be used both forward and backward lookups
refs map[uint64]reflect.Value
// fieldTagCache holds type => json:<tag> => field
// e.g. if a struct Struct has a field that is called FieldName
// and it has a struct tag `json:"field_name", filedTagCache will hold
// [<Struct>]["field_name"]["FieldName"]
fieldTagCache map[reflect.Type]map[string]string
}
func newValueUnmapper() *valueUnmapper {
return &valueUnmapper{
refs: make(map[uint64]reflect.Value),
fieldTagCache: make(map[reflect.Type]map[string]string),
}
}
// fieldByTag returns field name for a given type and a tag name.
// If no tag is found, it will return the name of the field
func (vu *valueUnmapper) fieldByTag(t reflect.Type, key string) string {
if vu.fieldTagCache[t] != nil {
return vu.fieldTagCache[t][key]
}
vu.fieldTagCache[t] = make(map[string]string)
for i := 0; i < t.NumField(); i++ {
ft := t.Field(i)
k := ft.Tag.Get("json")
if k == "" {
k = ft.Name
}
if k == "-" || k == "_" {
continue
}
vu.fieldTagCache[t][k] = ft.Name
}
return vu.fieldTagCache[t][key]
}
func (vu *valueUnmapper) fromBoolValue(data *Value, v reflect.Value) error {
if v.Kind() != reflect.Bool {
return &InvalidUnmapperKindError{Expected: Bool, Kind: v.Kind().String()}
}
if fval, ok := data.Value.(bool); ok {
v.SetBool(fval)
return nil
}
return &InvalidValueError{
Value: data.Value,
Kind: data.Kind,
}
}
//nolint:dupl // false positive!
func (vu *valueUnmapper) fromIntValue(data *Value, v reflect.Value) error {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
switch vv := data.Value.(type) {
case int:
if !v.OverflowInt(int64(vv)) {
v.SetInt(int64(vv))
return nil
}
case int8:
if !v.OverflowInt(int64(vv)) {
v.SetInt(int64(vv))
return nil
}
case int16:
if !v.OverflowInt(int64(vv)) {
v.SetInt(int64(vv))
return nil
}
case int32:
if !v.OverflowInt(int64(vv)) {
v.SetInt(int64(vv))
return nil
}
case int64:
if !v.OverflowInt(vv) {
v.SetInt(vv)
return nil
}
}
return &InvalidValueError{
Value: data.Value,
Kind: data.Kind,
}
}
return &InvalidUnmapperKindError{Expected: "int|int8|int16|int32|int64", Kind: v.Kind().String()}
}
//nolint:dupl // false positive!
func (vu *valueUnmapper) fromUintValue(data *Value, v reflect.Value) error {
switch v.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
switch vv := data.Value.(type) {
case uint:
if !v.OverflowUint(uint64(vv)) {
v.SetUint(uint64(vv))
return nil
}
case uint8:
if !v.OverflowUint(uint64(vv)) {
v.SetUint(uint64(vv))
return nil
}
case uint16:
if !v.OverflowUint(uint64(vv)) {
v.SetUint(uint64(vv))
return nil
}
case uint32:
if !v.OverflowUint(uint64(vv)) {
v.SetUint(uint64(vv))
return nil
}
case uint64:
if !v.OverflowUint(vv) {
v.SetUint(vv)
return nil
}
}
return &InvalidValueError{
Value: data.Value,
Kind: data.Kind,
}
}
return &InvalidUnmapperKindError{Expected: "uint|uint8|uint16|uint32|uint64", Kind: v.Kind().String()}
}
func (vu *valueUnmapper) fromFloatValue(data *Value, v reflect.Value) error {
switch v.Kind() {
case reflect.Float32, reflect.Float64:
switch vv := data.Value.(type) {
case float32:
if !v.OverflowFloat(float64(vv)) {
v.SetFloat(float64(vv))
return nil
}
case float64:
if !v.OverflowFloat(vv) {
v.SetFloat(vv)
return nil
}
}
return &InvalidValueError{
Value: data.Value,
Kind: data.Kind,
}
}
return &InvalidUnmapperKindError{Expected: "float32|float64", Kind: v.Kind().String()}
}
func (vu *valueUnmapper) fromSliceValue(data *Value, v reflect.Value) error {
if data.Value == nil {
return nil
}
var sl reflect.Value
mi, ok := data.Value.([]any)
if ok {
sl = reflect.MakeSlice(v.Type(), len(mi), len(mi))
}
mv, ok := data.Value.([]*Value)
if ok {
sl = reflect.MakeSlice(v.Type(), len(mv), len(mv))
}
if mi == nil && mv == nil {
return &InvalidValueError{Value: data.Value, Kind: data.Kind}
}
v.Set(sl)
for i := 0; i < v.Len(); i++ {
var x *Value
el := v.Index(i)
if mv != nil {
x = mv[i]
} else {
x = mi[i].(*Value)
}
err := vu.fromValue(x, el)
if err != nil {
return nil
}
}
return nil
}
func (vu *valueUnmapper) fromMapValue(data *Value, v reflect.Value) error {
if data.Value == nil {
return nil
}
if v.Kind() != reflect.Map {
return &InvalidUnmapperKindError{Expected: Map, Kind: v.Kind().String()}
}
var keys []reflect.Value
mi, ok := data.Value.(map[string]any)
if ok {
rm := reflect.ValueOf(mi)
keys = rm.MapKeys()
}
mv, ok := data.Value.(map[string]*Value)
if ok {
rm := reflect.ValueOf(mv)
keys = rm.MapKeys()
}
if mi == nil && mv == nil {
return &InvalidValueError{Value: data.Value, Kind: data.Kind}
}
v.Set(reflect.MakeMap(v.Type()))
for _, key := range keys {
var x *Value
if mv != nil {
x = mv[key.String()]
} else {
x = mi[key.String()].(*Value)
}
f := reflect.New(v.Type().Elem()).Elem()
err := vu.fromValue(x, f)
if err != nil {
return nil
}
fmt.Println(key, f)
v.SetMapIndex(key, f)
fmt.Println(v)
}
return nil
}
func (vu *valueUnmapper) fromPtrValue(data *Value, v reflect.Value) error {
if data.Value == nil {
return nil
}
el := v.Elem()
if !el.IsValid() {
t := v.Type()
telm := t.Elem()
elm := reflect.New(telm)
v.Set(elm)
el = v.Elem()
}
x := data.Value.(*Value)
err := vu.fromValue(x, el)
if err != nil {
return err
}
return nil
}
func (vu *valueUnmapper) fromStringValue(data *Value, v reflect.Value) error {
if v.Kind() != reflect.String {
return &InvalidUnmapperKindError{Expected: String, Kind: v.Kind().String()}
}
if fval, ok := data.Value.(string); ok {
v.SetString(fval)
return nil
}
return &InvalidValueError{
Value: data.Value,
Kind: data.Kind,
}
}
func (vu *valueUnmapper) fromStructValue(data *Value, v reflect.Value) error {
if v.Kind() == reflect.Interface {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return &InvalidUnmapperKindError{Expected: Struct, Kind: v.Kind().String()}
}
var keys []reflect.Value
mi, ok := data.Value.(map[string]any)
if ok {
rm := reflect.ValueOf(mi)
keys = rm.MapKeys()
}
mv, ok := data.Value.(map[string]*Value)
if ok {
rm := reflect.ValueOf(mv)
keys = rm.MapKeys()
}
if mi == nil && mv == nil {
return &InvalidValueError{Value: data.Value, Kind: data.Kind}
}
for _, key := range keys {
tagName := key.String()
keyName := vu.fieldByTag(v.Type(), tagName)
if keyName == "" {
keyName = tagName
}
f := v.FieldByName(keyName)
var x *Value
if mv != nil {
x = mv[tagName]
} else {
x = mi[tagName].(*Value)
}
if f.IsValid() {
err := vu.fromValue(x, f)
if err != nil {
return err
}
}
}
return nil
}
func (vu *valueUnmapper) fromRefValue(data *Value, v reflect.Value) error {
ref := data.Value.(*Reference)
if refv, ok := vu.refs[ref.Refid]; ok {
v.Set(refv)
return nil
}
err := vu.fromValue(ref.Value, v)
if err != nil {
return err
}
return nil
}
// fills v with the values from data
func (vu *valueUnmapper) fromValue(data *Value, v reflect.Value) error {
vu.refs[data.Refid] = v
switch data.Kind {
case Bool:
return vu.fromBoolValue(data, v)
case Int, Int8, Int16, Int32, Int64:
return vu.fromIntValue(data, v)
case Uint, Uint8, Uint16, Uint32, Uint64:
return vu.fromUintValue(data, v)
case Float32, Float64:
return vu.fromFloatValue(data, v)
case /*Array, */ Slice: // TODO: how to deal with array?
return vu.fromSliceValue(data, v)
case Map:
return vu.fromMapValue(data, v)
case Ptr:
return vu.fromPtrValue(data, v)
case String:
return vu.fromStringValue(data, v)
case Struct:
return vu.fromStructValue(data, v)
case Ref:
return vu.fromRefValue(data, v)
}
return &InvalidUnmapperKindError{Kind: data.Kind}
}
func FromValue(data *Value, v any) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return &UnmapperError{text: "value must be non-nil Pointer"}
}
resolver := NewResolver(data)
vu := newValueUnmapper()
if err := resolver.Resolve(); err != nil {
return &UnmapperError{text: err.Error()}
}
if resolver.HasUnresolved() {
return &UnmapperError{text: "can't resolve all refs, invalid input"}
}
return vu.fromValue(data, rv)
}