/
populate.go
187 lines (137 loc) · 4.26 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
package popular
import (
"fmt"
"github.com/mtabini/go-bowtie"
"net/http"
"reflect"
"strings"
)
func populateArray(name string, sourceType reflect.Type, sourceValue reflect.Value, destType reflect.Type, destValue reflect.Value) error {
itemCount := sourceValue.Len()
rowDestType := destType.Elem()
for index := 0; index < itemCount; index++ {
rowSourceValue := sourceValue.Index(index)
rowSourceType := rowSourceValue.Type()
rowDestValue := reflect.New(rowDestType).Elem()
if err := populateValue(fmt.Sprintf("%s[%d]", name, index), rowSourceType, rowSourceValue, rowDestType, rowDestValue); err != nil {
return err
}
reflect.Append(destValue, rowDestValue)
}
return nil
}
func analyzeField(field reflect.StructField) (name string, ignore bool) {
if field.Anonymous {
ignore = true
return
}
name = field.Name
tag := strings.TrimSpace(field.Tag.Get("json"))
if tag != "" {
parts := strings.Split(tag, ",")
name = strings.TrimSpace(parts[0])
if name == "-" {
ignore = true
return
}
}
tag = strings.TrimSpace(field.Tag.Get("pop"))
switch tag {
case "required", "optional":
break
default:
ignore = true
}
return
}
func valueForField(sourceType reflect.Type, sourceValue reflect.Value, fieldName string) (value reflect.Value, found bool) {
switch sourceType.Kind() {
case reflect.Struct:
value = sourceValue.FieldByName(fieldName)
case reflect.Map:
value = sourceValue.MapIndex(reflect.ValueOf(fieldName))
default:
panic("Unexpectedly tried to extract a field from something that is neither a struct nor a map!")
}
found = value.IsValid()
return
}
func populateStruct(name string, sourceType reflect.Type, sourceValue reflect.Value, destType reflect.Type, destValue reflect.Value) error {
fieldCount := destType.NumField()
for index := 0; index < fieldCount; index++ {
field := destType.Field(index)
fieldDestValue := destValue.Field(index)
if !fieldDestValue.CanSet() {
continue
}
fieldName, ignore := analyzeField(field)
if ignore {
continue
}
fieldSourceValue, found := valueForField(sourceType, sourceValue, fieldName)
if found {
if err := populateValue(name+"."+fieldName, fieldSourceValue.Type(), fieldSourceValue, fieldDestValue.Type(), fieldDestValue); err != nil {
return err
}
}
}
return nil
}
func populateValue(name string, sourceType reflect.Type, sourceValue reflect.Value, destType reflect.Type, destValue reflect.Value) error {
if sourceType.Kind() == reflect.Interface {
sourceValue = sourceValue.Elem()
sourceType = sourceValue.Type()
}
if sourceType.Kind() == reflect.Ptr {
if sourceValue.IsNil() {
return nil
}
sourceValue = sourceValue.Elem()
sourceType = sourceValue.Type()
}
if destType.Kind() == reflect.Ptr {
if destValue.IsNil() {
destValue.Set(reflect.New(destType.Elem()))
}
destValue = destValue.Elem()
destType = destValue.Type()
}
switch destType.Kind() {
case reflect.Array, reflect.Slice:
if sourceType.Kind() != reflect.Array {
return bowtie.NewError(http.StatusBadRequest, "%s.type.invalid", name)
}
return populateArray(name, sourceType, sourceValue, destType, destValue)
case reflect.Struct:
switch sourceType.Kind() {
case reflect.Struct, reflect.Map:
return populateStruct(name, sourceType, sourceValue, destType, destValue)
default:
return bowtie.NewError(http.StatusBadRequest, "%s.type.invalid", name)
}
default:
if !sourceValue.Type().ConvertibleTo(destType) {
return bowtie.NewError(http.StatusBadRequest, "%s.type.invalid", name)
}
if destType.Kind() == reflect.String && sourceType.Kind() == reflect.Int {
return bowtie.NewError(http.StatusBadRequest, "%s.type.invalid", name)
} else {
destValue.Set(sourceValue.Convert(destType))
}
}
return nil
}
func Populate(src interface{}, dest interface{}) error {
sourceValue := reflect.ValueOf(src)
sourceType := reflect.TypeOf(src)
destValue := reflect.ValueOf(dest)
if destValue.Kind() != reflect.Ptr {
return bowtie.NewError(http.StatusInternalServerError, "`dest` must be a pointer")
}
if destValue.IsNil() {
return bowtie.NewError(http.StatusInternalServerError, "`dest` cannot be nil")
}
destValue = destValue.Elem()
destType := destValue.Type()
return populateValue("root", sourceType, sourceValue, destType, destValue)
}