/
reflect.go
68 lines (65 loc) · 2.07 KB
/
reflect.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
package host
import (
"fmt"
"reflect"
)
func String(s string) *string {
return &s
}
func SetFieldTo(field reflect.Value, input interface{}) error {
// todo: explain how this works and why.
if !field.CanSet() {
return fmt.Errorf("cannot set field")
}
fieldType := field.Type()
switch fieldKind := field.Kind(); fieldKind {
case reflect.Array, reflect.Slice:
inputArray, ok := input.([]interface{})
field.Set(reflect.MakeSlice(field.Type(), len(inputArray), len(inputArray)))
if !ok {
return fmt.Errorf("Cannot assign to array from %s", reflect.TypeOf(input).Kind())
}
for idx, v := range inputArray {
err := SetFieldTo(field.Index(idx), v)
if err != nil {
return err
}
}
return nil
case reflect.Map:
inputMap := input.(map[string]interface{})
field.Set(reflect.MakeMap(field.Type()))
for k, v := range inputMap {
entry := reflect.New(field.Type().Elem())
err := SetFieldTo(entry, v)
if err != nil {
return err
}
field.SetMapIndex(reflect.ValueOf(k), entry)
}
case reflect.Ptr:
deref := field.Elem()
return SetFieldTo(deref, input)
case reflect.Bool:
field.SetBool(reflect.ValueOf(input).Convert(fieldType).Bool())
case reflect.Float32, reflect.Float64:
field.SetFloat(reflect.ValueOf(input).Convert(fieldType).Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.SetInt(reflect.ValueOf(input).Convert(fieldType).Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
field.SetUint(reflect.ValueOf(input).Convert(fieldType).Uint())
case reflect.String:
field.SetString(reflect.ValueOf(input).Convert(fieldType).String())
default:
valInput := reflect.ValueOf(input)
valid := field.IsValid()
canSet := field.CanSet()
inputType := valInput.Type()
if valid && canSet && inputType.ConvertibleTo(fieldType) {
field.Set(valInput.Convert(fieldType))
} else {
return fmt.Errorf("cannot set field %s\nIsValid: %v\nCanSet: %v\n%v -> %v: %v", fieldKind, valid, canSet, inputType, fieldType, valInput.Type().ConvertibleTo(fieldType))
}
}
return nil
}