Skip to content

Commit

Permalink
Removed change log file, renamed function and moved if out of the loop
Browse files Browse the repository at this point in the history
  • Loading branch information
edmocosta committed Oct 31, 2024
1 parent a027e80 commit 8225cec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 45 deletions.
27 changes: 0 additions & 27 deletions .chloggen/ottl-add-statement-context-append-utility.yaml

This file was deleted.

26 changes: 14 additions & 12 deletions pkg/ottl/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"sort"
"strings"

"github.com/alecthomas/participle/v2"
Expand Down Expand Up @@ -196,11 +197,11 @@ func (p *Parser[K]) ParseCondition(condition string) (*Condition[K], error) {
}, nil
}

// appendStatementPathsContext changes the given OTTL statement adding the context name prefix
// prependContextToStatementPaths changes the given OTTL statement adding the context name prefix
// to all context-less paths. No modifications are performed for paths which [Path.Context]
// value matches any WithPathContextNames value.
// The context argument must be valid WithPathContextNames value, otherwise an error is returned.
func (p *Parser[K]) appendStatementPathsContext(context string, statement string) (string, error) {
func (p *Parser[K]) prependContextToStatementPaths(context string, statement string) (string, error) {
if _, ok := p.pathContextNames[context]; !ok {
return statement, fmt.Errorf(`unknown context "%s" for parser %T, valid options are: %s`, context, p, p.buildPathContextNamesText(""))
}
Expand All @@ -220,7 +221,7 @@ func (p *Parser[K]) appendStatementPathsContext(context string, statement string
}
}

return writeStatementWithPathsContext(context, statement, missingContextOffsets), nil
return insertContextIntoStatementOffsets(context, statement, missingContextOffsets)
}

var parser = newParser[parsedStatement]()
Expand Down Expand Up @@ -254,27 +255,28 @@ func parseCondition(raw string) (*booleanExpression, error) {
return parsed, nil
}

func writeStatementWithPathsContext(context string, statement string, offsets []int) string {
func insertContextIntoStatementOffsets(context string, statement string, offsets []int) (string, error) {
if len(offsets) == 0 {
return statement
return statement, nil
}

contextPrefix := context + "."
var sb strings.Builder
sb.Grow(len(statement) + (len(contextPrefix) * len(offsets)))

sort.Ints(offsets)
left := 0
for i, offset := range offsets {
for _, offset := range offsets {
if offset < 0 || offset > len(statement) {
return statement, fmt.Errorf(`failed to insert context "%s" into statement "%s": offset %d is out of range`, context, statement, offset)
}
sb.WriteString(statement[left:offset])
sb.WriteString(contextPrefix)
if i+1 >= len(offsets) {
sb.WriteString(statement[offset:])
} else {
left = offset
}
left = offset
}
sb.WriteString(statement[left:])

return sb.String()
return sb.String(), nil
}

// newParser returns a parser that can be used to read a string into a parsedStatement. An error will be returned if the string
Expand Down
12 changes: 6 additions & 6 deletions pkg/ottl/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2715,7 +2715,7 @@ func Test_ConditionSequence_Eval_Error(t *testing.T) {
}
}

func Test_appendStatementPathsContext_InvalidStatement(t *testing.T) {
func Test_prependContextToStatementPaths_InvalidStatement(t *testing.T) {
ps, err := NewParser(
CreateFactoryMap[any](),
testParsePath[any],
Expand All @@ -2724,11 +2724,11 @@ func Test_appendStatementPathsContext_InvalidStatement(t *testing.T) {
WithPathContextNames[any]([]string{"foo", "bar"}),
)
require.NoError(t, err)
_, err = ps.appendStatementPathsContext("foo", "this is invalid")
_, err = ps.prependContextToStatementPaths("foo", "this is invalid")
require.ErrorContains(t, err, `statement has invalid syntax`)
}

func Test_appendStatementPathsContext_InvalidContext(t *testing.T) {
func Test_prependContextToStatementPaths_InvalidContext(t *testing.T) {
ps, err := NewParser(
CreateFactoryMap[any](),
testParsePath[any],
Expand All @@ -2737,11 +2737,11 @@ func Test_appendStatementPathsContext_InvalidContext(t *testing.T) {
WithPathContextNames[any]([]string{"foo", "bar"}),
)
require.NoError(t, err)
_, err = ps.appendStatementPathsContext("foobar", "set(foo, 1)")
_, err = ps.prependContextToStatementPaths("foobar", "set(foo, 1)")
require.ErrorContains(t, err, `unknown context "foobar" for parser`)
}

func Test_appendStatementPathsContext_Success(t *testing.T) {
func Test_prependContextToStatementPaths_Success(t *testing.T) {
type mockSetArguments[K any] struct {
Target Setter[K]
Value Getter[K]
Expand Down Expand Up @@ -2845,7 +2845,7 @@ func Test_appendStatementPathsContext_Success(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, ps)

result, err := ps.appendStatementPathsContext(tt.context, tt.statement)
result, err := ps.prependContextToStatementPaths(tt.context, tt.statement)
require.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
Expand Down

0 comments on commit 8225cec

Please sign in to comment.