Skip to content

Commit

Permalink
[confmap] Allow using any type as a string
Browse files Browse the repository at this point in the history
  • Loading branch information
mx-psi committed Aug 5, 2024
1 parent 6f27297 commit 9c94725
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
26 changes: 26 additions & 0 deletions .chloggen/mx-psi_add-string-representation-for-everything.yaml
Original file line number Diff line number Diff line change
@@ -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: []
16 changes: 0 additions & 16 deletions confmap/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}"})
Expand Down
10 changes: 10 additions & 0 deletions confmap/internal/e2e/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion confmap/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}

Expand Down

0 comments on commit 9c94725

Please sign in to comment.