-
Notifications
You must be signed in to change notification settings - Fork 10
/
set.go
162 lines (136 loc) · 3.54 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
package clop
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"time"
)
type convert struct {
bitSize int
cb func(val string, bitSize int, field reflect.Value) error
}
var convertFunc map[reflect.Kind]convert
func init() {
convertFunc = map[reflect.Kind]convert{
reflect.Uint: {bitSize: 0, cb: setUintField},
reflect.Uint8: {bitSize: 8, cb: setUintField},
reflect.Uint16: {bitSize: 16, cb: setUintField},
reflect.Uint32: {bitSize: 32, cb: setUintField},
reflect.Uint64: {bitSize: 64, cb: setUintField},
reflect.Int: {bitSize: 0, cb: setIntField},
reflect.Int8: {bitSize: 8, cb: setIntField},
reflect.Int16: {bitSize: 16, cb: setIntField},
reflect.Int32: {bitSize: 32, cb: setIntField},
reflect.Int64: {bitSize: 64, cb: setIntDurationField},
reflect.Bool: {bitSize: 0, cb: setBoolField},
reflect.Float32: {bitSize: 32, cb: setFloatField},
reflect.Float64: {bitSize: 64, cb: setFloatField},
reflect.Struct: {bitSize: 0, cb: setStructField},
reflect.Slice: {bitSize: 0, cb: setSlice},
reflect.Map: {bitSize: 0, cb: setMapField},
}
}
func setIntDurationField(val string, bitSize int, value reflect.Value) error {
switch value.Interface().(type) {
case time.Duration:
return setTimeDuration(val, bitSize, value)
}
return setIntField(val, bitSize, value)
}
func setIntField(val string, bitSize int, field reflect.Value) error {
if val == "" {
val = "0"
}
intVal, err := strconv.ParseInt(val, 10, bitSize)
if err == nil {
field.SetInt(intVal)
}
return err
}
func setUintField(val string, bitSize int, field reflect.Value) error {
if val == "" {
val = "0"
}
uintVal, err := strconv.ParseUint(val, 10, bitSize)
if err == nil {
field.SetUint(uintVal)
}
return err
}
func setBoolField(val string, bitSize int, field reflect.Value) error {
if val == "" {
val = "false"
}
boolVal, err := strconv.ParseBool(val)
if err == nil {
field.SetBool(boolVal)
}
return err
}
func setFloatField(val string, bitSize int, field reflect.Value) error {
if val == "" {
val = "0.0"
}
floatVal, err := strconv.ParseFloat(val, bitSize)
if err == nil {
field.SetFloat(floatVal)
}
return err
}
func setStructField(val string, bitSize int, value reflect.Value) error {
switch value.Interface().(type) {
case time.Time:
//return setTimeField(val, bitSize, value)
}
return json.Unmarshal([]byte(val), value.Addr().Interface())
}
func setSlice(val string, bitSize int, value reflect.Value) error {
if value.Kind() == reflect.Ptr {
value = value.Elem()
}
if value.Len() == 0 {
// 初始化一个不为空的slice
value.Set(reflect.MakeSlice(value.Type(), 0, 1))
}
v := reflect.New(value.Type().Elem())
v = v.Elem()
if err := setBase(val, v); err != nil {
return err
}
v2 := reflect.Append(value, v)
value.Set(v2)
return nil
}
func setMapField(val string, bitSize int, value reflect.Value) error {
return json.Unmarshal([]byte(val), value.Addr().Interface())
}
func setTimeDuration(val string, bitSize int, value reflect.Value) error {
if val == "" {
val = "0"
}
d, err := time.ParseDuration(val)
if err != nil {
return err
}
value.Set(reflect.ValueOf(d))
return nil
}
func setBase(val string, value reflect.Value) error {
if value.Kind() == reflect.String {
value.SetString(val)
return nil
}
fn, ok := convertFunc[value.Kind()]
if ok {
return fn.cb(val, fn.bitSize, value)
}
return fmt.Errorf("type (%T) unsupported type", value)
}
func resetValue(value reflect.Value) {
if !value.CanSet() {
return
}
v := reflect.New(value.Type()).Elem()
value.Set(v)
}