-
Notifications
You must be signed in to change notification settings - Fork 303
/
struct_reflect.go
76 lines (70 loc) · 1.79 KB
/
struct_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
69
70
71
72
73
74
75
76
package utils
import (
"reflect"
"strconv"
"strings"
)
var sliceOfInts = reflect.TypeOf([]int(nil))
var sliceOfStrings = reflect.TypeOf([]string(nil))
// JSONreflectStruct json数据反射为结构体
func JSONreflectStruct(jsonData map[string]interface{}, obj interface{}) (err error) {
objT := reflect.TypeOf(obj)
objV := reflect.ValueOf(obj)
objT = objT.Elem()
objV = objV.Elem()
for i := 0; i < objT.NumField(); i++ {
fieldT := objT.Field(i)
fieldV := objV.Field(i)
tags := strings.Split(fieldT.Tag.Get("form"), ",")
var tag string
if len(tags) == 0 || len(tags[0]) == 0 {
tag = fieldT.Name
} else if tags[0] == "-" {
continue
} else {
tag = tags[0]
}
jsonValue := jsonData[tag]
if jsonValue == nil {
continue
}
if fieldT.Type.Kind() != reflect.Ptr {
switch fieldT.Type.Kind() {
case reflect.Bool:
value := jsonValue.(string)
if strings.ToLower(value) == "on" || strings.ToLower(value) == "1" || strings.ToLower(value) == "yes" {
fieldV.SetBool(true)
continue
}
if strings.ToLower(value) == "off" || strings.ToLower(value) == "0" || strings.ToLower(value) == "no" {
fieldV.SetBool(false)
continue
}
b, err := strconv.ParseBool(value)
if err != nil {
return err
}
fieldV.SetBool(b)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
x, err := ToInt64(jsonValue)
if err != nil {
return err
}
fieldV.SetInt(x)
case reflect.Float32, reflect.Float64:
value := jsonValue.(string)
x, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
fieldV.SetFloat(x)
case reflect.Interface:
fieldV.Set(reflect.ValueOf(jsonValue))
case reflect.String:
value := jsonValue.(string)
fieldV.SetString(value)
}
}
}
return
}