-
Notifications
You must be signed in to change notification settings - Fork 1
/
unmarshal.go
158 lines (146 loc) · 3.33 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
package encodingjs
import (
"fmt"
"reflect"
"github.com/robertkrimen/otto"
)
const maxInt = int64(^uint(0) >> 1)
// Unmarshal transfers the contents of d into result.
func Unmarshal(d otto.Value, result interface{}) error {
rv := reflect.ValueOf(result)
return unmarshal(d, rv)
}
func unmarshal(d otto.Value, rv reflect.Value) error {
//fmt.Println(d, "<-", rv, rv.Type().Name(), rv.CanSet())
if rv.Type().NumMethod() > 0 {
if u, ok := rv.Interface().(Unmarshaler); ok {
return u.UnmarshalJS(d)
}
}
if rv.CanAddr() && rv.Addr().Type().NumMethod() > 0 {
if u, ok := rv.Addr().Interface().(Unmarshaler); ok {
return u.UnmarshalJS(d)
}
}
switch rv.Kind() {
case reflect.Ptr:
return unmarshal(d, rv.Elem())
case reflect.Struct:
return unmarshalStruct(d, rv)
case reflect.Slice:
return unmarshalSlice(d, rv)
case reflect.Map:
return unmarshalMap(d, rv)
case reflect.String:
if !d.IsUndefined() {
rv.SetString(d.String())
}
case reflect.Int:
if !d.IsNumber() {
return &InvalidValueError{rv.Type().String(), d}
}
iv, err := d.ToInteger()
if err != nil {
return err
}
rv.SetInt(iv)
case reflect.Float64:
if !d.IsNumber() {
return &InvalidValueError{rv.Type().String(), d}
}
fv, err := d.ToFloat()
if err != nil {
return err
}
rv.SetFloat(fv)
case reflect.Float32:
if !d.IsNumber() {
return &InvalidValueError{rv.Type().String(), d}
}
fv, err := d.ToFloat()
if err != nil {
return err
}
rv.SetFloat(fv)
default:
return &UnsupportedTypeError{rtype: rv.Type()}
}
return nil
}
func unmarshalStruct(d otto.Value, rv reflect.Value) error {
rt := rv.Type()
if !d.IsObject() {
return ErrObjectExpected
}
for i := 0; i < rt.NumField(); i++ {
fname := rt.Field(i).Name
tname := rt.Field(i).Tag.Get("js")
if tname != "" {
fname = tname
}
if dv, err := d.Object().Get(fname); err == nil {
unmarshal(dv, rv.Field(i))
} else if err != nil {
return err
}
}
return nil
}
func unmarshalMap(d otto.Value, rv reflect.Value) error {
rt := rv.Type()
if !d.IsObject() {
return ErrObjectExpected
}
dobj := d.Object()
keyT := rt.Key()
valueT := rt.Elem()
rv.Set(reflect.MakeMap(rt))
for _, key := range dobj.Keys() {
kv := reflect.Indirect(reflect.New(keyT))
kv.SetString(key)
fv := reflect.Indirect(reflect.New(valueT))
dval, err := dobj.Get(key)
if err != nil {
return err
}
err = unmarshal(dval, fv)
if err != nil {
return err
}
rv.SetMapIndex(kv, fv)
}
return nil
}
func unmarshalSlice(d otto.Value, rv reflect.Value) error {
rt := rv.Type()
if d.Class() != "Array" {
return ErrArrayExpected
}
darr := d.Object()
dlens, err := darr.Get("length")
if err != nil {
return fmt.Errorf("could not get array length: %s", err)
}
dlen64, err := dlens.ToInteger()
if err != nil {
return fmt.Errorf("non numeric array length: %s", err)
}
if dlen64 > maxInt {
return fmt.Errorf("array length exceeded: %d", dlen64)
}
dlen := int(dlen64)
rslice := reflect.MakeSlice(rt, dlen, dlen)
for i := 0; i < dlen; i++ {
delem, err := darr.Get(fmt.Sprintf("%d", i))
if err != nil {
return err
}
unmarshal(delem, rslice.Index(i))
}
rv.Set(rslice)
return nil
}
// Unmarshaler is the interface implemented by objects that can unmarshal a JS representation of themselves.
type Unmarshaler interface {
UnmarshalJS(d otto.Value) error
}