Skip to content

Commit

Permalink
Enable Nil Decoding
Browse files Browse the repository at this point in the history
Signed-off-by: Mahad Zaryab <[email protected]>
  • Loading branch information
mahadzaryab1 committed Nov 23, 2024
1 parent f74890a commit 75840e6
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion confmap/confmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 75840e6

Please sign in to comment.