-
Notifications
You must be signed in to change notification settings - Fork 4
/
cast.go
360 lines (310 loc) · 12.3 KB
/
cast.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package config
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
"github.com/pkg/errors"
"github.com/spf13/cast"
)
func isFieldExported(typeOfField reflect.StructField) bool {
return typeOfField.PkgPath == ""
}
// castToPrimitive supports casting of primitive types (such as int, string,...) to the given target type.
func castToPrimitive(rawValue interface{}, targetType reflect.Type) (interface{}, error) {
typeOfValue := reflect.TypeOf(rawValue)
if targetType == reflect.TypeOf(time.Second) {
dur, err := cast.ToDurationE(rawValue)
if err != nil {
return nil, errors.Wrapf(err, "Casting '%v' to duration", rawValue)
}
rawValue = dur
typeOfValue = reflect.TypeOf(rawValue)
}
isConvertible := typeOfValue.ConvertibleTo(targetType)
if isConvertible {
return reflect.ValueOf(rawValue).Convert(targetType).Interface(), nil
}
// last resort, do a simple cast
return castSimple(rawValue, targetType)
}
func castSimple(rawValue interface{}, targetType reflect.Type) (interface{}, error) {
switch targetType {
case reflect.TypeOf(string("")):
return cast.ToStringE(rawValue)
case reflect.TypeOf(bool(false)):
return cast.ToBoolE(rawValue)
case reflect.TypeOf(float32(0)):
return cast.ToFloat32E(rawValue)
case reflect.TypeOf(float64(0)):
return cast.ToFloat64E(rawValue)
case reflect.TypeOf(int(0)):
return cast.ToIntE(rawValue)
case reflect.TypeOf(int8(0)):
return cast.ToInt8E(rawValue)
case reflect.TypeOf(int16(0)):
return cast.ToInt16E(rawValue)
case reflect.TypeOf(int32(0)):
return cast.ToInt32E(rawValue)
case reflect.TypeOf(int64(0)):
return cast.ToInt64E(rawValue)
case reflect.TypeOf(uint(0)):
return cast.ToUintE(rawValue)
case reflect.TypeOf(uint8(0)):
return cast.ToUint8E(rawValue)
case reflect.TypeOf(uint16(0)):
return cast.ToUint16E(rawValue)
case reflect.TypeOf(uint32(0)):
return cast.ToUint32E(rawValue)
case reflect.TypeOf(uint64(0)):
return cast.ToUint64E(rawValue)
}
return nil, fmt.Errorf("Can't cast %T to %v (not yet supported).", rawValue, targetType)
}
// castToStruct supports casting of structs (also nested) to the given target type.
func castToStruct(rawValue interface{}, targetType reflect.Type) (interface{}, error) {
if targetType.Kind() != reflect.Struct {
return nil, fmt.Errorf("Can't cast to struct since the target type is not a struct. Instead it is %v", targetType)
}
parsedDefinitionCastedToMap, ok := rawValue.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Unable to cast %v (type=%T) to %v. Type must be map[string]interface{}", rawValue, rawValue, targetType)
}
castedToTargetType, err := createAndFillStruct(targetType, parsedDefinitionCastedToMap)
if err != nil {
return nil, errors.Wrap(err, "Handling default value for element in a slice of structs")
}
return castedToTargetType.Interface(), nil
}
// castToSlice supports casting of slices (of primitives and structs) to the given target type.
func castToSlice(rawValue interface{}, targetType reflect.Type) (interface{}, error) {
if targetType.Kind() != reflect.Slice {
return nil, fmt.Errorf("Can't cast to slice since the target type is not a slice. Instead it is %v", targetType)
}
typeOfRawValue := reflect.TypeOf(rawValue)
if typeOfRawValue.Kind() != reflect.Slice {
return nil, fmt.Errorf("Can't cast to slice since the given raw value is no slice. Instead it is %v", typeOfRawValue)
}
sliceValue := reflect.ValueOf(rawValue)
// obtain the type of the slices elements
elementType := targetType.Elem()
sliceInTargetType := reflect.MakeSlice(targetType, 0, sliceValue.Len())
for i := 0; i < sliceValue.Len(); i++ {
rawDefaultValueElement := sliceValue.Index(i).Interface()
switch castedRawElement := rawDefaultValueElement.(type) {
case map[string]interface{}:
// handles structs
castedToTargetType, err := createAndFillStruct(elementType, castedRawElement)
if err != nil {
return nil, errors.Wrap(err, "Handling default value for element in a slice of structs")
}
sliceInTargetType = reflect.Append(sliceInTargetType, castedToTargetType)
default:
// handles primitive elements (int, string, ...)
castedToTargetType, err := castToPrimitive(rawDefaultValueElement, elementType)
if err != nil {
return nil, errors.Wrap(err, "Casting default value to element type")
}
sliceInTargetType = reflect.Append(sliceInTargetType, reflect.ValueOf(castedToTargetType))
}
}
return sliceInTargetType.Interface(), nil
}
// castToTargetType casts the given raw value to the given target type.
func castToTargetType(rawUntypedValue interface{}, targetType reflect.Type) (interface{}, error) {
// Try to cast immediately if possible (if types match)
typeOfValue := reflect.TypeOf(rawUntypedValue)
isConvertible := typeOfValue.ConvertibleTo(targetType)
if isConvertible {
return reflect.ValueOf(rawUntypedValue).Convert(targetType).Interface(), nil
}
switch targetType.Kind() {
case reflect.Struct:
return castToStruct(rawUntypedValue, targetType)
case reflect.Slice:
return castToSlice(rawUntypedValue, targetType)
default:
return castToPrimitive(rawUntypedValue, targetType)
}
}
func getConfigTagDefinition(fieldDeclaration reflect.StructField) (string, bool) {
return fieldDeclaration.Tag.Lookup("cfg")
}
// createAndFillStruct creates a struct based on the given type and fills its fields based on the given data.
// For being able to fill the struct the given datas keys have to match the config tags that are defined on the target type.
//
// e.g. for type
//
// type my struct {
// Field1 string `cfg:"{'name':'field_1'}"`
// }
//
// the data map should contain an entry with name 'field_1'
//
// data := map[string]interface{}{"field_1":"a value"}
func createAndFillStruct(targetTypeOfStruct reflect.Type, data map[string]interface{}) (reflect.Value, error) {
if targetTypeOfStruct.Kind() != reflect.Struct {
return reflect.Zero(targetTypeOfStruct), fmt.Errorf("The target type must be a struct")
}
newStruct := reflect.New(targetTypeOfStruct)
newStructValue := newStruct.Elem()
for i := 0; i < targetTypeOfStruct.NumField(); i++ {
fieldDeclaration := targetTypeOfStruct.Field(i)
fieldValue := newStructValue.FieldByName(fieldDeclaration.Name)
fieldType := fieldDeclaration.Type
configTag, hasConfig := getConfigTagDefinition(fieldDeclaration)
if !hasConfig {
continue
}
entry, err := parseConfigTagDefinition(configTag, fieldType, "")
if err != nil {
return reflect.Zero(targetTypeOfStruct), errors.Wrapf(err, "Parsing configTag '%s'", configTag)
}
val, ok := data[entry.Name]
if !ok && entry.IsRequired() {
return reflect.Zero(targetTypeOfStruct), fmt.Errorf("Missing value for required field (struct-field='%s',expected-key='%s')", fieldDeclaration.Name, entry.Name)
}
if !ok {
// take the default value
val = entry.Def
}
// cast the parsed default value to the target type
castedToTargetTypeIf, err := castToTargetType(val, fieldType)
if err != nil {
return reflect.Zero(targetTypeOfStruct), errors.Wrapf(err, "Casting to target type")
}
castedToTargetType := reflect.ValueOf(castedToTargetTypeIf)
// ensure that the casted value can be set
if !isFieldExported(fieldDeclaration) {
return reflect.Zero(targetTypeOfStruct), fmt.Errorf("Can't set value for unexported field (struct-field='%s',key='%s').", fieldDeclaration.Name, entry.Name)
}
if !fieldValue.CanSet() {
return reflect.Zero(targetTypeOfStruct), fmt.Errorf("Can't set value for field (struct-field='%s',key='%s').", fieldDeclaration.Name, entry.Name)
}
fieldValue.Set(castedToTargetType)
}
return newStructValue, nil
}
func fullFieldName(nameOfParent string, fieldName string) string {
if len(nameOfParent) == 0 {
return fieldName
}
return fmt.Sprintf("%s.%s", nameOfParent, fieldName)
}
func parseStringContainingSliceOfX(sliceString string, targetSliceType reflect.Type) ([]interface{}, error) {
sliceString = strings.ReplaceAll(sliceString, "'", `"`)
slice := reflect.MakeSlice(targetSliceType, 0, 0).Interface()
err := json.Unmarshal([]byte(sliceString), &slice)
if err != nil {
return nil, errors.Wrapf(err, "Parsing string that contains a slice of %v", targetSliceType)
}
if reflect.TypeOf(slice).Kind() != reflect.Slice {
return nil, fmt.Errorf("Unmarshalling did not produce the expected slice type instead it produced '%T'", slice)
}
return slice.([]interface{}), nil
}
// handleViperWorkarounds viper does not handle all types correctly. e.g. a slice of structs or booleans is not supported and just returned as
// a jsonstring. handleViperWorkarounds casts those jsonstrings into the correct golang types.
func handleViperWorkarounds(val interface{}, targetType reflect.Type, hasMappingfunc bool) (interface{}, error) {
if val == nil {
return val, nil
}
if hasMappingfunc {
return val, nil
}
typeOfValue := reflect.TypeOf(val)
// immediately return / do nothing in case we have no string
if typeOfValue.Kind() != reflect.String {
return val, nil
}
// immediately return / do nothing in case the target type is no slice
if targetType.Kind() != reflect.Slice {
return val, nil
}
valueAsString, err := cast.ToStringE(val)
if err != nil {
return nil, errors.Wrapf(err, "Casting %v (type=%T) to string", val, val)
}
return parseStringContainingSliceOfX(valueAsString, targetType)
}
// handleYamlElementListInput ensures that the value is coming in as an yaml element list are in the right format because usually they are given as []interface{}.
// Internally they are converted to a json string.
func handleYamlElementListInput(val interface{}, targetType reflect.Type) (interface{}, error) {
if val == nil {
return val, nil
}
typeOfValue := reflect.TypeOf(val)
// do nothing if the target type is no slice
if targetType.Kind() != reflect.Slice {
return val, nil
}
// do nothing if the given type is no slice
if typeOfValue.Kind() != reflect.Slice {
return val, nil
}
targetTypeElementKind := targetType.Elem().Kind()
valueTypeElementKind := typeOfValue.Elem().Kind()
// do nothing if the element types match
if targetTypeElementKind == valueTypeElementKind {
return val, nil
}
return cfgValueToStructuredString(val)
}
// cfgValueToStructuredString handles structured config values that are either valid json or yaml element lists.
// The function returns a json string that represents the input data.
func cfgValueToStructuredString(cfgValue interface{}) (string, error) {
cfgValueAsStr := ""
// We have to handle two cases.
// 1. The config parameter was provided as one string (e.g. via env-parameter or via command-line).
// 2. The config parameter was provided via yaml as a list of objects (will be type []interface{})
switch typedIface := cfgValue.(type) {
case string:
// This handles the case that config parameter was provided as one string.
// example [{'key1':'value1','key2':'value2'}]
cfgValueAsStr = cfgValue.(string)
case []interface{}:
// This handles the case that config parameter was provided via yaml as a list of objects.
return yamlElementListToJsonString(typedIface)
default:
return "", fmt.Errorf("Unable to handle parameter of type %T", cfgValue)
}
return cfgValueAsStr, nil
}
// yamlElementListToJsonString converts contents of a yaml list into a json string
func yamlElementListToJsonString(yamlData []interface{}) (string, error) {
// This handles the case that config parameter was provided via yaml as a list of objects.
// example :
// my-config:
// - key1: "value1"
// key2: 111
// - key1: "value2"
// key2: 222
pairList := make([]string, 0)
for _, element := range yamlData {
pair := ""
switch castedElement := element.(type) {
case map[string]string:
internalPair, err := json.Marshal(castedElement)
if err != nil {
return "", errors.Wrap(err, "Handling map[string]string")
}
pair = string(internalPair)
case map[string]interface{}, map[interface{}]interface{}:
cElement, err := cast.ToStringMapE(element)
if err != nil {
return "", errors.Wrap(err, "Casting map[string]interface{}")
}
internalPair, err := json.Marshal(cElement)
if err != nil {
return "", errors.Wrap(err, "Handling map[string]interface{}")
}
pair = string(internalPair)
default:
return "", fmt.Errorf("Unable to handle element %#v of type %T", element, element)
}
pairList = append(pairList, pair)
}
// concatenate all elements to [{'key1':'value1','key2':'value2'}]
return fmt.Sprintf(`[%s]`, strings.Join(pairList, ",")), nil
}