Skip to content

Commit 0552371

Browse files
committed
Removed WithPathContextNameValidation option
1 parent a2c8ab8 commit 0552371

File tree

3 files changed

+39
-73
lines changed

3 files changed

+39
-73
lines changed

pkg/ottl/functions.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,23 @@ func (p *Parser[K]) newPath(path *path) (*basePath[K], error) {
7878
}
7979

8080
func (p *Parser[K]) parsePathContext(path *path) (string, []field, error) {
81-
fields := path.Fields
82-
var pathContext string
81+
hasPathContextNames := len(p.pathContextNames) > 0
8382
if path.Context != "" {
84-
// nil or empty pathContextNames means the Parser isn't handling the grammar path's
85-
// context yet, so it falls back to the previous behavior with the path.Context value
86-
// as the first path's segment.
87-
if p.pathContextNames == nil || len(p.pathContextNames) == 0 {
88-
fields = append([]field{{Name: path.Context}}, fields...)
83+
// no pathContextNames means the Parser isn't handling the grammar path's context yet,
84+
// so it falls back to the previous behavior with the path.Context value as the first
85+
// path's segment.
86+
if !hasPathContextNames {
87+
return "", append([]field{{Name: path.Context}}, path.Fields...), nil
8988
} else if _, ok := p.pathContextNames[path.Context]; !ok {
90-
if p.validatePathContextNames {
91-
return "", nil, fmt.Errorf(`context "%s" from path "%s" is not valid, it must be replaced by one of: %s`, path.Context, buildOriginalText(path), p.buildPathContextNamesText(""))
92-
}
93-
fields = append([]field{{Name: path.Context}}, fields...)
94-
} else {
95-
pathContext = path.Context
89+
return "", path.Fields, fmt.Errorf(`context "%s" from path "%s" is not valid, it must be replaced by one of: %s`, path.Context, buildOriginalText(path), p.buildPathContextNamesText(""))
9690
}
97-
} else if p.validatePathContextNames {
91+
return path.Context, path.Fields, nil
92+
} else if hasPathContextNames {
9893
originalText := buildOriginalText(path)
9994
return "", nil, fmt.Errorf(`missing context name for path "%s", valid options are: %s`, originalText, p.buildPathContextNamesText(originalText))
10095
}
10196

102-
return pathContext, fields, nil
97+
return "", path.Fields, nil
10398
}
10499

105100
func (p *Parser[K]) buildPathContextNamesText(path string) string {

pkg/ottl/functions_test.go

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,52 +2402,34 @@ func Test_newPath(t *testing.T) {
24022402

24032403
func Test_newPath_WithPathContextNames(t *testing.T) {
24042404
tests := []struct {
2405-
name string
2406-
pathContext string
2407-
pathContextNames []string
2408-
validationEnabled bool
2409-
contextParsedAsField bool
2410-
expectedError bool
2405+
name string
2406+
pathContext string
2407+
pathContextNames []string
2408+
expectedError string
24112409
}{
24122410
{
2413-
name: "with no context",
2411+
name: "with no path context",
24142412
pathContextNames: []string{"log"},
2413+
expectedError: `missing context name for path "body.string[key]", valid options are: "log.body.string[key]"`,
24152414
},
24162415
{
2417-
name: "with context",
2416+
name: "with no path context and configuration",
2417+
},
2418+
{
2419+
name: "with valid path context",
24182420
pathContext: "log",
24192421
pathContextNames: []string{"log"},
24202422
},
24212423
{
2422-
name: "with multiple contexts",
2424+
name: "with invalid path context",
24232425
pathContext: "span",
2424-
pathContextNames: []string{"log", "span"},
2425-
},
2426-
{
2427-
name: "with invalid context and validation disabled",
2428-
pathContext: "span",
2429-
pathContextNames: []string{"log"},
2430-
validationEnabled: false,
2431-
contextParsedAsField: true,
2432-
},
2433-
{
2434-
name: "with invalid context and validation enabled",
2435-
pathContext: "span",
2436-
pathContextNames: []string{"log"},
2437-
validationEnabled: true,
2438-
expectedError: true,
2439-
},
2440-
{
2441-
name: "with valid context and validation enabled",
2442-
pathContext: "spanevent",
2443-
pathContextNames: []string{"spanevent"},
2444-
validationEnabled: true,
2426+
pathContextNames: []string{"log"},
2427+
expectedError: `context "span" from path "span.body.string[key]" is not valid, it must be replaced by one of: "log"`,
24452428
},
24462429
{
2447-
name: "with no context and validation enabled",
2448-
pathContextNames: []string{"spanevent"},
2449-
validationEnabled: true,
2450-
expectedError: true,
2430+
name: "with multiple configured contexts",
2431+
pathContext: "span",
2432+
pathContextNames: []string{"log", "span"},
24512433
},
24522434
}
24532435

@@ -2459,7 +2441,6 @@ func Test_newPath_WithPathContextNames(t *testing.T) {
24592441
componenttest.NewNopTelemetrySettings(),
24602442
WithEnumParser[any](testParseEnum),
24612443
WithPathContextNames[any](tt.pathContextNames),
2462-
WithPathContextNameValidation[any](tt.validationEnabled),
24632444
)
24642445

24652446
gp := &path{
@@ -2479,13 +2460,14 @@ func Test_newPath_WithPathContextNames(t *testing.T) {
24792460
}}
24802461

24812462
np, err := ps.newPath(gp)
2482-
if tt.expectedError {
2483-
assert.Error(t, err)
2463+
if tt.expectedError != "" {
2464+
assert.Error(t, err, tt.expectedError)
24842465
return
24852466
}
24862467
assert.NoError(t, err)
24872468
p := Path[any](np)
2488-
if tt.contextParsedAsField {
2469+
contextParsedAsField := len(tt.pathContextNames) == 0 && tt.pathContext != ""
2470+
if contextParsedAsField {
24892471
assert.Equal(t, tt.pathContext, p.Name())
24902472
assert.Equal(t, "", p.Context())
24912473
assert.Nil(t, p.Keys())
@@ -2500,13 +2482,13 @@ func Test_newPath_WithPathContextNames(t *testing.T) {
25002482
assert.Equal(t, "body", p.Name())
25012483
assert.Nil(t, p.Keys())
25022484
assert.Equal(t, bodyStringFuncValue, p.String())
2503-
if !tt.contextParsedAsField {
2485+
if !contextParsedAsField {
25042486
assert.Equal(t, tt.pathContext, p.Context())
25052487
}
25062488
p = p.Next()
25072489
assert.Equal(t, "string", p.Name())
25082490
assert.Equal(t, bodyStringFuncValue, p.String())
2509-
if !tt.contextParsedAsField {
2491+
if !contextParsedAsField {
25102492
assert.Equal(t, tt.pathContext, p.Context())
25112493
}
25122494
assert.Nil(t, p.Next())

pkg/ottl/parser.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,11 @@ func (c *Condition[K]) Eval(ctx context.Context, tCtx K) (bool, error) {
6363
// Parser provides the means to parse OTTL StatementSequence and Conditions given a specific set of functions,
6464
// a PathExpressionParser, and an EnumParser.
6565
type Parser[K any] struct {
66-
functions map[string]Factory[K]
67-
pathParser PathExpressionParser[K]
68-
enumParser EnumParser
69-
telemetrySettings component.TelemetrySettings
70-
pathContextNames map[string]bool
71-
validatePathContextNames bool
66+
functions map[string]Factory[K]
67+
pathParser PathExpressionParser[K]
68+
enumParser EnumParser
69+
telemetrySettings component.TelemetrySettings
70+
pathContextNames map[string]bool
7271
}
7372

7473
func NewParser[K any](
@@ -102,21 +101,11 @@ func WithEnumParser[K any](parser EnumParser) Option[K] {
102101
}
103102
}
104103

105-
// WithPathContextNameValidation turns the validation mode on/off for the Path's contexts.
106-
// When enabled, and WithPathContextNames is configured, it requires all Path's to be prefixed
107-
// with a valid context name, matching at least one configured WithPathContextNames value.
108-
func WithPathContextNameValidation[K any](enabled bool) Option[K] {
109-
return func(p *Parser[K]) {
110-
p.validatePathContextNames = enabled
111-
}
112-
}
113-
114104
// WithPathContextNames sets the context names to be considered when parsing a Path value.
115105
// When this option is empty or nil, all Path segments are considered fields, and the
116106
// Path.Context value is always empty.
117-
// When this option is configured, and the first path segment is not present in this context
118-
// names list, it either parses the whole Path as fields, without a Path.Context value,
119-
// or results into an error, depending on the WithPathContextNameValidation config.
107+
// When this option is configured, and the path's context is empty or is not present in
108+
// this context names list, it results into an error.
120109
func WithPathContextNames[K any](contexts []string) Option[K] {
121110
return func(p *Parser[K]) {
122111
pathContextNames := make(map[string]bool, len(contexts))

0 commit comments

Comments
 (0)