-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode.go
249 lines (231 loc) · 5.7 KB
/
decode.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
package toml
import (
"runtime"
"reflect"
"strings"
"time"
"fmt"
)
var timeType = reflect.TypeOf(time.Time{})
func Unmarshal(data string, v interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
if _, ok := r.(runtime.Error); ok {
panic(r)
}
err = r.(error)
}
}()
tree, e := Parse(data)
if e != nil { return e }
d := &decode{}
rv := reflect.Indirect(reflect.ValueOf(v))
if rv.Kind() == reflect.Interface && rv.NumMethod() == 0 {
// Decoding into nil interface.
newv := reflect.ValueOf(make(map[string]interface{}))
d.top(newv, tree.Root)
rv.Set(newv)
} else {
d.top(reflect.Indirect(reflect.ValueOf(v)), tree.Root)
}
return
}
// An UnmarshalTypeError describes a JSON value that was
// not appropriate for a value of a specific Go type.
type UnmarshalTypeError struct {
Value string // description of JSON value - "bool", "array", "number -5"
Type reflect.Type // type of Go value it could not be assigned to
}
func (e *UnmarshalTypeError) Error() string {
return "toml: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
}
type decode struct {
node Node // current node
}
// error aborts the decoding by panicking with err.
func (d *decode) error(arg interface{}) {
panic(arg)
}
// error aborts the decoding by panicking with err.
func (d *decode) errorf(format string, args ...interface{}) {
panic(fmt.Errorf(format, args...))
}
func (d *decode) top(v reflect.Value, node *ListNode) {
for _, node := range node.Nodes {
switch node := node.(type) {
case *EntryGroupNode:
for _, key := range node.KeyGroup.StringKeys() {
var ok bool
v, ok = d.findField("keygroup", v, key)
if !ok {
return
}
}
for _, node := range node.Entries.Nodes {
d.entry(v, node.(*EntryNode))
}
case *EntryNode:
d.entry(v, node)
}
}
}
func (d *decode) findField(context string, v reflect.Value, key string) (next reflect.Value, ok bool) {
// Check type of target: struct or map[string]T
switch v.Kind() {
case reflect.Map:
t := v.Type()
if t.Key().Kind() != reflect.String {
d.error(&UnmarshalTypeError{context, v.Type()})
}
// init map
if v.IsNil() {
v.Set(reflect.MakeMap(v.Type()))
}
case reflect.Struct:
// continue.
default:
d.error(&UnmarshalTypeError{context, v.Type()})
}
// Map. for entry only.
if v.Kind() == reflect.Map {
if context == "keygroup" {
next = reflect.ValueOf(make(map[string]interface{}))
v.SetMapIndex(reflect.ValueOf(key), next)
return next, true
} else { // entry
next = reflect.New(v.Type().Elem()).Elem()
return next, true
}
}
// Struct
t := v.Type()
for i := 0; i < t.NumField(); i ++ {
tf := t.Field(i)
name := tf.Tag.Get("toml")
if name == "" {
name = tf.Name
}
if name == key || strings.EqualFold(name, key) {
f := v.Field(i)
if !f.CanSet() {
continue
}
return f, true
}
}
// can't find the field
return reflect.ValueOf(nil), false
}
func (d *decode) entry(v reflect.Value, node *EntryNode) {
key := node.Key.Key
f, ok := d.findField("entry", v, key)
if !ok {
return
}
d.value(f, node.Value)
// Write to map, if using struct, f points into struct already.
if v.Kind() == reflect.Map {
v.SetMapIndex(reflect.ValueOf(key), f)
}
}
func (d *decode) value(v reflect.Value, node Node) {
switch n := node.(type) {
case *BoolNode:
value := n.True
switch v.Kind() {
case reflect.Bool:
v.SetBool(value)
case reflect.Interface:
if v.NumMethod() == 0 {
v.Set(reflect.ValueOf(value))
} else {
d.error(&UnmarshalTypeError{"bool", v.Type()})
}
default:
d.error(&UnmarshalTypeError{"bool", v.Type()})
}
case *StringNode:
value := n.Text
switch v.Kind() {
case reflect.String:
v.SetString(value)
case reflect.Interface:
if v.NumMethod() == 0 {
v.Set(reflect.ValueOf(value))
} else {
d.error(&UnmarshalTypeError{"string", v.Type()})
}
default:
d.error(&UnmarshalTypeError{"string", v.Type()})
}
case *NumberNode:
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if !n.IsInt {
d.error(&UnmarshalTypeError{"int", v.Type()})
}
v.SetInt(n.Int)
case reflect.Float32, reflect.Float64:
if !n.IsFloat {
d.error(&UnmarshalTypeError{"float", v.Type()})
}
v.SetFloat(n.Float)
case reflect.Interface:
if v.NumMethod() == 0 {
if n.IsInt {
v.Set(reflect.ValueOf(n.Int))
pd("int %s %p", v, v)
} else {
v.Set(reflect.ValueOf(n.Float))
}
} else {
d.error(&UnmarshalTypeError{"number", v.Type()})
}
default:
d.error(&UnmarshalTypeError{"number", v.Type()})
}
case *DatetimeNode:
value := reflect.ValueOf(n.Time)
switch k := v.Kind(); {
case k == reflect.Struct && v.Type() == timeType:
v.Set(value)
case k == reflect.Interface:
if v.NumMethod() == 0 {
v.Set(value)
} else {
d.error(&UnmarshalTypeError{"datetime", v.Type()})
}
default:
d.error(&UnmarshalTypeError{"datetime", v.Type()})
}
case *ArrayNode:
switch v.Kind() {
case reflect.Interface:
l := len(n.Array.Nodes)
if v.NumMethod() == 0 {
newv := reflect.ValueOf(make([]interface{}, l))
d.value(newv, n)
v.Set(newv)
} else {
d.error(&UnmarshalTypeError{"array", v.Type()})
}
case reflect.Array, reflect.Slice:
l := len(n.Array.Nodes)
if v.Len() < l {
if v.Kind() == reflect.Array {
d.errorf("run out of fixed array, len is %d, want %d -- %s", v.Len(), l, n)
}
// Growing slice
newv := reflect.MakeSlice(v.Type(), l, l)
reflect.Copy(newv, v)
v.Set(newv)
v.SetLen(l)
}
for i, subn := range n.Array.Nodes {
d.value(v.Index(i), subn)
}
default:
d.error(&UnmarshalTypeError{"array", v.Type()})
}
}
}