From 9c94725c896169baae57e6ff95d457fcb8d2a23b Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Mon, 5 Aug 2024 13:17:07 +0200 Subject: [PATCH] [confmap] Allow using any type as a string --- ...-string-representation-for-everything.yaml | 26 +++++++++++++++++++ confmap/expand_test.go | 16 ------------ confmap/internal/e2e/types_test.go | 10 +++++++ confmap/provider.go | 2 +- 4 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 .chloggen/mx-psi_add-string-representation-for-everything.yaml diff --git a/.chloggen/mx-psi_add-string-representation-for-everything.yaml b/.chloggen/mx-psi_add-string-representation-for-everything.yaml new file mode 100644 index 00000000000..8abc371f6fc --- /dev/null +++ b/.chloggen/mx-psi_add-string-representation-for-everything.yaml @@ -0,0 +1,26 @@ +# 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: Allow using any YAML structure as a string when loading configuration. + +# One or more tracking issues or pull requests related to the change +issues: [10800] + +# (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: | + Previous to this change, slices could not be used as strings in configuration. + +# 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: [] diff --git a/confmap/expand_test.go b/confmap/expand_test.go index 5b87d747543..211b1254d5b 100644 --- a/confmap/expand_test.go +++ b/confmap/expand_test.go @@ -542,22 +542,6 @@ func TestResolverExpandUnsupportedScheme(t *testing.T) { assert.EqualError(t, err, `scheme "unsupported" is not supported for uri "unsupported:VALUE"`) } -func TestResolverExpandStringValueInvalidReturnValue(t *testing.T) { - provider := newFakeProvider("input", func(context.Context, string, WatcherFunc) (*Retrieved, error) { - return NewRetrievedFromYAML([]byte(`test: "localhost:${test:PORT}"`)) - }) - - testProvider := newFakeProvider("test", func(context.Context, string, WatcherFunc) (*Retrieved, error) { - return NewRetrievedFromYAML([]byte("[1243]")) - }) - - resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, testProvider}, ConverterFactories: nil}) - require.NoError(t, err) - - _, err = resolver.Resolve(context.Background()) - assert.EqualError(t, err, `expanding ${test:PORT}: retrieved value does not have unambiguous string representation: [1243]`) -} - func TestResolverDefaultProviderExpand(t *testing.T) { provider := newFakeProvider("input", func(context.Context, string, WatcherFunc) (*Retrieved, error) { return NewRetrieved(map[string]any{"foo": "${HOST}"}) diff --git a/confmap/internal/e2e/types_test.go b/confmap/internal/e2e/types_test.go index c96847f9c8d..be099ee9bb2 100644 --- a/confmap/internal/e2e/types_test.go +++ b/confmap/internal/e2e/types_test.go @@ -383,6 +383,16 @@ func TestStrictTypeCasting(t *testing.T) { targetField: TargetFieldSlice, expected: []any{"filelog", "windowseventlog/application"}, }, + { + value: `[filelog,windowseventlog/application]`, + targetField: TargetFieldString, + expected: "[filelog,windowseventlog/application]", + }, + { + value: `[filelog,windowseventlog/application]`, + targetField: TargetFieldInlineString, + expected: "inline field with [filelog,windowseventlog/application] expansion", + }, } previousValue := globalgates.StrictlyTypedInputGate.IsEnabled() diff --git a/confmap/provider.go b/confmap/provider.go index b78221d34ae..e1c1973988d 100644 --- a/confmap/provider.go +++ b/confmap/provider.go @@ -152,7 +152,7 @@ func NewRetrievedFromYAML(yamlBytes []byte, opts ...RetrievedOption) (*Retrieved val = string(yamlBytes) } return NewRetrieved(val, append(opts, withStringRepresentation(val))...) - case int, int32, int64, float32, float64, bool, map[string]any: + default: opts = append(opts, withStringRepresentation(string(yamlBytes))) }