-
Notifications
You must be signed in to change notification settings - Fork 1
/
field.go
285 lines (236 loc) · 5.34 KB
/
field.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
281
282
283
284
285
package gofig
import (
"reflect"
"strconv"
)
// A Field represents a type that we can set a value for based on its key.
type Field interface {
// Set sets the fields value to the provided interface provided the types match.
Set(interface{}) error
// Key returns the full key path to the field
Key() string
// Value returns the destination reflect.Value values will be set into.
Value() reflect.Value
// CanSet returns a bool indicating the parser can set the fields value.
CanSet(Prioritiser) bool
// SetPriority sets the fields priority.
SetPriority(Prioritiser)
}
// Fields holds a map of keys to fields.
type Fields map[string]Field
// Set sets a keys field value
func (f Fields) Set(k string, v Field) {
f[k] = v
}
// A Field holds the fields struct path and reflected value.
type field struct {
key string // foo.bar.baz
value reflect.Value
priority uint8
}
func newField(k string, v reflect.Value) *field {
return &field{
key: k,
value: v,
}
}
func (f *field) Set(value interface{}) error {
return set(f.value, value)
}
func (f *field) Key() string {
return f.key
}
func (f *field) Value() reflect.Value {
return f.value
}
func (f *field) CanSet(p Prioritiser) bool {
return p.Priority() >= f.priority
}
func (f *field) SetPriority(p Prioritiser) {
f.priority = p.Priority()
}
// mapField embedded field wrapping map key values allowing setting map fields to be the same as
// setting struct fields.
type mapField struct {
*field
mk reflect.Value // Map key to set values in
mp reflect.Value // Map to set values in
}
func newMapField(k, mk string, mp reflect.Value) *mapField {
return &mapField{
field: newField(k, reflect.New(mp.Type().Elem()).Elem()),
mk: reflect.ValueOf(mk),
mp: mp,
}
}
func (f *mapField) Set(v interface{}) error {
// TODO: ensure v == f.value
if err := f.field.Set(v); err != nil {
return err
}
// Set the map index to the given value
f.mp.SetMapIndex(f.mk, f.value)
return nil
}
func set(field reflect.Value, value interface{}) error {
if u := unmarshaler(field); u != nil {
return u.UnmarshalGoFig(value)
}
switch field.Kind() {
case reflect.Ptr:
return set(field.Elem(), value)
case reflect.String:
return setString(field, value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return setInt(field, value)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return setUint(field, value)
case reflect.Float32, reflect.Float64:
return setFloat(field, value)
case reflect.Slice, reflect.Array:
return setSlice(field, value)
}
return ErrInvalidConversion{
To: field.Kind(),
From: reflect.ValueOf(value).Kind(),
}
}
// setString sets the fields value to a string.
func setString(field reflect.Value, value interface{}) error {
v, ok := value.(string)
if !ok {
return ErrSetValue{
Field: field,
Value: reflect.ValueOf(value),
}
}
field.SetString(v)
return nil
}
// setInt sets the fields value to an integer.
func setInt(field reflect.Value, value interface{}) error {
var i int64
switch t := value.(type) {
case string:
v, err := strconv.ParseInt(t, 10, 64)
if err != nil {
return err
}
i = v
case int:
i = int64(t)
case int8:
i = int64(t)
case int16:
i = int64(t)
case int32:
i = int64(t)
case int64:
i = int64(t)
case float32:
i = int64(t)
case float64:
i = int64(t)
default:
return ErrSetValue{
Field: field,
Value: reflect.ValueOf(value),
}
}
if field.OverflowInt(i) {
return ErrSetValue{
Field: field,
Value: reflect.ValueOf(i),
}
}
field.SetInt(i)
return nil
}
// setUint64 sets the fields value to an unsigned integer type.
func setUint(field reflect.Value, value interface{}) error {
var i uint64
switch t := value.(type) {
case string:
v, err := strconv.ParseUint(t, 10, 64)
if err != nil {
return err
}
i = v
case int:
i = uint64(t)
case int8:
i = uint64(t)
case int16:
i = uint64(t)
case int32:
i = uint64(t)
case int64:
i = uint64(t)
case float32:
i = uint64(t)
case float64:
i = uint64(t)
default:
return ErrSetValue{
Field: field,
Value: reflect.ValueOf(value),
}
}
if field.OverflowUint(i) {
return ErrSetValue{
Field: field,
Value: reflect.ValueOf(i),
}
}
field.SetUint(i)
return nil
}
// setFloat sets the fields value to the a float.
func setFloat(field reflect.Value, value interface{}) error {
var i float64
switch t := value.(type) {
case string:
v, err := strconv.ParseFloat(t, 64)
if err != nil {
return err
}
i = v
case float32:
i = float64(t)
case float64:
i = float64(t)
default:
return ErrSetValue{
Field: field,
Value: reflect.ValueOf(value),
}
}
if field.OverflowFloat(i) {
return ErrSetValue{
Field: field,
Value: reflect.ValueOf(i),
}
}
field.SetFloat(i)
return nil
}
// setSlice sets the fields value to the given slice.
func setSlice(field reflect.Value, value interface{}) error {
ft := field.Type()
vv := reflect.ValueOf(value)
if vv.Kind() != reflect.Array && vv.Kind() != reflect.Slice {
return ErrInvalidValue{
Type: ft,
}
}
s := reflect.MakeSlice(reflect.SliceOf(ft.Elem()), vv.Len(), vv.Cap())
for i := 0; i < vv.Len(); i++ {
e := reflect.New(ft.Elem())
if err := set(e, vv.Index(i).Interface()); err != nil {
return err
}
s.Index(i).Set(e.Elem())
}
field.Set(s)
return nil
}