-
Notifications
You must be signed in to change notification settings - Fork 2
/
mapBinding.go
88 lines (79 loc) · 2.21 KB
/
mapBinding.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
package bencoding
import (
"errors"
"reflect"
)
func bind(d map[string]interface{}, o reflect.Value) error {
for k, v := range d {
if e := bindField(k, v, o); e != nil {
return e
}
}
return nil
}
func findCorrectlyTaggedField(name string, o reflect.Value) reflect.Value {
ot := o.Type()
for i := 0; i < o.NumField(); i++ {
fname := ot.Field(i).Name
fieldOpts := extractFieldOptions(o, fname)
if string(fieldOpts) == name {
return o.Field(i)
}
}
return reflect.Value{}
}
func bindField(k string, value interface{}, o reflect.Value) error {
if field := findCorrectlyTaggedField(k, o); !field.IsValid() {
return nil // missing struct fields are not errors
} else if !bindValues(field, value) {
fromType := reflect.ValueOf(value).String()
return errors.New(" field '" + k + "' failed to bind from " + fromType)
}
return nil
}
func bindValues(field reflect.Value, value interface{}) bool {
vvalue := reflect.ValueOf(value)
if vvalue.Type().AssignableTo(field.Type()) {
field.Set(vvalue)
return true
} else if isBindableStructAndDict(field, value) {
return bind(value.(map[string]interface{}), field) == nil
} else if field.Kind() == reflect.Ptr {
return bindPtr(value, field) == nil
} else if list, isList := value.([]interface{}); field.Kind() == reflect.Slice && isList {
for _, v := range list {
if reflect.TypeOf(v).AssignableTo(field.Type().Elem()) {
field.Set(reflect.Append(field, reflect.ValueOf(v)))
}
}
return true
}
return false
}
type withKind interface {
Kind() reflect.Kind
}
func isBindableStructAndDict(structure withKind, dictionary interface{}) bool {
if _, isDict := dictionary.(map[string]interface{}); structure.Kind() == reflect.Struct && isDict {
return true
}
return false
}
func bindPtr(value interface{}, field reflect.Value) error {
vvalue := reflect.ValueOf(value)
if vvalue.Type().AssignableTo(field.Type().Elem()) {
prepare(field)
field.Elem().Set(vvalue)
} else if isBindableStructAndDict(field.Type().Elem(), value) {
prepare(field)
bind(value.(map[string]interface{}), field.Elem())
} else {
return errors.New("unable to bind")
}
return nil
}
func prepare(v reflect.Value) {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
}