-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode.go
223 lines (214 loc) · 6.41 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
package config
import (
"fmt"
"reflect"
"time"
)
// Decode initializes structPtr with contents of given section. Function does
// not support circular types; it will loop forever.
func (c *Configuration) Decode(section string, structPtr interface{}) (err error) {
if c != nil {
if c.IsSection(section) == false {
return fmt.Errorf("'%s': unknown section", section)
}
if structPtr == nil {
return fmt.Errorf("structure argument cannot be a nil value")
}
structPtrType := reflect.TypeOf(structPtr)
if structPtrType.Kind() != ptrType {
return fmt.Errorf("structure argument is not a pointer")
}
structPtrVal := reflect.ValueOf(structPtr)
if structPtrVal.IsNil() {
return fmt.Errorf("structure argument cannot be a nil pointer")
}
if structPtrVal.Elem().Kind() != structType {
return fmt.Errorf("structure argument is not a pointer to a structure")
}
err = c.doDecode(section, structPtrVal.Elem(), structPtrType.Elem())
}
return err
}
func (c *Configuration) doDecode(section string, val reflect.Value, typ reflect.Type) (err error) {
sVal := val
sType := typ
numFields := sVal.NumField()
// For each field of destination structure...
for f := 0; f < numFields; f++ {
fieldVal := sVal.Field(f)
fieldType := sType.Field(f)
// Embedded field?
if fieldType.Anonymous {
if fieldType.Type.Kind() == structType {
if err := c.doDecode(section, fieldVal, fieldType.Type); err != nil {
return err
}
} else {
return fmt.Errorf(
"'%s': embedded pointer fields are not supported yet",
fieldType.Name)
}
// My failed attempt to support embedded pointer fields. For next
// release I guess..
/*
} else {
structPtrType := fieldType.Type
if structPtrType.Kind() == ptrType {
structPtrVal := reflect.ValueOf(fieldType.Type)
if structPtrVal.IsNil() == false {
if structPtrVal.Elem().Kind() == structType {
err := c.doDecode(section, structPtrVal.Elem(), structPtrType.Elem())
if err != nil {
return err
}
}
}
}
}
*/
} else {
// Build option's path from section and either the StructTag or
// the field name. See http://golang.org/pkg/reflect/#StructTag.
tag := fieldType.Tag.Get("option")
if tag == "" {
tag = fieldType.Name
}
path := buildOptionPath(section, tag)
// Path corresponds to an existing option?
if src := c.getOption(path); src != nil {
if fieldVal.IsValid() == false {
return fmt.Errorf("'%s': cannot set field's value",
fieldType.Name)
}
if fieldVal.CanSet() == false {
return fmt.Errorf("'%s': cannot set value of unexported struct field",
fieldType.Name)
}
if fieldVal.Type().Kind() == sliceType {
eltType := fieldType.Type.Elem()
if err := c.decodeSlice(path, src, fieldVal, eltType); err != nil {
return err
}
} else {
if err := c.decodeValue(path, src, fieldVal); err != nil {
return err
}
}
}
}
}
return nil
}
func (c *Configuration) decodeSlice(path string, src *configurationValue,
dst reflect.Value, eltType reflect.Type) error {
srcVal := reflect.ValueOf(src.value)
if eltType == dateType {
if src.ctype != _ArrayType|_DateType {
return fmt.Errorf(
"'%s': value of type %s is not assignable to type []time.Time",
path, src.ctype)
}
a := []time.Time{}
for i := 0; i < srcVal.Len(); i++ {
a = append(a, srcVal.Index(i).Interface().(time.Time))
}
dst.Set(reflect.ValueOf(a))
} else {
switch eltType.Kind() {
case boolType:
if src.ctype != _ArrayType|_BoolType {
return fmt.Errorf("'%s': value of type %s is not assignable to type []bool",
path, src.ctype)
}
a := []bool{}
for i := 0; i < srcVal.Len(); i++ {
a = append(a, reflect.ValueOf(srcVal.Index(i).Interface()).Bool())
}
dst.Set(reflect.ValueOf(a))
case intType:
if src.ctype != _ArrayType|_IntType {
return fmt.Errorf("'%s': value of type %s is not assignable to type []int64", path,
src.ctype)
}
a := []int64{}
for i := 0; i < srcVal.Len(); i++ {
a = append(a, reflect.ValueOf(srcVal.Index(i).Interface()).Int())
}
dst.Set(reflect.ValueOf(a))
case floatType:
if src.ctype != _ArrayType|_FloatType {
return fmt.Errorf("'%s': value of type %s is not assignable to type []float64", path,
src.ctype)
}
a := []float64{}
for i := 0; i < srcVal.Len(); i++ {
a = append(a, reflect.ValueOf(srcVal.Index(i).Interface()).Float())
}
dst.Set(reflect.ValueOf(a))
case stringType:
if src.ctype != _ArrayType|_StringType {
return fmt.Errorf("'%s': value of type %s is not assignable to type []string", path,
src.ctype)
}
a := []string{}
for i := 0; i < srcVal.Len(); i++ {
a = append(a, reflect.ValueOf(srcVal.Index(i).Interface()).String())
}
dst.Set(reflect.ValueOf(a))
default:
// Type not supported, '[]float' for instance. Default case is
// unlikely to be executed since unsupported type errors are
// trapped while configuration files are loaded.
return fmt.Errorf("'%s': value of type %s is not assignable to type %s", path,
dst.Kind(), src.ctype)
}
}
return nil
}
func (c *Configuration) decodeValue(path string, src *configurationValue, dst reflect.Value) error {
if dst.Type() == dateType {
if src.ctype != _DateType {
return fmt.Errorf(
"'%s': value of type %s is not assignable to type time.Time", path,
src.ctype)
}
dst.Set(reflect.ValueOf(src.value.(time.Time)))
} else {
switch dst.Kind() {
case boolType:
if src.ctype != _BoolType {
return fmt.Errorf(
"'%s': value of type %s is not assignable to type bool", path,
src.ctype)
}
dst.SetBool(src.value.(bool))
case intType:
if src.ctype != _IntType {
return fmt.Errorf(
"'%s': value of type %s is not assignable to type int64", path,
src.ctype)
}
dst.SetInt(src.value.(int64))
case floatType:
if src.ctype != _FloatType {
return fmt.Errorf(
"'%s': value of type %s is not assignable to type float64", path,
src.ctype)
}
dst.SetFloat(src.value.(float64))
case stringType:
if src.ctype != _StringType {
return fmt.Errorf(
"'%s': value of type %s is not assignable to type string", path,
src.ctype)
}
dst.SetString(src.value.(string))
default:
// Type not supported, 'int' for instance.
return fmt.Errorf(
"'%s': value of type %s is not assignable to type %s", path,
dst.Kind(), src.ctype)
}
}
return nil
}