Repository navigation
Expand file tree
/
Copy pathimplementation.go
More file actions
213 lines (193 loc) · 5.54 KB
/
Copy pathimplementation.go
File metadata and controls
213 lines (193 loc) · 5.54 KB
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
package flatpack
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"unicode"
"unicode/utf8"
)
// Unexported implementation class for unmarshaller.
type implementation struct {
source Getter
}
// Unmarshal reads configuration data from some source into a struct.
func (f implementation) Unmarshal(dest interface{}) error {
_, err := f.unmarshal(Key{}, dest)
return err
}
// Read configuration source into a struct or sub-struct. Return the number of
// fields that were set.
func (f implementation) unmarshal(prefix Key, dest interface{}) (int, error) {
count := 0
v := reflect.ValueOf(dest)
if v.Kind() == reflect.Ptr {
if v.IsNil() {
return 0, &BadValue{Name: prefix, expected: "non-nil pointer to struct"}
}
v = v.Elem()
} else {
return 0, &BadType{Name: prefix, Kind: v.Kind(), reason: "expected pointer to struct"}
}
vt := v.Type()
if vt.Kind() != reflect.Struct {
return 0, &BadType{Name: prefix, Kind: vt.Kind(), reason: "expected struct"}
}
// prepare a reusable key whose last element will change as we iterate
// through the fields in this struct
name := make(Key, len(prefix)+1)
copy(name, prefix)
for i := 0; i < vt.NumField(); i++ {
field := vt.Field(i)
value := v.Field(i)
letter, _ := utf8.DecodeRuneInString(field.Name)
if !unicode.IsUpper(letter) {
if f.canIgnore(&field) {
continue
} else {
return 0, &NoReflection{Name: name}
}
}
name[len(name)-1] = field.Name
read, err := f.read(name, &field, value)
if err != nil {
return 0, err
}
count += read
}
validater, ok := dest.(Validater)
if ok {
return count, validater.Validate()
}
return count, nil
}
func (f implementation) canIgnore(field *reflect.StructField) bool {
tag := field.Tag.Get("flatpack")
return tag == "ignore"
}
// Coerce a string to a suitable Type and then assign it to a Value (either a
// struct field or an element of a slice).
func (f implementation) assign(dest reflect.Value, source string, name Key) (err error) {
kind := dest.Type().Kind()
switch kind {
case reflect.Bool:
var boolean bool
boolean, err = strconv.ParseBool(source)
if err == nil {
dest.SetBool(boolean)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var number int64
number, err = strconv.ParseInt(source, 10, int(dest.Type().Size()*8))
if err == nil {
dest.SetInt(number)
} else {
numError, ok := err.(*strconv.NumError)
if ok {
err = &BadValue{Name: name, Cause: numError}
}
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
reflect.Uint64, reflect.Uintptr:
var number uint64
number, err = strconv.ParseUint(source, 10, int(dest.Type().Size()*8))
if err == nil {
dest.SetUint(number)
} else {
numError, ok := err.(*strconv.NumError)
if ok {
err = &BadValue{Name: name, Cause: numError}
}
}
case reflect.Float32, reflect.Float64:
var number float64
number, err = strconv.ParseFloat(source, int(dest.Type().Size()*8))
if err == nil {
dest.SetFloat(number)
} else {
numError, ok := err.(*strconv.NumError)
if ok {
err = &BadValue{Name: name, Cause: numError}
}
}
case reflect.String:
if err == nil {
dest.SetString(source)
}
default:
// should be unreachable due to validation in read()
panic(fmt.Errorf("flatpack: unreachable code in assign(); bug in read()? (kind=%s)", kind))
}
return
}
// Set a single struct field by reading a string from the Getter, massaging it
// to the correct Type for that field, and assigning to the given Value.
//
// If the field is itself a struct, recursively unmarshal into the sub-
// struct. If the field is a pointer to anything, allocate it and then
// recursively read into the pointed-to value.
//
// Return the number of fields that were set.
func (f implementation) read(name Key, field *reflect.StructField, value reflect.Value) (int, error) {
count := 0
vt := value.Type()
kind := vt.Kind()
var got string
var err error
switch kind {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64,
reflect.String:
got, err = f.source.Get(name)
if err == nil && got != "" {
err = f.assign(value, got, name)
count++
}
case reflect.Slice:
got, err = f.source.Get(name)
if err == nil && got != "" {
var raw []interface{}
err = json.Unmarshal([]byte(got), &raw)
if err == nil {
vte := value.Type().Elem()
value.Set(reflect.MakeSlice(vt, len(raw), len(raw)))
for i, elem := range raw {
if err == nil {
vi := value.Index(i)
if vte.Kind() == reflect.Ptr {
vi.Set(reflect.New(vte.Elem()))
vi = vi.Elem()
}
err = f.assign(vi, fmt.Sprintf("%v", elem), name)
count++
}
}
}
}
case reflect.Struct:
addr := value.Addr()
if addr.CanInterface() {
count, err = f.unmarshal(name, addr.Interface())
} // else if !f.canIgnore(field) {
// err = &NoReflection{Name: name}
//}
case reflect.Ptr:
// Handle pointers by allocating if necessary, then recursively calling
// ourselves.
if value.IsNil() {
value.Set(reflect.New(value.Type().Elem()))
}
count, err = f.read(name, field, value.Elem())
// Set pointer (back) to nil if no values were read into it; prevent
// fooling client into thinking he got nested values when he did not.
if count == 0 {
value.Set(reflect.Zero(value.Type()))
}
default:
if !f.canIgnore(field) {
err = &BadType{Name: name, Kind: value.Kind(), reason: "unsupported data type"}
}
}
return count, err
}