Skip to content

Commit e9f3dec

Browse files
authored
[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
1 parent bd2f000 commit e9f3dec

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

.chloggen/mx-psi_decodenil.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: confmap
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Support running Unmarshal hooks on nil values.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12981]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

confmap/confmap.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ func decodeConfig(m *Conf, result any, errorUnused bool, skipTopLevelUnmarshaler
235235
TagName: MapstructureTag,
236236
WeaklyTypedInput: false,
237237
MatchName: caseSensitiveMatchName,
238+
DecodeNil: true,
238239
DecodeHook: composehook.ComposeDecodeHookFunc(
239240
useExpandValue(),
240241
expandNilStructPointersHookFunc(),
@@ -582,10 +583,7 @@ type Marshaler interface {
582583
func zeroSliceHookFunc() mapstructure.DecodeHookFuncValue {
583584
return safeWrapDecodeHookFunc(func(from reflect.Value, to reflect.Value) (any, error) {
584585
if to.CanSet() && to.Kind() == reflect.Slice && from.Kind() == reflect.Slice {
585-
if from.IsNil() {
586-
// input slice is nil, set output slice to nil.
587-
to.Set(reflect.Zero(to.Type()))
588-
} else {
586+
if !from.IsNil() {
589587
// input slice is not nil, set the output slice to a new slice of the same type.
590588
to.Set(reflect.MakeSlice(to.Type(), from.Len(), from.Cap()))
591589
}

0 commit comments

Comments
 (0)