-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariable.go
115 lines (110 loc) · 2.28 KB
/
variable.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
package go_config
import (
"fmt"
"reflect"
"strconv"
)
// Variable Routines
type Variable struct {
// field name
Name string
// default value
Def reflect.Value
// field description
Description string
// set value
Set func(x reflect.Value)
// field tags
Tag reflect.StructTag
// field type
Type reflect.Type
}
func (v Variable) String() string {
return fmt.Sprintf("%v[%v] %v", v.Name, v.Type.Kind(), v.Description)
}
func (v *Variable) set(value interface{}) {
if v.Type.Kind() == reflect.Struct {
return
}
if v.Type.Kind() == reflect.Slice {
slice, ok := value.([]interface{})
if !ok {
return
}
switch v.Type.Elem().Kind() {
case reflect.String:
resp := make([]string, len(slice))
for i, v := range slice {
switch v.(type) {
case string:
resp[i] = v.(string)
default:
resp[i] = fmt.Sprintf("%v", v)
}
}
v.Set(reflect.ValueOf(resp))
case reflect.Int:
resp := make([]int, len(slice))
for i, v := range slice {
switch v.(type) {
case int:
resp[i] = v.(int)
default:
intVal, err := strconv.ParseInt(fmt.Sprintf("%v", v), 10, 64)
if err != nil {
continue
}
resp[i] = int(intVal)
}
}
v.Set(reflect.ValueOf(resp))
case reflect.Float64:
resp := make([]float64, len(slice))
for i, v := range slice {
switch v.(type) {
case float64:
resp[i] = v.(float64)
default:
intVal, err := strconv.ParseFloat(fmt.Sprintf("%v", v), 64)
if err != nil {
continue
}
resp[i] = float64(intVal)
}
}
v.Set(reflect.ValueOf(resp))
case reflect.Uint:
resp := make([]uint, len(slice))
for i, v := range slice {
switch v.(type) {
case uint:
resp[i] = v.(uint)
default:
intVal, err := strconv.ParseUint(fmt.Sprintf("%v", v), 10, 64)
if err != nil {
continue
}
resp[i] = uint(intVal)
}
}
v.Set(reflect.ValueOf(resp))
case reflect.Bool:
resp := make([]bool, len(slice))
for i, v := range slice {
switch v.(type) {
case bool:
resp[i] = v.(bool)
default:
intVal, err := strconv.ParseBool(fmt.Sprintf("%v", v))
if err != nil {
continue
}
resp[i] = intVal
}
}
v.Set(reflect.ValueOf(resp))
}
return
}
v.Set(reflect.ValueOf(value).Convert(v.Type))
}