diff --git a/.chloggen/ottl-nested-map-literals.yaml b/.chloggen/ottl-nested-map-literals.yaml new file mode 100644 index 000000000000..80f79eab4432 --- /dev/null +++ b/.chloggen/ottl-nested-map-literals.yaml @@ -0,0 +1,27 @@ +# 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. filelogreceiver) +component: pkg/ottl + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix limitation of map literals within slices not being handled correctly + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [37405] + +# (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: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# 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/pkg/ottl/e2e/e2e_test.go b/pkg/ottl/e2e/e2e_test.go index e803a7bb3092..bcb69fe9a30a 100644 --- a/pkg/ottl/e2e/e2e_test.go +++ b/pkg/ottl/e2e/e2e_test.go @@ -1041,6 +1041,22 @@ func Test_e2e_converters(t *testing.T) { m.PutInt("bar", 5) }, }, + { + statement: `set(attributes["test"], {"list":[{"foo":"bar"}]})`, + want: func(tCtx ottllog.TransformContext) { + m := tCtx.GetLogRecord().Attributes().PutEmptyMap("test") + m2 := m.PutEmptySlice("list").AppendEmpty().SetEmptyMap() + m2.PutStr("foo", "bar") + }, + }, + { + statement: `set(attributes, {"list":[{"foo":"bar"}]})`, + want: func(tCtx ottllog.TransformContext) { + tCtx.GetLogRecord().Attributes().Clear() + m2 := tCtx.GetLogRecord().Attributes().PutEmptySlice("list").AppendEmpty().SetEmptyMap() + m2.PutStr("foo", "bar") + }, + }, } for _, tt := range tests { diff --git a/pkg/ottl/expression.go b/pkg/ottl/expression.go index a2e8d29e63c4..62ba64bc9806 100644 --- a/pkg/ottl/expression.go +++ b/pkg/ottl/expression.go @@ -169,6 +169,7 @@ func (l *listGetter[K]) Get(ctx context.Context, tCtx K) (any, error) { if err != nil { return nil, err } + evaluated[i] = val } @@ -180,23 +181,37 @@ type mapGetter[K any] struct { } func (m *mapGetter[K]) Get(ctx context.Context, tCtx K) (any, error) { - evaluated := map[string]any{} + result := pcommon.NewMap() for k, v := range m.mapValues { val, err := v.Get(ctx, tCtx) if err != nil { return nil, err } - switch t := val.(type) { + switch typedVal := val.(type) { case pcommon.Map: - evaluated[k] = t.AsRaw() + target := result.PutEmpty(k).SetEmptyMap() + typedVal.CopyTo(target) + case []any: + target := result.PutEmpty(k).SetEmptySlice() + for _, el := range typedVal { + switch typedEl := el.(type) { + case pcommon.Map: + m := target.AppendEmpty().SetEmptyMap() + typedEl.CopyTo(m) + default: + err := target.AppendEmpty().FromRaw(el) + if err != nil { + return nil, err + } + } + } default: - evaluated[k] = t + err := result.PutEmpty(k).FromRaw(val) + if err != nil { + return nil, err + } } } - result := pcommon.NewMap() - if err := result.FromRaw(evaluated); err != nil { - return nil, err - } return result, nil } diff --git a/pkg/ottl/expression_test.go b/pkg/ottl/expression_test.go index b215a79e9e07..6571ea507bee 100644 --- a/pkg/ottl/expression_test.go +++ b/pkg/ottl/expression_test.go @@ -641,6 +641,16 @@ func Test_newGetter(t *testing.T) { Int: ottltest.Intp(1), }, }, + { + Map: &mapValue{ + Values: []mapItem{ + { + Key: ottltest.Strp("stringAttr"), + Value: &value{String: ottltest.Strp("value")}, + }, + }, + }, + }, }, }, }, @@ -660,7 +670,7 @@ func Test_newGetter(t *testing.T) { "foo": map[string]any{ "test": "value", }, - "listAttr": []any{"test0", int64(1)}, + "listAttr": []any{"test0", int64(1), map[string]any{"stringAttr": "value"}}, }, "stringAttr": "value", "intAttr": int64(3), diff --git a/pkg/ottl/parser.go b/pkg/ottl/parser.go index ad42f9e0b327..f0a5ff665b96 100644 --- a/pkg/ottl/parser.go +++ b/pkg/ottl/parser.go @@ -12,6 +12,7 @@ import ( "github.com/alecthomas/participle/v2" "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/pdata/pcommon" "go.uber.org/zap" ) @@ -480,6 +481,19 @@ func (p *Parser[K]) ParseValueExpression(raw string) (*ValueExpression[K], error } return &ValueExpression[K]{ - getter: getter, + getter: &StandardGetSetter[K]{ + Getter: func(ctx context.Context, tCtx K) (any, error) { + val, err := getter.Get(ctx, tCtx) + if err != nil { + return nil, err + } + switch v := val.(type) { + case pcommon.Map: + return v.AsRaw(), nil + default: + return v, nil + } + }, + }, }, nil } diff --git a/pkg/ottl/parser_test.go b/pkg/ottl/parser_test.go index e7ef0ccbfc7d..9d57462a132e 100644 --- a/pkg/ottl/parser_test.go +++ b/pkg/ottl/parser_test.go @@ -16,7 +16,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component/componenttest" - "go.opentelemetry.io/collector/pdata/pcommon" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottltest" ) @@ -2209,11 +2208,9 @@ func Test_parseValueExpression_full(t *testing.T) { name: "map", valueExpression: `{"map": 1}`, expected: func() any { - m := pcommon.NewMap() - _ = m.FromRaw(map[string]any{ - "map": 1, - }) - return m + return map[string]any{ + "map": int64(1), + } }, }, {