forked from vdobler/ht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate.go
334 lines (302 loc) · 8.49 KB
/
populate.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2016 Volker Dobler. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package populate provides function to populate Go values from
// an untyped interface{} soup.
package populate
import (
"fmt"
"reflect"
"strconv"
"time"
)
type Populator interface {
Populate(src interface{}) error
}
// Strict populates dst from src failing if elements in src cannot be mapped
// to dst.
func Strict(dst, src interface{}) error {
dv, sv := reflect.ValueOf(dst), reflect.ValueOf(src)
if dv.Kind() != reflect.Ptr || dv.IsNil() {
panic("populate: not a pointer or nil")
}
x := reflect.New(dv.Type()).Elem()
err := recFillWith(x, sv, x.Type().Elem().Name(), true)
if err != nil {
return err
}
dv.Elem().Set(x.Elem())
return nil
}
// Lax populates dst from src. Src may contain elements which cannot
// be mapped to dst in which case they are ignored silently.
func Lax(dst, src interface{}) error {
dv, sv := reflect.ValueOf(dst), reflect.ValueOf(src)
if dv.Kind() != reflect.Ptr || dv.IsNil() {
return fmt.Errorf("Not a pointer or nil")
}
x := reflect.New(dv.Type()).Elem()
err := recFillWith(x, sv, x.Type().Elem().Name(), false)
if err != nil {
return err
}
dv.Elem().Set(x.Elem())
return nil
}
func setFloat(dst, src reflect.Value, elem string) error {
f := 0.0
switch src.Kind() {
case reflect.Bool:
if src.Bool() {
f = 1.0
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
f = float64(src.Int())
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
f = float64(src.Uint())
case reflect.Float64, reflect.Float32:
f = src.Float()
case reflect.String:
s := src.String()
var err error
f, err = strconv.ParseFloat(s, 64)
if err != nil {
return fmt.Errorf("cannot set %s <%s> to %s", elem, dst.Kind(), s)
}
default:
return fmt.Errorf("cannot set %s <%s> to %v <%s>",
elem, dst.Kind(), src.Interface(), src.Kind())
}
dst.SetFloat(f)
return nil
}
func setBool(dst, src reflect.Value, elem string) error {
b := false
switch src.Kind() {
case reflect.Bool:
b = src.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if src.Int() != 0 {
b = true
}
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if src.Uint() != 0 {
b = true
}
case reflect.Float64, reflect.Float32:
if src.Float() != 0.0 {
b = true
}
case reflect.String:
switch src.String() {
case "true", "TRUE", "1", "yes", "YES":
b = true
case "false", "FALSE", "0", "no", "NO":
b = false
default:
return fmt.Errorf("cannot set %s <bool> to %s", elem, src.String())
}
default:
return fmt.Errorf("cannot set %s <%s> to %v <%s>",
elem, dst.Kind(), src.Interface(), src.Kind())
}
dst.SetBool(b)
return nil
}
func setInt(dst, src reflect.Value, elem string) error {
i := int64(0)
switch src.Kind() {
case reflect.Bool:
if src.Bool() {
i = 1
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i = src.Int()
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
i = int64(src.Uint())
if uint64(i) != src.Uint() {
return fmt.Errorf("cannot set %s <%s> to %d, overflow",
elem, dst.Kind(), src.Uint())
}
case reflect.Float64, reflect.Float32:
i = int64(src.Float())
case reflect.String:
s := src.String()
var err error
i, err = strconv.ParseInt(s, 10, 64)
if err != nil {
return fmt.Errorf("cannot set %s <%s> to %s", elem, dst.Kind(), s)
}
default:
return fmt.Errorf("cannot set %s <%s> to %v <%s>",
elem, dst.Kind(), src.Interface(), src.Kind())
}
dst.SetInt(i)
return nil
}
func setDuration(dst, src reflect.Value, elem string) error {
switch src.Kind() {
case reflect.Int64:
if isDuration(src) {
dst.Set(src)
return nil
}
fallthrough
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
dst.SetInt(1e9 * src.Int()) // nanoseconds
return nil
case reflect.Float64, reflect.Float32:
dst.SetInt(int64(1e9 * src.Float())) // nanoseconds
case reflect.String:
d, err := time.ParseDuration(src.String())
if err != nil {
return fmt.Errorf("cannot set %s <Duration> to %s", elem, src.String())
}
dst.SetInt(int64(d))
return nil
}
return fmt.Errorf("cannot set %s <Duration> to %v <%s>",
elem, src.Interface(), src.Kind())
}
func setUint(dst, src reflect.Value, elem string) error {
panic("not implemented")
}
func setSlice(dst, src reflect.Value, elem string, strict bool) error {
if src.Kind() == reflect.Slice {
n := src.Len()
dst.Set(reflect.MakeSlice(dst.Type(), n, n))
for i := 0; i < n; i++ {
err := recFillWith(dst.Index(i), src.Index(i),
fmt.Sprintf("%s[%d]", elem, i), strict)
if err != nil {
return err
}
}
return nil
}
// Autogenerated single element slice.
dst.Set(reflect.MakeSlice(dst.Type(), 1, 1))
return recFillWith(dst.Index(0), src, fmt.Sprintf("%s[%d]", elem, 0), strict)
}
func setMap(dst, src reflect.Value, elem string, strict bool) error {
switch src.Kind() {
case reflect.Map:
dst.Set(reflect.MakeMap(dst.Type()))
for _, key := range src.MapKeys() {
srcValue := src.MapIndex(key)
dstValue := reflect.New(dst.Type().Elem()).Elem()
err := recFillWith(dstValue, srcValue,
fmt.Sprintf("%s[%v]", elem, key.Interface()), strict)
if err != nil {
return err
}
// TODO: generate and fill destination key from source key as their types may differ.
dst.SetMapIndex(key, dstValue)
}
return nil
}
mt := dst.Type()
return fmt.Errorf("cannot set %s <map[%s]%s> to %v <%s>",
elem, mt.Key().Kind(), mt.Elem().Kind(), src.Interface(), src.Kind())
}
func setStruct(dst, src reflect.Value, elem string, strict bool) error {
switch src.Kind() {
case reflect.Map:
for _, key := range src.MapKeys() {
if key.Kind() != reflect.String {
return fmt.Errorf("cannot set %s to map with %s keys",
elem, key.Kind())
}
name := key.String()
srcValue := src.MapIndex(key)
//field := dst.Type().FieldByName(name)
field := dst.FieldByName(name)
if !field.IsValid() {
if name == "comment" || !strict {
continue
}
return fmt.Errorf("unknown field %s in %s",
name, elem) // TODO: error is unclear
}
err := recFillWith(field, srcValue,
fmt.Sprintf("%s.%s", elem, name), strict)
if err != nil {
return err
}
}
return nil
}
return fmt.Errorf("cannot set %s <%s> to %v <%s>",
elem, dst.Kind(), src.Interface(), src.Kind())
}
func recFillWith(dst, src reflect.Value, elem string, strict bool) error {
// fmt.Println("recFillWith", elem)
if src.Kind() == reflect.Interface {
src = src.Elem()
// fmt.Printf("Unwrapped interface src to %s\n", src.Kind())
return recFillWith(dst, src, elem, strict)
}
if !dst.CanSet() {
// This should not happen, or?
return fmt.Errorf("cannot set element %s (%v)", elem, dst)
}
if dst.Kind() != reflect.Ptr && dst.Type().Name() != "" && dst.CanAddr() {
dstAddr := dst.Addr()
if p, ok := dstAddr.Interface().(Populator); ok {
err := p.Populate(src.Interface())
if err != nil {
return err
} else {
dst.Set(dstAddr.Elem())
return nil
}
}
}
for dst.Kind() == reflect.Ptr {
elemTyp := dst.Type().Elem()
dst.Set(reflect.New(elemTyp))
// TODO: only create new var if src != null
/*
err := recFillWith(dst.Elem(), src, fmt.Sprintf("*%s", elem))
if err != nil {
return err
}
*/
dst = dst.Elem()
}
// fmt.Printf("recFillWith %s (%s) with %s \n", elem, dst.Kind(), src.Kind())
switch dst.Kind() {
case reflect.Bool:
return setBool(dst, src, elem)
case reflect.Int64:
if isDuration(dst) {
return setDuration(dst, src, elem)
}
fallthrough
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
return setInt(dst, src, elem)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return setUint(dst, src, elem)
case reflect.Float64, reflect.Float32:
return setFloat(dst, src, elem)
case reflect.String:
dst.SetString(fmt.Sprintf("%v", src.Interface()))
return nil
case reflect.Slice:
return setSlice(dst, src, elem, strict)
case reflect.Map:
return setMap(dst, src, elem, strict)
case reflect.Struct:
return setStruct(dst, src, elem, strict)
case reflect.Interface:
dst.Set(src)
default:
return fmt.Errorf("cannot set %s <%s> to <%s>", elem, dst.Kind(), src.Kind())
}
return nil
}
func isDuration(v reflect.Value) bool {
t := v.Type()
return (t.PkgPath() == "time" && t.Name() == "Duration") ||
(t.PkgPath() == "github.com/vdobler/ht/ht" && t.Name() == "Duration")
}