This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
marshal.go
221 lines (187 loc) · 5.7 KB
/
marshal.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
package plugin
import (
"errors"
"fmt"
"reflect"
. "github.com/foxis/EasyRobot/pkg/core/logger"
)
var ErrNOP = errors.New("No op")
func MarshalOptions(opts interface{}) map[string]interface{} {
config := make(map[string]interface{})
marshalOptions(opts, config)
return config
}
func marshalOptions(opts interface{}, config map[string]interface{}) {
rType := reflect.TypeOf(opts)
rValue := reflect.ValueOf(opts)
for i := 0; i < rType.NumField(); i++ {
field := rType.Field(i)
fieldName, ok := field.Tag.Lookup("opts")
if !ok {
continue
}
if !rValue.Field(i).CanInterface() {
continue
}
val := rValue.Field(i).Interface()
if field.Name == "Base" {
marshalOptions(val, config)
continue
}
if fieldName == "" {
fieldName = field.Name
}
if field.Type.Kind() == reflect.Struct {
tmp := make(map[string]interface{})
marshalOptions(val, tmp)
config[fieldName] = tmp
continue
}
config[fieldName] = val
}
}
// Thanks to dave
// https://stackoverflow.com/questions/26744873/converting-map-to-struct/26746461
func setField(obj interface{}, name string, value interface{}) error {
structValue := reflect.ValueOf(obj).Elem()
structType := reflect.TypeOf(obj).Elem()
structFieldValue := structValue.FieldByNameFunc(func(s string) bool {
if structFieldType, ok := structType.FieldByName(s); ok {
if fieldName, ok := structFieldType.Tag.Lookup("opts"); ok {
if fieldName == name {
return true
}
}
}
if name == s {
return true
}
return false
})
if !structFieldValue.IsValid() {
return fmt.Errorf("No such field: %s in obj", name)
}
if !structFieldValue.CanSet() {
return fmt.Errorf("Cannot set %s field value", name)
}
structFieldType := structFieldValue.Type()
if structFieldType, ok := structType.FieldByName(name); ok {
_, ok := structFieldType.Tag.Lookup("opts")
if !ok {
return fmt.Errorf("No tag set: %s", name)
}
}
val := reflect.ValueOf(value)
if value == nil {
return nil
}
if structFieldType != val.Type() {
var err error
val, err = convertFieldValue(name, structFieldValue, structFieldType, val)
if err != nil {
return err
}
}
structFieldValue.Set(val)
return nil
}
func convertFieldValue(name string, structFieldValue reflect.Value, structFieldType reflect.Type, val reflect.Value) (reflect.Value, error) {
if val.Type().ConvertibleTo(structFieldType) {
return val.Convert(structFieldType), nil
}
switch structFieldType.Kind() {
case reflect.Map:
return convertMap(name, structFieldValue, structFieldType, val)
case reflect.Slice:
return convertArray(name, structFieldValue, structFieldType, val)
case reflect.Array:
Log.Warn().Str("name", name).Msg("Array not supported")
case reflect.Struct:
return convertStruct(name, structFieldValue, structFieldType, val)
}
return reflect.Value{}, fmt.Errorf("Provided value type didn't match obj field type: %s: %s v.s. %s", name, fmt.Sprint(structFieldType), fmt.Sprint(val.Type()))
}
func convertMap(name string, structFieldValue reflect.Value, structFieldType reflect.Type, val reflect.Value) (reflect.Value, error) {
m, ok := val.Interface().(map[string]interface{})
if !ok {
return reflect.Value{}, fmt.Errorf("Not a map data: %s", name)
}
if structFieldType.Key().Kind() != reflect.String {
return reflect.Value{}, fmt.Errorf("Map keys must be string: %s", name)
}
mapVal := reflect.MakeMapWithSize(structFieldType, len(m))
elemType := structFieldType.Elem()
for k, v := range m {
rv := reflect.ValueOf(v)
if reflect.TypeOf(v) != elemType {
if rv.Type().ConvertibleTo(elemType) {
rv = rv.Convert(elemType)
} else {
Log.Warn().Str("name", name).Str("key", k).Str("Elem type", fmt.Sprint(structFieldType)).Str("Value type", fmt.Sprint(reflect.TypeOf(v))).Msg("Slice item")
}
}
mapVal.SetMapIndex(reflect.ValueOf(k), rv)
}
return mapVal, nil
}
func convertArray(name string, structFieldValue reflect.Value, structFieldType reflect.Type, val reflect.Value) (reflect.Value, error) {
arr, ok := val.Interface().([]interface{})
if !ok {
return reflect.Value{}, fmt.Errorf("Not an array data: %s", name)
}
arrVal := reflect.MakeSlice(structFieldType, len(arr), len(arr))
elemType := structFieldType.Elem()
for i, v := range arr {
a := arrVal.Index(i)
rv := reflect.ValueOf(v)
if reflect.TypeOf(v) != a.Type() {
var err error
rv, err = convertFieldValue(name, a, elemType, rv)
if err != nil {
Log.Warn().Str("name", name).Int("index", i).Str("Elem type", fmt.Sprint(structFieldType)).Str("Value type", fmt.Sprint(reflect.TypeOf(v))).Msg("Slice item")
continue
}
}
a.Set(rv)
}
return arrVal, nil
}
func convertStruct(name string, structFieldValue reflect.Value, structFieldType reflect.Type, val reflect.Value) (reflect.Value, error) {
if !val.CanInterface() {
return reflect.Value{}, fmt.Errorf("Field cannot be interfaced: %s", name)
}
m, ok := val.Interface().(map[string]interface{})
if !ok {
return reflect.Value{}, fmt.Errorf("Not a map data: %s", name)
}
for k, v := range m {
setField(structFieldValue.Addr().Interface(), k, v)
// if err != nil {
// Log.Error().Err(err).Str("key", k).Msg("setField")
// }
}
return reflect.Value{}, ErrNOP
}
func fillStruct(s interface{}, m map[string]interface{}) error {
rv := reflect.ValueOf(s)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return errors.New("Options must be a pointer")
}
for k, v := range m {
setField(s, k, v)
// if err != nil {
// Log.Error().Err(err).Str("key", k).Msg("setField")
// }
}
base := rv.Elem().FieldByName("Base")
if !base.CanAddr() {
return nil
}
for k, v := range m {
setField(base.Addr().Interface(), k, v)
// if err != nil {
// Log.Error().Err(err).Str("key", k).Msg("setField")
// }
}
return nil
}