Skip to content

Commit

Permalink
Remove accessing to top-level objects support
Browse files Browse the repository at this point in the history
  • Loading branch information
edmocosta committed Dec 17, 2024
1 parent b82a4f0 commit 6737564
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 90 deletions.
22 changes: 1 addition & 21 deletions pkg/ottl/context_inferrer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 0 additions & 27 deletions pkg/ottl/context_inferrer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
17 changes: 0 additions & 17 deletions pkg/ottl/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,30 +103,13 @@ 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))
}

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
Expand Down
15 changes: 0 additions & 15 deletions pkg/ottl/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
4 changes: 1 addition & 3 deletions pkg/ottl/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
7 changes: 0 additions & 7 deletions pkg/ottl/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6737564

Please sign in to comment.