-
Notifications
You must be signed in to change notification settings - Fork 3
/
set.go
307 lines (259 loc) · 6.31 KB
/
set.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
/*
# reflectutils
reflectutils is for setting your struct value by using string name and path that follows reflectutils rules.
## The rules
- `.Name` to set a property by field name
- `.Person.Name` to set the name of the current property
- `.Person.Addresses[0].Phone` to set an element of an array property
- `.Person.Addresses[].Name` it will create a object of address and set it's property
- `.Person.MapData.Name` it can also set value to map
## How to install
go get github.com/sunfmin/reflectutils
*/
package reflectutils
import (
"errors"
"fmt"
"log"
"reflect"
"strconv"
"strings"
)
var NoSuchFieldError = errors.New("no such field")
// Set value of a struct by path using reflect.
func Set(i interface{}, name string, value interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprint(r))
}
}()
v := reflect.ValueOf(i)
if v.Kind() != reflect.Ptr {
err = errors.New("set object must be a pointer.")
return
}
for v.Elem().Kind() == reflect.Ptr {
v = v.Elem()
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
}
sv := v.Elem()
if name == "" {
switch inputv := value.(type) {
case string:
err = setStringValue(sv, inputv)
if err != nil {
return
}
case []byte:
err = setStringValue(sv, string(inputv))
if err != nil {
return
}
default:
if value == nil {
vm := reflect.ValueOf(i)
vm.Elem().Set(reflect.Zero(vm.Elem().Type()))
return
}
valv := reflect.ValueOf(value)
for valv.Kind() == reflect.Ptr {
valv = valv.Elem()
}
sv.Set(valv)
}
return
}
var token *dotToken
token, err = nextDot(name)
if err != nil {
return
}
// printv(sv.Interface(), name, value)
if sv.Kind() == reflect.Map {
// map must have string type
mv := sv
if mv.Type().Key() != reflect.TypeOf("") {
return fmt.Errorf("map key %s must be string type", name)
}
if mv.IsNil() {
mv.Set(reflect.MakeMap(mv.Type()))
}
keyValue := reflect.ValueOf(token.Field)
elemType := mv.Type().Elem()
mapElem := reflect.New(elemType).Elem()
existElem := mv.MapIndex(keyValue)
if existElem.IsValid() {
mapElem.Set(existElem)
}
err = Set(mapElem.Addr().Interface(), token.Left, value)
if err != nil {
return
}
mv.SetMapIndex(keyValue, mapElem)
return
}
if sv.Kind() == reflect.Slice {
av := sv
elemType := av.Type().Elem()
var newslice reflect.Value
if token.IsAppendingArray {
newslice = av
arrayElem := reflect.New(elemType).Elem()
err = Set(arrayElem.Addr().Interface(), token.Left, value)
if err != nil {
return
}
newslice = reflect.Append(newslice, arrayElem)
} else {
if av.Len() > token.ArrayIndex {
newslice = reflect.MakeSlice(av.Type(), 0, 0)
arrayElem := av.Index(token.ArrayIndex)
if !arrayElem.IsValid() {
arrayElem.Set(reflect.New(elemType).Elem())
}
err = Set(arrayElem.Addr().Interface(), token.Left, value)
if err != nil {
return
}
for i := 0; i < token.ArrayIndex; i++ {
newslice = reflect.Append(newslice, av.Index(i))
}
newslice = reflect.Append(newslice, arrayElem)
for i := token.ArrayIndex + 1; i < av.Len(); i++ {
newslice = reflect.Append(newslice, av.Index(i))
}
} else {
newslice = av
if newslice.IsNil() {
newslice = reflect.MakeSlice(newslice.Type(), 0, 0)
}
arrayElem := reflect.New(elemType).Elem()
err = Set(arrayElem.Addr().Interface(), token.Left, value)
if err != nil {
return
}
if newslice.Len() < token.ArrayIndex {
for newslice.Len() < token.ArrayIndex {
newslice = reflect.Append(newslice, reflect.Zero(elemType))
}
}
newslice = reflect.Append(newslice, arrayElem)
}
}
av.Set(newslice)
return
}
if sv.Kind() == reflect.Struct {
fv := sv.FieldByNameFunc(func(fname string) bool {
return strings.EqualFold(fname, token.Field)
})
if !fv.IsValid() {
// err = errors.New(fmt.Sprintf("%+v has no such field `%s`.", sv.Interface(), token.Field))
err = NoSuchFieldError
return
}
err = Set(fv.Addr().Interface(), token.Left, value)
return
}
return
}
type dotToken struct {
Field string
Left string
IsArray bool
ArrayIndex int
IsAppendingArray bool
}
func nextDot(name string) (t *dotToken, err error) {
t = &dotToken{}
t.Field = strings.Trim(name, ".[")
if i := strings.IndexAny(t.Field, ".["); i > 0 {
t.Field, t.Left = t.Field[:i], t.Field[i+1:]
}
if t.Field[len(t.Field)-1:] == "]" {
t.IsArray = true
arrayIndexString := t.Field[0 : len(t.Field)-1]
if arrayIndexString == "" {
t.IsAppendingArray = true
} else {
var i64 int64
i64, err = strconv.ParseInt(arrayIndexString, 10, 64)
t.ArrayIndex = int(i64)
if err != nil {
return
}
}
}
return
}
func printv(v interface{}, name interface{}) {
log.Println("=====")
rv := reflect.ValueOf(v)
log.Printf(
"\n\tname: %+v, \n\tv: %+v, \n\trv: %+v, \n\trv.Kind(): %+v, \n\trv.Type(): %+v, \n\trv.IsValid(): %+v",
name,
v,
rv,
rv.Kind(),
rv.Type(),
rv.IsValid(),
)
log.Println("=====")
}
func setStringValue(v reflect.Value, value string) (err error) {
s := value
// if type is []byte
if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 {
v.SetBytes([]byte(s))
return
}
switch v.Kind() {
case reflect.String:
v.SetString(s)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var n int64
n, err = strconv.ParseInt(s, 10, 64)
if err != nil {
return
}
if v.OverflowInt(n) {
err = fmt.Errorf("overflow int64 for %d", n)
return
}
v.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
var n uint64
n, err = strconv.ParseUint(s, 10, 64)
if err != nil {
return
}
if v.OverflowUint(n) {
err = fmt.Errorf("overflow uint64 for %d", n)
return
}
v.SetUint(n)
case reflect.Float32, reflect.Float64:
var n float64
n, err = strconv.ParseFloat(s, v.Type().Bits())
if err != nil {
return
}
if v.OverflowFloat(n) {
err = fmt.Errorf("overflow float64 for %f", n)
return
}
v.SetFloat(n)
case reflect.Bool:
var n bool
n, err = strconv.ParseBool(s)
if err != nil {
return
}
v.SetBool(n)
default:
err = fmt.Errorf("value %+v can only been set to primary type but was %+v", value, v)
}
return
}