-
Notifications
You must be signed in to change notification settings - Fork 12
/
reflect-set.go
211 lines (200 loc) · 6.18 KB
/
reflect-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
package variable
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/eolinker/eosc"
"github.com/eolinker/eosc/log"
)
var (
ErrorVariableNotFound = errors.New("data not found")
ErrorUnsupportedKind = errors.New("unsupported kind")
)
func stringSet(value reflect.Value, targetVal reflect.Value, variables eosc.IVariable) ([]string, error) {
if targetVal.Kind() == reflect.Ptr {
return stringSet(value, targetVal.Elem(), variables)
}
builder := NewBuilder(value.String())
val, useVariables, success := builder.Replace(variables)
if !success {
return nil, ErrorVariableNotFound
}
switch targetVal.Kind() {
case reflect.Int, reflect.Int32, reflect.Int64:
v, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return nil, fmt.Errorf("string set parse int error: %w", err)
}
targetVal.SetInt(v)
case reflect.Bool:
v, err := strconv.ParseBool(val)
if err != nil {
return nil, fmt.Errorf("string set parse bool error: %w", err)
}
targetVal.SetBool(v)
case reflect.String:
targetVal.SetString(val)
default:
return nil, fmt.Errorf("%w %s", ErrorUnsupportedKind, targetVal.Kind())
}
return useVariables, nil
}
func interfaceSet(originVal reflect.Value, targetVal reflect.Value, variables eosc.IVariable) ([]string, error) {
usedVariables := make([]string, 0, variables.Len())
var used []string
var err error
switch originVal.Elem().Kind() {
case reflect.Map:
used, err = mapSet(originVal.Elem(), targetVal, variables)
case reflect.Array, reflect.Slice:
used, err = arraySet(originVal.Elem(), targetVal, variables)
case reflect.String:
used, err = stringSet(originVal.Elem(), targetVal, variables)
case reflect.Float64:
err = float64Set(originVal.Elem(), targetVal)
case reflect.Bool:
err = boolSet(originVal.Elem(), targetVal)
default:
err = fmt.Errorf("interface deal kind: %s", originVal.Elem().Kind().String())
log.Error(err)
return nil, err
}
usedVariables = append(usedVariables, used...)
return usedVariables, err
}
func boolSet(originVal reflect.Value, targetVal reflect.Value) error {
if targetVal.Kind() == reflect.Ptr {
targetVal = targetVal.Elem()
}
switch targetVal.Kind() {
case reflect.String:
targetVal.SetString(strconv.FormatBool(originVal.Bool()))
case reflect.Bool:
targetVal.Set(originVal)
default:
return fmt.Errorf("bool set error:%w %s", ErrorUnsupportedKind, targetVal.Kind())
}
return nil
}
func float64Set(originVal reflect.Value, targetVal reflect.Value) error {
if targetVal.Kind() == reflect.Ptr {
targetVal = targetVal.Elem()
}
switch targetVal.Kind() {
case reflect.Int, reflect.Int32, reflect.Int64:
value, err := strconv.ParseInt(fmt.Sprintf("%1.0f", originVal.Float()), 10, 64)
if err != nil {
return err
}
targetVal.SetInt(value)
case reflect.Float64:
targetVal.SetFloat(originVal.Float())
case reflect.String:
value := fmt.Sprintf("%f", originVal.Float())
targetVal.SetString(value)
default:
return fmt.Errorf("float64 set error:%w %s", ErrorUnsupportedKind, targetVal.Kind())
}
return nil
}
func arraySet(originVal reflect.Value, targetVal reflect.Value, variables eosc.IVariable) ([]string, error) {
if originVal.Kind() != reflect.Slice && originVal.Kind() != reflect.Array {
return nil, fmt.Errorf("origin error: %w %s", ErrorUnsupportedKind, originVal.Kind())
}
if targetVal.Kind() == reflect.Ptr {
targetVal = targetVal.Elem()
}
if targetVal.Kind() != reflect.Slice {
return nil, fmt.Errorf("target error %w %s", ErrorUnsupportedKind, targetVal.Kind())
}
usedVariables := make([]string, 0, variables.Len())
newSlice := reflect.MakeSlice(targetVal.Type(), 0, originVal.Cap())
for j := 0; j < originVal.Len(); j++ {
indexValue := originVal.Index(j)
newValue := reflect.New(targetVal.Type().Elem())
used, err := recurseReflect(indexValue, newValue, variables)
if err != nil {
return nil, err
}
usedVariables = append(usedVariables, used...)
newSlice = reflect.Append(newSlice, newValue.Elem())
}
targetVal.Set(newSlice)
return usedVariables, nil
}
func mapSet(originVal reflect.Value, targetVal reflect.Value, variables eosc.IVariable) ([]string, error) {
if originVal.Kind() != reflect.Map {
return nil, fmt.Errorf("map deal %w %s", ErrorUnsupportedKind, originVal.Kind())
}
if targetVal.Kind() == reflect.Ptr {
if !targetVal.Elem().IsValid() {
targetType := targetVal.Type()
newVal := reflect.New(targetType.Elem())
targetVal.Set(newVal)
}
targetVal = targetVal.Elem()
}
usedVariables := make([]string, 0, variables.Len())
switch targetVal.Kind() {
case reflect.Struct:
{
return structSet(originVal, targetVal, variables)
}
case reflect.Map:
{
targetType := targetVal.Type()
newMap := reflect.MakeMap(targetType)
for _, key := range originVal.MapKeys() {
newKey := reflect.New(targetType.Key())
_, err := recurseReflect(key, newKey, variables)
if err != nil {
return nil, err
}
value := originVal.MapIndex(key)
newValue := reflect.New(targetType.Elem())
used, err := recurseReflect(value, newValue, variables)
if err != nil {
return nil, err
}
usedVariables = append(usedVariables, used...)
newMap.SetMapIndex(newKey.Elem(), newValue.Elem())
}
targetVal.Set(newMap)
}
case reflect.Ptr:
{
used, err := mapSet(originVal, targetVal, variables)
if err != nil {
return nil, err
}
usedVariables = append(usedVariables, used...)
}
default:
{
log.Error("type ", targetVal.Type(), " kind ", targetVal.Kind(), " ", originVal, " ", targetVal.Type().Name())
}
}
return usedVariables, nil
}
func structSet(originVal reflect.Value, targetVal reflect.Value, variables eosc.IVariable) ([]string, error) {
usedVariables := make([]string, 0, variables.Len())
targetType := targetVal.Type()
for i := 0; i < targetType.NumField(); i++ {
field := targetType.Field(i)
fieldValue := reflect.New(field.Type)
tag := field.Tag.Get("json")
ts := strings.Split(tag, ",")
value := originVal.MapIndex(reflect.ValueOf(ts[0]))
used, err := recurseReflect(value, fieldValue, variables)
if err != nil {
return nil, err
}
usedVariables = append(usedVariables, used...)
if targetVal.Field(i).CanSet() {
targetVal.Field(i).Set(fieldValue.Elem())
}
}
return usedVariables, nil
}