Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[confmap][fix] Correctly differentiate between nil and empty slices in ToStringMap #11755

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/nil-empty-slice.yaml
Original file line number Diff line number Diff line change
@@ -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: 'bug_fix'

# 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: "Correctly differentiate between a nil and empty slice in `ToStringMap`"

# One or more tracking issues or pull requests related to the change
issues: [11749]

# (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]
4 changes: 4 additions & 0 deletions confmap/confmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func sanitizeExpanded(a any, useOriginal bool) any {
return c
case []any:
var newSlice []any
if m == nil {
return newSlice
}
Comment on lines +136 to +138
Copy link
Contributor Author

@mahadzaryab1 mahadzaryab1 Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mx-psi This block is currently not covered by tests because we're not decoding nil values in this PR (that will happen in #11734). We could (1) remove this if block here and add it in the following PR; (2) leave it in here but merge with missing code coverage; (3) add a unit test for this function directly. Let me know what you think.

newSlice = []any{}
for _, e := range m {
newSlice = append(newSlice, sanitizeExpanded(e, useOriginal))
}
Expand Down
9 changes: 7 additions & 2 deletions confmap/confmaptest/configtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ func TestLoadConf(t *testing.T) {
assert.Equal(t, map[string]any{"floating": 3.14}, cfg.ToStringMap())
}

func TestToStringMapSanitizeNil(t *testing.T) {
cfg, err := LoadConf(filepath.Join("testdata", "nil.yaml"))
require.NoError(t, err)
assert.Equal(t, map[string]any{"slice": nil}, cfg.ToStringMap())
}

func TestToStringMapSanitizeEmptySlice(t *testing.T) {
cfg, err := LoadConf(filepath.Join("testdata", "empty-slice.yaml"))
require.NoError(t, err)
var nilSlice []interface{}
assert.Equal(t, map[string]any{"slice": nilSlice}, cfg.ToStringMap())
assert.Equal(t, map[string]any{"slice": []interface{}{}}, cfg.ToStringMap())
}

func TestValidateProviderScheme(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion confmap/confmaptest/testdata/empty-slice.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
slice: [] # empty slices are sanitized to nil in ToStringMap
slice: []
1 change: 1 addition & 0 deletions confmap/confmaptest/testdata/nil.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
slice:
Loading