forked from thegrumpylion/jsref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal.go
177 lines (169 loc) · 3.15 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
package jsref
import (
"fmt"
"reflect"
"syscall/js"
"time"
)
// Unmarshal from js.Value
func Unmarshal(i interface{}, val js.Value) error {
v := reflect.ValueOf(i)
return unmarshal(v, val)
}
func unmarshal(v reflect.Value, val js.Value) error {
t := v.Type()
switch {
case isScalar(t):
return unmarshalScalar(v, val)
case isArray(t):
return unmarshalArr(v, val)
case isMap(t):
return unmarshalMap(v, val)
case isStruct(t):
return unmarshalStruct(v, val)
default:
return fmt.Errorf("unknown type: %s", t.String())
}
}
func unmarshalStruct(v reflect.Value, val js.Value) error {
if !IsValid(val) {
return nil
}
t := v.Type()
if isPtr(t) {
if v.IsNil() {
v.Set(reflect.New(t.Elem()))
}
v = v.Elem()
t = t.Elem()
}
// Handle time.Time as a special case
switch v.Interface().(type) {
case time.Time:
timeVal, err := time.Parse(time.RFC3339, val.String())
if err != nil {
return err
}
v.Set(reflect.ValueOf(timeVal))
return nil
}
if !IsObject(val) {
return fmt.Errorf("val must be object for struct. val=%v", val)
}
for i := 0; i < t.NumField(); i++ {
fld := t.Field(i)
fv := v.Field(i)
ft := fld.Type
name := fld.Name
tag := parseTag(fld.Tag)
if tag.ignore {
continue
}
if tag.name != "" {
name = tag.name
}
jsVal := val.Get(name)
if !IsValid(jsVal) {
continue
}
if isPtr(ft) {
if fv.IsNil() {
fv.Set(reflect.New(ft.Elem()))
}
}
err := unmarshal(fv, jsVal)
if err != nil {
return err
}
}
return nil
}
func unmarshalMap(v reflect.Value, val js.Value) error {
if !IsValid(val) {
return nil
}
if !IsObject(val) {
return fmt.Errorf("val must be object for map")
}
t := v.Type()
if isPtr(t) {
if v.IsNil() {
v.Set(reflect.New(t.Elem()))
}
v = v.Elem()
t = t.Elem()
}
et := t.Elem()
m := reflect.MakeMap(t)
for _, key := range ObjectKeys(val) {
goVal := reflect.New(et).Elem()
jsVal := val.Get(key)
err := unmarshal(goVal, jsVal)
if err != nil {
return err
}
m.SetMapIndex(reflect.ValueOf(key), goVal)
}
v.Set(m)
return nil
}
func unmarshalArr(v reflect.Value, val js.Value) error {
if !IsValid(val) {
return nil
}
if !IsArray(val) {
return fmt.Errorf("val must be array")
}
t := v.Type()
if isPtr(t) {
if v.IsNil() {
v.Set(reflect.New(t.Elem()))
}
t = t.Elem()
v = v.Elem()
}
slc := reflect.MakeSlice(t, 0, val.Length())
te := t.Elem()
for i := 0; i < val.Length(); i++ {
jsVal := val.Index(i)
goVal := reflect.New(te).Elem()
err := unmarshal(goVal, jsVal)
if err != nil {
return err
}
slc = reflect.Append(slc, goVal)
}
v.Set(slc)
return nil
}
func unmarshalScalar(v reflect.Value, val js.Value) error {
if !IsValid(val) {
return nil
}
t := v.Type()
if isPtr(t) {
if v.IsNil() {
v.Set(reflect.New(t.Elem()))
}
v = v.Elem()
}
// Skip unexported fields
if !v.CanInterface() {
return nil
}
switch {
case isBool(t):
v.SetBool(val.Bool())
case isString(t):
v.SetString(val.String())
case isInt(t):
v.SetInt(int64(val.Int()))
case isUint(t):
v.SetUint(uint64(val.Int()))
case isFloat(t):
v.SetFloat(val.Float())
default:
return fmt.Errorf("unknown type: %s", t.String())
}
return nil
}