config: handle empty configurations

This commit is contained in:
Vincent Bernat
2022-07-08 09:17:10 +02:00
parent 328d11158e
commit 15fc92bfe3
2 changed files with 39 additions and 1 deletions

View File

@@ -188,7 +188,12 @@ func DefaultHook() (mapstructure.DecodeHookFunc, func()) {
// We already have a pointer
method, ok := to.Type().MethodByName("Reset")
if !ok {
return from.Interface(), nil
// We may have a pointer to a pointer when totally empty.
to = to.Elem()
method, ok = to.Type().MethodByName("Reset")
if !ok {
return from.Interface(), nil
}
}
if to.IsNil() {
new := reflect.New(to.Type().Elem())

View File

@@ -427,3 +427,36 @@ modules:
})
})
}
func TestDevNullDefault(t *testing.T) {
c := cmd.ConfigRelatedOptions{
Path: "/dev/null",
Dump: true,
}
var parsed dummyConfiguration
out := bytes.NewBuffer([]byte{})
if err := c.Parse(out, "dummy", &parsed); err != nil {
t.Fatalf("Parse() error:\n%+v", err)
}
// Expected configuration
expected := dummyConfiguration{
Module1: dummyModule1Configuration{
Listen: "127.0.0.1:8080",
Topic: "nothingness",
Workers: 100,
},
Module2: dummyModule2Configuration{
MoreDetails: MoreDetails{
Stuff: "hello",
},
Details: dummyModule2DetailsConfiguration{
Workers: 1,
IntervalValue: time.Minute,
},
},
}
if diff := helpers.Diff(parsed, expected); diff != "" {
t.Errorf("Parse() (-got, +want):\n%s", diff)
}
}