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

[chore][confmap] Do not merge: demonstrate empty slice handling #11882

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
36 changes: 4 additions & 32 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
}
newSlice = []any{}
for _, e := range m {
newSlice = append(newSlice, sanitizeExpanded(e, useOriginal))
}
Expand Down Expand Up @@ -219,7 +223,6 @@ func decodeConfig(m *Conf, result any, errorUnused bool, skipTopLevelUnmarshaler
// after the main unmarshaler hook is called,
// we unmarshal the embedded structs if present to merge with the result:
unmarshalerEmbeddedStructsHookFunc(),
zeroSliceHookFunc(),
),
}
decoder, err := mapstructure.NewDecoder(dc)
Expand Down Expand Up @@ -513,37 +516,6 @@ type Marshaler interface {
Marshal(component *Conf) error
}

// This hook is used to solve the issue: https://github.com/open-telemetry/opentelemetry-collector/issues/4001
// We adopt the suggestion provided in this issue: https://github.com/mitchellh/mapstructure/issues/74#issuecomment-279886492
// We should empty every slice before unmarshalling unless user provided slice is nil.
// Assume that we had a struct with a field of type slice called `keys`, which has default values of ["a", "b"]
//
// type Config struct {
// Keys []string `mapstructure:"keys"`
// }
//
// The configuration provided by users may have following cases
// 1. configuration have `keys` field and have a non-nil values for this key, the output should be overridden
// - for example, input is {"keys", ["c"]}, then output is Config{ Keys: ["c"]}
//
// 2. configuration have `keys` field and have an empty slice for this key, the output should be overridden by empty slices
// - for example, input is {"keys", []}, then output is Config{ Keys: []}
//
// 3. configuration have `keys` field and have nil value for this key, the output should be default config
// - for example, input is {"keys": nil}, then output is Config{ Keys: ["a", "b"]}
//
// 4. configuration have no `keys` field specified, the output should be default config
// - for example, input is {}, then output is Config{ Keys: ["a", "b"]}
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()))
}

return from.Interface(), nil
}
}

type moduleFactory[T any, S any] interface {
Create(s S) T
}
Expand Down
44 changes: 44 additions & 0 deletions confmap/confmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,50 @@ func TestZeroSliceHookFunc(t *testing.T) {
}
}

func TestNilValuesUnchanged(t *testing.T) {
type structWithSlices struct {
Strings []string `mapstructure:"strings"`
}

slicesStruct := &structWithSlices{}

nilCfg := map[string]any{
"strings": []any(nil),
}
nilConf := NewFromStringMap(nilCfg)
err := nilConf.Unmarshal(slicesStruct)
require.NoError(t, err)

confFromStruct := New()
err = confFromStruct.Marshal(slicesStruct)
require.NoError(t, err)

require.Equal(t, nilCfg, nilConf.ToStringMap())
require.EqualValues(t, nilConf.ToStringMap(), confFromStruct.ToStringMap())
}

func TestEmptySliceUnchanged(t *testing.T) {
type structWithSlices struct {
Strings []string `mapstructure:"strings"`
}

slicesStruct := &structWithSlices{}

nilCfg := map[string]any{
"strings": []any{},
}
nilConf := NewFromStringMap(nilCfg)
err := nilConf.Unmarshal(slicesStruct)
require.NoError(t, err)

confFromStruct := New()
err = confFromStruct.Marshal(slicesStruct)
require.NoError(t, err)

require.Equal(t, nilCfg, nilConf.ToStringMap())
require.EqualValues(t, nilConf.ToStringMap(), confFromStruct.ToStringMap())
}

type C struct {
Modifiers []string `mapstructure:"modifiers"`
}
Expand Down
11 changes: 8 additions & 3 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 TestToStringMapSanitizeEmptySlice(t *testing.T) {
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 TestToStringMapEmptySlice(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": []any{}}, 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:
3 changes: 3 additions & 0 deletions confmap/internal/mapstructure/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ func (e *Encoder) encodeSlice(value reflect.Value) (any, error) {
Kind: value.Kind(),
}
}
if value.IsNil() {
return []any(nil), nil
}
result := make([]any, value.Len())
for i := 0; i < value.Len(); i++ {
var err error
Expand Down
Loading