-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal.go
280 lines (250 loc) · 6.56 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
276
277
278
279
280
package jsonapi
import (
"encoding/json"
"fmt"
"math/big"
"reflect"
"strconv"
"strings"
)
// Unmarshal parses the JSON:API-encoded data and stores the result in the value pointed to by v.
func Unmarshal(data []byte, v interface{}) error {
rType := reflect.TypeOf(v)
rValue := reflect.ValueOf(v)
kind := rType.Kind()
// v must be pointer
if kind != reflect.Ptr {
return fmt.Errorf("v must be pointer")
}
// v must not be nil
if rValue.IsNil() {
return fmt.Errorf("v must not be nil")
}
// determine if v is a slice
isSlice := false
if rType.Elem().Kind() == reflect.Slice {
isSlice = true
}
// handle compound document
if isSlice {
document := NewCompoundDocument(nil)
if err := json.Unmarshal(data, document); err != nil {
return err
}
return unmarshalCompoundDocument(v, document)
}
// handle single document
document := NewDocument(nil)
if err := json.Unmarshal(data, document); err != nil {
return err
}
return unmarshalDocument(v, document)
}
// RegisterUnmarshaler register a new unmarshaler function for type t.
func RegisterUnmarshaler(t reflect.Type, u unmarshalerFunc) {
customUnmarshalers[t] = u
}
type unmarshalerFunc = func(interface{}, reflect.Value)
var customUnmarshalers = make(map[reflect.Type]unmarshalerFunc)
func unmarshalCompoundDocument(v interface{}, cd *CompoundDocument) error {
rValue := reflect.ValueOf(v)
for _, resource := range cd.Data {
v2 := reflect.New(rValue.Elem().Type().Elem()).Interface()
if err := iterateStruct(v2, func(value reflect.Value, memberType memberType, memberNames ...string) error {
fieldKind := value.Kind()
// TODO this sets ID for all nexted primary tag fields
if memberType == memberTypePrimary {
switch fieldKind {
case reflect.String:
value.SetString(resource.ID)
case reflect.Int:
intID, err := strconv.Atoi(resource.ID)
if err != nil {
return err
}
value.SetInt(int64(intID))
default:
return fmt.Errorf("ID must be a string or int")
}
return nil
}
// set raw value
return unmarshal(resource, memberType, memberNames, value)
}); err != nil {
return err
}
value := rValue.Elem()
value.Set(reflect.Append(value, reflect.ValueOf(v2).Elem()))
}
return nil
}
func unmarshalDocument(v interface{}, d *Document) error {
return iterateStruct(v, func(value reflect.Value, memberType memberType, memberNames ...string) error {
fieldKind := value.Kind()
// TODO this sets ID for all nexted primary tag fields
if memberType == memberTypePrimary {
switch fieldKind {
case reflect.String:
value.SetString(d.Data.ID)
case reflect.Int:
intID, err := strconv.Atoi(d.Data.ID)
if err != nil {
return err
}
value.SetInt(int64(intID))
default:
return fmt.Errorf("ID must be a string or int")
}
return nil
}
// set raw value
return unmarshal(d.Data, memberType, memberNames, value)
})
}
func unmarshal(resource *Resource, memberType memberType, memberNames []string, field reflect.Value) error {
// find raw value if exists
var search map[string]interface{}
switch memberType {
case memberTypeAttribute:
search = resource.Attributes
case memberTypeMeta:
search = resource.Meta
}
rawValue, found := deepSearch(search, memberNames...)
if !found {
return nil
}
if cu, ok := customUnmarshalers[field.Type()]; ok {
cu(rawValue, field)
return nil
}
// if pointer, get non-pointer kind
if field.Kind() == reflect.Ptr {
field.Set(reflect.New(field.Type().Elem()))
field = field.Elem()
}
value := reflect.Indirect(reflect.ValueOf(rawValue))
// set values by kind
switch field.Kind() {
case reflect.Bool:
return setBool(field, value)
case reflect.String:
return setString(field, value)
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
return setInt(field, value)
case reflect.Uint:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
fallthrough
case reflect.Uintptr:
return setUint(field, value)
case reflect.Float32:
fallthrough
case reflect.Float64:
return setFloat(field, value)
case reflect.Slice:
switch field.Type() {
case reflect.TypeOf([]string{}):
return setStringSlice(field, value)
case reflect.TypeOf([]int{}):
return setIntSlice(field, value)
}
}
return nil
}
func deepSearch(tree map[string]interface{}, keys ...string) (interface{}, bool) {
key, keys := keys[0], keys[1:]
value, ok := tree[key]
if !ok {
return nil, false
}
if len(keys) == 0 {
return value, true
}
return deepSearch(tree[key].(map[string]interface{}), keys...)
}
func setInt(field, value reflect.Value) error {
bf := new(big.Float)
float64Str := float64StringFromValue(value)
if _, err := fmt.Sscan(float64Str, bf); err != nil {
return err
}
i, _ := bf.Int64()
field.SetInt(i)
return nil
}
func setUint(field, value reflect.Value) error {
bf := new(big.Float)
float64Str := float64StringFromValue(value)
if _, err := fmt.Sscan(float64Str, bf); err != nil {
return err
}
ui, _ := bf.Uint64()
field.SetUint(ui)
return nil
}
func setFloat(field, value reflect.Value) error {
if _, ok := value.Interface().(float64); !ok {
return fmt.Errorf("number has no digits")
}
field.SetFloat(value.Float())
return nil
}
func setStringSlice(field, value reflect.Value) error {
arr := make([]string, value.Len(), value.Cap())
for i := 0; i < value.Len(); i++ {
strVal, ok := value.Index(i).Interface().(string)
if !ok {
return fmt.Errorf("value is not of type string")
}
arr[i] = strVal
}
field.Set(reflect.ValueOf(arr))
return nil
}
func setIntSlice(field, value reflect.Value) error {
arr := make([]int, value.Len(), value.Cap())
for i := 0; i < value.Len(); i++ {
floatVal, ok := value.Index(i).Interface().(float64)
if !ok {
return fmt.Errorf("value is not of type float64")
}
arr[i] = int(floatVal)
}
field.Set(reflect.ValueOf(arr))
return nil
}
func setBool(field, value reflect.Value) error {
if _, ok := value.Interface().(bool); !ok {
return fmt.Errorf("invalid value for field %s", field.Type().Name())
}
field.SetBool(value.Bool())
return nil
}
func setString(field, value reflect.Value) error {
if _, ok := value.Interface().(string); !ok {
return fmt.Errorf("invalid value for field %s", field.Type().Name())
}
field.SetString(value.String())
return nil
}
func float64StringFromValue(value reflect.Value) string {
s := fmt.Sprintf("%s", value)
s = strings.TrimPrefix(s, "%!s(float64=")
s = strings.TrimSuffix(s, ")")
return s
}