diff --git a/.chloggen/ottl-add-pmapgetsetter.yaml b/.chloggen/ottl-add-pmapgetsetter.yaml new file mode 100644 index 0000000000000..1091d1685f517 --- /dev/null +++ b/.chloggen/ottl-add-pmapgetsetter.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: enhancement + +# 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: Add PMapGetSetter interface and StandardPMapGetSetter type. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [39657] + +# (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: [api] diff --git a/pkg/ottl/expression.go b/pkg/ottl/expression.go index 48a21acc2d1a8..3fad75a8bed3e 100644 --- a/pkg/ottl/expression.go +++ b/pkg/ottl/expression.go @@ -406,6 +406,24 @@ func (g StandardFunctionGetter[K]) Get(args Arguments) (Expr[K], error) { return Expr[K]{exprFunc: fn}, nil } +type PMapGetSetter[K any] interface { + Get(ctx context.Context, tCtx K) (pcommon.Map, error) + Set(ctx context.Context, tCtx K, val pcommon.Map) error +} + +type StandardPMapGetSetter[K any] struct { + Getter func(ctx context.Context, tCtx K) (pcommon.Map, error) + Setter func(ctx context.Context, tCtx K, val any) error +} + +func (path StandardPMapGetSetter[K]) Get(ctx context.Context, tCtx K) (pcommon.Map, error) { + return path.Getter(ctx, tCtx) +} + +func (path StandardPMapGetSetter[K]) Set(ctx context.Context, tCtx K, val pcommon.Map) error { + return path.Setter(ctx, tCtx, val) +} + // PMapGetter is a Getter that must return a pcommon.Map. type PMapGetter[K any] interface { // Get retrieves a pcommon.Map value. diff --git a/pkg/ottl/functions.go b/pkg/ottl/functions.go index 30ad36746981b..9715917857d51 100644 --- a/pkg/ottl/functions.go +++ b/pkg/ottl/functions.go @@ -590,6 +590,16 @@ func (p *Parser[K]) buildArg(argVal value, argType reflect.Type) (any, error) { return nil, err } return StandardIntLikeGetter[K]{Getter: arg.Get}, nil + case strings.HasPrefix(name, "PMapGetSetter"): + if argVal.Literal == nil || argVal.Literal.Path == nil { + return nil, errors.New("must be a path") + } + pathGetSetter, err := p.buildGetSetterFromPath(argVal.Literal.Path) + if err != nil { + return nil, err + } + stdMapGetter := StandardPMapGetter[K]{Getter: pathGetSetter.Get} + return StandardPMapGetSetter[K]{Getter: stdMapGetter.Get, Setter: pathGetSetter.Set}, nil case strings.HasPrefix(name, "PMapGetter"): arg, err := p.newGetter(argVal) if err != nil { diff --git a/pkg/ottl/functions_test.go b/pkg/ottl/functions_test.go index 85d853781340a..1ccad9cbaeb54 100644 --- a/pkg/ottl/functions_test.go +++ b/pkg/ottl/functions_test.go @@ -24,6 +24,11 @@ func Test_NewFunctionCall_invalid(t *testing.T) { &errorFunctionArguments{}, functionThatHasAnError, ), + createFactory[any]( + "testing_pmapgetsetter", + &pMapGetSetterArguments{}, + functionWithPMapGetSetter, + ), createFactory[any]( "testing_getsetter", &getSetterArguments{}, @@ -112,6 +117,19 @@ func Test_NewFunctionCall_invalid(t *testing.T) { }, }, }, + { + name: "not accessor (pmap)", + inv: editor{ + Function: "testing_pmapgetsetter", + Arguments: []argument{ + { + Value: value{ + String: ottltest.Strp("not path"), + }, + }, + }, + }, + }, { name: "not accessor", inv: editor{ @@ -391,6 +409,30 @@ func Test_NewFunctionCall_invalid(t *testing.T) { Function: "non_pointer", }, }, + { + name: "path parts not all used (pmap)", + inv: editor{ + Function: "testing_pmapgetsetter", + Arguments: []argument{ + { + Value: value{ + Literal: &mathExprLiteral{ + Path: &path{ + Fields: []field{ + { + Name: "name", + }, + { + Name: "not-used", + }, + }, + }, + }, + }, + }, + }, + }, + }, { name: "path parts not all used", inv: editor{ @@ -415,6 +457,35 @@ func Test_NewFunctionCall_invalid(t *testing.T) { }, }, }, + { + name: "Keys not allowed (pmap)", + inv: editor{ + Function: "testing_pmapgetsetter", + Arguments: []argument{ + { + Value: value{ + Literal: &mathExprLiteral{ + Path: &path{ + Fields: []field{ + { + Name: "name", + Keys: []key{ + { + String: ottltest.Strp("foo"), + }, + { + String: ottltest.Strp("bar"), + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, { name: "Keys not allowed", inv: editor{ @@ -806,6 +877,28 @@ func Test_NewFunctionCall(t *testing.T) { }, want: 2, }, + { + name: "pmapgetsetter arg", + inv: editor{ + Function: "testing_pmapgetsetter", + Arguments: []argument{ + { + Value: value{ + Literal: &mathExprLiteral{ + Path: &path{ + Fields: []field{ + { + Name: "name", + }, + }, + }, + }, + }, + }, + }, + }, + want: nil, + }, { name: "pmapgetter slice arg", inv: editor{ @@ -1452,6 +1545,36 @@ func Test_NewFunctionCall(t *testing.T) { }, want: nil, }, + { + name: "Complex Indexing (pmap)", + inv: editor{ + Function: "testing_pmapgetsetter", + Arguments: []argument{ + { + Value: value{ + Literal: &mathExprLiteral{ + Path: &path{ + Fields: []field{ + { + Name: "attributes", + Keys: []key{ + { + String: ottltest.Strp("foo"), + }, + { + String: ottltest.Strp("bar"), + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + want: nil, + }, { name: "Complex Indexing", inv: editor{ @@ -1482,6 +1605,28 @@ func Test_NewFunctionCall(t *testing.T) { }, want: nil, }, + { + name: "path that allows keys but none have been specified (pmap)", + inv: editor{ + Function: "testing_pmapgetsetter", + Arguments: []argument{ + { + Value: value{ + Literal: &mathExprLiteral{ + Path: &path{ + Fields: []field{ + { + Name: "attributes", + }, + }, + }, + }, + }, + }, + }, + }, + want: nil, + }, { name: "path that allows keys but none have been specified", inv: editor{ @@ -1888,6 +2033,16 @@ func functionWithByteSliceLikeGetter(ByteSliceLikeGetter[any]) (ExprFunc[any], e }, nil } +type pMapGetSetterArguments struct { + PMapGetSetterArg PMapGetSetter[any] +} + +func functionWithPMapGetSetter(PMapGetSetter[any]) (ExprFunc[any], error) { + return func(context.Context, any) (any, error) { + return "anything", nil + }, nil +} + type pMapGetterArguments struct { PMapArg PMapGetter[any] } @@ -2109,6 +2264,11 @@ func defaultFunctionsForTests() map[string]Factory[any] { &setterArguments{}, functionWithSetter, ), + createFactory[any]( + "testing_pmapgetsetter", + &pMapGetSetterArguments{}, + functionWithPMapGetSetter, + ), createFactory[any]( "testing_getsetter", &getSetterArguments{},