Skip to content

Commit

Permalink
support expression as attribute key in parser
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Dec 18, 2024
1 parent 87f2d33 commit 2301072
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 27 deletions.
12 changes: 6 additions & 6 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ func Test_e2e_converters(t *testing.T) {
statement string
want func(tCtx ottllog.TransformContext)
}{
// {
// statement: `set(attributes[ConvertCase(attributes["A|B|C"], "upper")], "myvalue")`,
// want: func(tCtx ottllog.TransformContext) {
// tCtx.GetLogRecord().Attributes().PutStr("SOMETHING", "myvalue")
// },
// },
{
statement: `set(attributes[attributes["flags"]], "something33")`,
want: func(tCtx ottllog.TransformContext) {
Expand All @@ -341,12 +347,6 @@ func Test_e2e_converters(t *testing.T) {
tCtx.GetLogRecord().Attributes().PutStr("something", "something2")
},
},
// {
// statement: `set(attributes[ConvertCase(attributes["A|B|C"], "upper")], "myvalue")`,
// want: func(tCtx ottllog.TransformContext) {
// tCtx.GetLogRecord().Attributes().PutStr("SOMETHING", "myvalue")
// },
// },
{
statement: `set(body, attributes[attributes["foo"][attributes["slice"]][attributes["int_value"]]])`,
want: func(tCtx ottllog.TransformContext) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/ottl/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func buildOriginalKeysText(keys []key) string {
if k.String != nil {
builder.WriteString(*k.String)
}
if k.Path != nil {
builder.WriteString(buildOriginalText(k.Path))
if k.Expression != nil && k.Expression.Path != nil {
builder.WriteString(buildOriginalText(k.Expression.Path))
}
builder.WriteString("]")
}
Expand Down Expand Up @@ -217,8 +217,8 @@ func (p *Parser[K]) newKeys(keys []key) ([]Key[K], error) {
ks := make([]Key[K], len(keys))
for i := range keys {
var par GetSetter[K]
if keys[i].Path != nil {
arg, err := p.buildGetSetterFromPath(keys[i].Path)
if keys[i].Expression != nil && keys[i].Expression.Path != nil {
arg, err := p.buildGetSetterFromPath(keys[i].Expression.Path)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/ottl/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,9 @@ type field struct {
}

type key struct {
String *string `parser:"'[' (@String "`
Int *int64 `parser:"| @Int"`
Path *path `parser:"| @@ ) ']'"`
String *string `parser:"'[' (@String "`
Int *int64 `parser:"| @Int"`
Expression *mathExprLiteral `parser:"| @@ ) ']'"`
}

type list struct {
Expand Down
14 changes: 0 additions & 14 deletions pkg/ottl/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,6 @@ func Test_lexer(t *testing.T) {
{"Punct", "]"},
{"Punct", "]"},
}},
// {"Dynamic path", `attributes[ConvertCase(attributes["foo"], "upper")]`, false, []result{
// {"Lowercase", "attributes"},
// {"Punct", "["},
// {"Uppercase", "ConvertCase"},
// {"LParen", "("},
// {"Lowercase", "attributes"},
// {"Punct", "["},
// {"String", `"foo"`},
// {"Punct", "]"},
// {"Punct", ","},
// {"String", "upper"},
// {"RParen", ")"},
// {"Punct", "]"},
// }},
}

for _, tt := range tests {
Expand Down

0 comments on commit 2301072

Please sign in to comment.