[confmap] Enable `DecodeNil` option (#12996)

<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

<!-- Issue number if applicable -->

Enables `DecoderConfig.DecodeNil` 


#### Link to tracking issue

Taken from #10260
Updates #12981
This commit is contained in:
Pablo Baeyens 2025-05-13 10:12:01 +02:00 committed by GitHub
parent bd2f00044c
commit e9f3dec3c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 4 deletions

View File

@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: confmap
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Support running Unmarshal hooks on nil values.
# One or more tracking issues or pull requests related to the change
issues: [12981]
# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]

View File

@ -235,6 +235,7 @@ func decodeConfig(m *Conf, result any, errorUnused bool, skipTopLevelUnmarshaler
TagName: MapstructureTag,
WeaklyTypedInput: false,
MatchName: caseSensitiveMatchName,
DecodeNil: true,
DecodeHook: composehook.ComposeDecodeHookFunc(
useExpandValue(),
expandNilStructPointersHookFunc(),
@ -582,10 +583,7 @@ type Marshaler interface {
func zeroSliceHookFunc() mapstructure.DecodeHookFuncValue {
return safeWrapDecodeHookFunc(func(from reflect.Value, to reflect.Value) (any, error) {
if to.CanSet() && to.Kind() == reflect.Slice && from.Kind() == reflect.Slice {
if from.IsNil() {
// input slice is nil, set output slice to nil.
to.Set(reflect.Zero(to.Type()))
} else {
if !from.IsNil() {
// input slice is not nil, set the output slice to a new slice of the same type.
to.Set(reflect.MakeSlice(to.Type(), from.Len(), from.Cap()))
}