From 75840e637803f3980bab09dce7520831bc52f266 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Sat, 23 Nov 2024 14:12:50 -0500 Subject: [PATCH] Enable Nil Decoding Signed-off-by: Mahad Zaryab --- confmap/confmap.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/confmap/confmap.go b/confmap/confmap.go index a7a99d10352..e43a45f108a 100644 --- a/confmap/confmap.go +++ b/confmap/confmap.go @@ -221,6 +221,7 @@ func decodeConfig(m *Conf, result any, errorUnused bool, skipTopLevelUnmarshaler unmarshalerEmbeddedStructsHookFunc(), zeroSliceHookFunc(), ), + DecodeNil: true, } decoder, err := mapstructure.NewDecoder(dc) if err != nil { @@ -537,7 +538,9 @@ type Marshaler interface { func zeroSliceHookFunc() mapstructure.DecodeHookFuncValue { return func(from reflect.Value, to reflect.Value) (interface{}, error) { if to.CanSet() && to.Kind() == reflect.Slice && from.Kind() == reflect.Slice { - to.Set(reflect.MakeSlice(to.Type(), from.Len(), from.Cap())) + if !from.IsNil() { + to.Set(reflect.MakeSlice(to.Type(), from.Len(), from.Cap())) + } } return from.Interface(), nil