Skip to content

Commit

Permalink
Fix config resolver bug (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgdigital authored Nov 12, 2023
1 parent 24f7827 commit b19b224
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
16 changes: 10 additions & 6 deletions internal/boilerplate/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type ResolvedNode struct {
Type reflect.Type
ParentPath []string
PathString string
StructKey string
Value interface{}
ValueRaw interface{}
ValueLabel string
Expand Down Expand Up @@ -103,14 +104,15 @@ func resolveRootNode(
val *validator.Validate,
spec Spec,
) (ResolvedNode, error) {
return resolveStructNode(resolvers, val, []string{}, spec.Key, reflect.ValueOf(spec.DefaultValue))
return resolveStructNode(resolvers, val, []string{}, spec.Key, "", reflect.ValueOf(spec.DefaultValue))
}

func resolveStructNode(
resolvers []configresolver.Resolver,
val *validator.Validate,
parentPath []string,
key string,
structKey string,
value reflect.Value,
) (ResolvedNode, error) {
if value.Type().Kind() != reflect.Struct {
Expand All @@ -125,7 +127,7 @@ func resolveStructNode(
fieldValue := value.FieldByName(field.Name)
switch field.Type.Kind() {
case reflect.Struct:
if structResolved, err := resolveStructNode(resolvers, val, thisPath, fieldKey, fieldValue); err != nil {
if structResolved, err := resolveStructNode(resolvers, val, thisPath, fieldKey, field.Name, fieldValue); err != nil {
return ResolvedNode{}, err
} else {
children[fieldKey] = structResolved
Expand All @@ -152,23 +154,24 @@ func resolveStructNode(
Type: field.Type,
ParentPath: thisPath,
PathString: strings.Join(append(thisPath, fieldKey), "."),
StructKey: field.Name,
Value: rv,
ValueRaw: rv,
ValueLabel: createValueLabel(rv),
DefaultLabel: createValueLabel(dv),
}
}
}
valueMap := make(map[string]interface{})
for k, c := range children {
valueMap[k] = c.ValueRaw
valueMap := make(map[string]interface{}, len(children))
for _, c := range children {
valueMap[c.StructKey] = c.ValueRaw
}
resolvedValue := reflect.New(value.Type())
decodeConfig := &mapstructure.DecoderConfig{
Metadata: nil,
Result: resolvedValue.Interface(),
MatchName: func(mapKey, fieldName string) bool {
return strcase.ToCamel(mapKey) == fieldName
return mapKey == fieldName
},
}
decoder, decoderErr := mapstructure.NewDecoder(decodeConfig)
Expand All @@ -188,6 +191,7 @@ func resolveStructNode(
},
ParentPath: parentPath,
PathString: strings.Join(thisPath, "."),
StructKey: structKey,
IsStruct: true,
Type: value.Type(),
Value: reflect.Indirect(resolvedValue).Interface(),
Expand Down
3 changes: 3 additions & 0 deletions internal/boilerplate/config/configfx/configfx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestConfig(t *testing.T) {
NestedKeyFromConfig string
NestedKeyFromEnv int `validate:"min=1,max=10"`
IntSlice []int
DB int
}
type TestConfig struct {
Foo string `validate:"lowercase"`
Expand All @@ -42,6 +43,7 @@ func TestConfig(t *testing.T) {
return configresolver.NewEnv(map[string]string{
"TEST_DURATION": "2s",
"TEST_NESTED_NESTED_KEY_FROM_ENV": "2",
"TEST_NESTED_DB": "3",
}, configresolver.WithPriority(-10)), nil
},
},
Expand Down Expand Up @@ -76,6 +78,7 @@ func TestConfig(t *testing.T) {
NestedKeyFromConfig: "from_config",
NestedKeyFromEnv: 2,
IntSlice: []int{1, 2, 3},
DB: 3,
},
}, cfg)
shutdownErr := shutdowner.Shutdown()
Expand Down

0 comments on commit b19b224

Please sign in to comment.