-
Notifications
You must be signed in to change notification settings - Fork 22
/
configuration.go
69 lines (56 loc) · 1.69 KB
/
configuration.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
package main
import (
"fmt"
"reflect"
"strings"
)
func newConfiguration(fileName, name, receiver, featuresStr, additionalFieldsStr string) any {
// add all enabled features to a map
features := make(map[string]bool)
for _, feature := range strings.Split(featuresStr, ",") {
features[feature] = true
}
additionalFields := make(map[string]string)
for _, additionalField := range strings.Split(additionalFieldsStr, ",") {
if len(additionalField) == 0 {
continue
}
values := strings.Split(additionalField, "=")
if len(values) != 2 {
panic(fmt.Sprintf("failed to parse format of additional field, should be \"FieldName=FieldValue\", got \"%s\"", additionalField))
}
additionalFields[values[0]] = values[1]
}
// create a dynamic configuration struct
structData := map[string]any{
"FileName": fileName,
"Name": name,
"Receiver": strings.ToLower(receiver),
"Features": features,
}
// add the additional fields
for key, value := range additionalFields {
structData[key] = value
}
// gather all struct fields
structFields := make([]reflect.StructField, 0, len(structData))
for key, value := range structData {
structFields = append(structFields, reflect.StructField{
Name: key,
Type: reflect.TypeOf(value),
})
}
// create a struct type at runtime
structType := reflect.StructOf(structFields)
// create a new struct instance using reflection
newStruct := reflect.New(structType).Elem()
// set the values of the dynamic fields
for i := range newStruct.NumField() {
field := newStruct.Field(i)
fieldName := structType.Field(i).Name
if value, ok := structData[fieldName]; ok {
field.Set(reflect.ValueOf(value))
}
}
return newStruct.Interface()
}