From 6737564a7f1a7749663dc648bfc32689376ef2a3 Mon Sep 17 00:00:00 2001 From: edmocosta <11836452+edmocosta@users.noreply.github.com> Date: Tue, 17 Dec 2024 18:58:28 +0100 Subject: [PATCH] Remove accessing to top-level objects support --- pkg/ottl/context_inferrer.go | 22 +--------------------- pkg/ottl/context_inferrer_test.go | 27 --------------------------- pkg/ottl/functions.go | 17 ----------------- pkg/ottl/functions_test.go | 15 --------------- pkg/ottl/parser.go | 4 +--- pkg/ottl/parser_test.go | 7 ------- 6 files changed, 2 insertions(+), 90 deletions(-) diff --git a/pkg/ottl/context_inferrer.go b/pkg/ottl/context_inferrer.go index a5e18bb25eeb..714100ac4d57 100644 --- a/pkg/ottl/context_inferrer.go +++ b/pkg/ottl/context_inferrer.go @@ -88,7 +88,7 @@ func (s *priorityContextInferrer) infer(statements []string) (string, error) { statementPaths, statementFunctions, statementEnums := s.getParsedStatementHints(parsed) for _, p := range statementPaths { - candidate := s.getContextCandidate(p) + candidate := p.Context candidatePriority, ok := s.contextPriority[candidate] if !ok { candidatePriority = math.MaxInt @@ -172,26 +172,6 @@ func (s *priorityContextInferrer) inferFromLowerContexts( return "" } -// When a path has no dots separators (e.g.: resource), the grammar extracts it into the -// path.Fields slice, letting the path.Context empty. This function returns either the -// path.Context string or, if it's eligible and meets certain conditions, the first -// path.Fields name. -func (s *priorityContextInferrer) getContextCandidate(p path) string { - if p.Context != "" { - return p.Context - } - // If it has multiple fields or keys, it means the path has at least one dot on it, - // and isn't a context access. - if len(p.Fields) != 1 || len(p.Fields[0].Keys) > 0 { - return "" - } - _, ok := s.contextPriority[p.Fields[0].Name] - if ok { - return p.Fields[0].Name - } - return "" -} - // sortContextCandidates sorts the slice candidates using the priorityContextInferrer.contextsPriority order. func (s *priorityContextInferrer) sortContextCandidates(candidates []string) { slices.SortFunc(candidates, func(l, r string) int { diff --git a/pkg/ottl/context_inferrer_test.go b/pkg/ottl/context_inferrer_test.go index 6e441a202982..9cec76503451 100644 --- a/pkg/ottl/context_inferrer_test.go +++ b/pkg/ottl/context_inferrer_test.go @@ -89,33 +89,6 @@ func Test_NewPriorityContextInferrer_Infer(t *testing.T) { statements: []string{"set(span.foo, true) where span.bar == true"}, expected: "span", }, - { - name: "with statement context root object", - priority: []string{"resource", "foo"}, - candidates: map[string]*priorityContextInferrerCandidate{ - "resource": defaultDummyPriorityContextInferrerCandidate, - }, - statements: []string{"set(foo.attributes[\"body\"], resource)"}, - expected: "resource", - }, - { - name: "with non-eligible statement context root object", - priority: []string{"resource", "foo"}, - candidates: map[string]*priorityContextInferrerCandidate{ - "foo": defaultDummyPriorityContextInferrerCandidate, - }, - statements: []string{"set(foo.attributes[\"body\"], resource[\"foo\"])"}, - expected: "foo", - }, - { - name: "with non-prioritized statement context root object", - priority: []string{"foo"}, - candidates: map[string]*priorityContextInferrerCandidate{ - "bar": defaultDummyPriorityContextInferrerCandidate, - }, - statements: []string{"set(resource, bar.attributes[\"body\"])"}, - expected: "bar", - }, { name: "inferred path context with missing function", priority: []string{"foo", "datapoint", "metric"}, diff --git a/pkg/ottl/functions.go b/pkg/ottl/functions.go index 3b11b8fd7fd3..4ff92123c7e6 100644 --- a/pkg/ottl/functions.go +++ b/pkg/ottl/functions.go @@ -103,9 +103,6 @@ func (p *Parser[K]) parsePathContext(path *path) (string, []field, error) { } if hasPathContextNames { - if ok, contextName := p.isPathToContextRootData(path); ok { - return contextName, []field{{}}, nil - } originalText := buildOriginalText(path) return "", nil, fmt.Errorf(`missing context name for path "%s", possibly valid options are: %s`, originalText, p.buildPathContextNamesText(originalText)) } @@ -113,20 +110,6 @@ func (p *Parser[K]) parsePathContext(path *path) (string, []field, error) { return "", path.Fields, nil } -// When a path has no dots separators (e.g.: resource), the grammar extracts it into the -// path.Fields slice, letting the path.Context empty. This function verifies if a given -// path is accessing any configured (ottl.WithPathContextNames) object root, returning -// true and the resolved context name. -func (p *Parser[K]) isPathToContextRootData(pat *path) (bool, string) { - // If the context value is filled, or it has multiple fields, it means the path - // has at least one dot on it. - if pat.Context != "" || len(pat.Fields) != 1 || len(pat.Fields[0].Keys) > 0 { - return false, "" - } - _, ok := p.pathContextNames[pat.Fields[0].Name] - return ok, pat.Fields[0].Name -} - func (p *Parser[K]) buildPathContextNamesText(path string) string { var builder strings.Builder var suffix string diff --git a/pkg/ottl/functions_test.go b/pkg/ottl/functions_test.go index 358f61e94f0b..a7bd4aef87c7 100644 --- a/pkg/ottl/functions_test.go +++ b/pkg/ottl/functions_test.go @@ -2504,21 +2504,6 @@ func Test_newPath_WithPathContextNames(t *testing.T) { } } -func Test_newPath_withPathNameEqualsToContextName(t *testing.T) { - ps, _ := NewParser[any]( - defaultFunctionsForTests(), - testParsePath[any], - componenttest.NewNopTelemetrySettings(), - WithEnumParser[any](testParseEnum), - WithPathContextNames[any]([]string{"resource"}), - ) - - rs, err := ps.newPath(&path{Context: "", Fields: []field{{Name: "resource"}}}) - assert.NoError(t, err) - assert.Equal(t, "resource", rs.Context()) - assert.Empty(t, rs.Name()) -} - func Test_baseKey_String(t *testing.T) { bp := baseKey[any]{ s: ottltest.Strp("test"), diff --git a/pkg/ottl/parser.go b/pkg/ottl/parser.go index e162c8b57888..fade87d2982d 100644 --- a/pkg/ottl/parser.go +++ b/pkg/ottl/parser.go @@ -224,9 +224,7 @@ func (p *Parser[K]) prependContextToStatementPaths(context string, statement str var missingContextOffsets []int for _, it := range paths { if _, ok := p.pathContextNames[it.Context]; !ok { - if skip, _ := p.isPathToContextRootData(&it); !skip { - missingContextOffsets = append(missingContextOffsets, it.Pos.Offset) - } + missingContextOffsets = append(missingContextOffsets, it.Pos.Offset) } } diff --git a/pkg/ottl/parser_test.go b/pkg/ottl/parser_test.go index b416d8239cd1..726f531bfb81 100644 --- a/pkg/ottl/parser_test.go +++ b/pkg/ottl/parser_test.go @@ -2832,13 +2832,6 @@ func Test_prependContextToStatementPaths_Success(t *testing.T) { pathContextNames: []string{"log", "resource"}, expected: `set(log.attributes["test"], "pass") where IsMatch(resource.name, "operation[AC]")`, }, - { - name: "path to context root object", - statement: `set(attributes["test"], resource)`, - context: "log", - pathContextNames: []string{"log", "resource"}, - expected: `set(log.attributes["test"], resource)`, - }, } for _, tt := range tests {