Skip to content

Commit 5189734

Browse files
Adding tests that show the change in the expected result without the optional hash function argument
1 parent d2fa6bf commit 5189734

10 files changed

+162
-155
lines changed

.chloggen/ottl-replace-pattern.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ issues: [27235]
1616
# These lines will be padded with 2 spaces and then inserted directly into the document.
1717
# Use pipe (|) for multiline entries.
1818
subtext: |
19-
The `ottl.Optional` type can now be used in a converter's `Arguments` struct
20-
to indicate that a parameter is optional. Hashing functions are passed as optional parameters to converters.
19+
Hashing functions are passed as optional arguments to the following converters:
20+
- `replace_pattern`
21+
- `replace_all_patterns`
22+
- `replace_match`
23+
- `replace_all_matches`
2124
2225
# If your change doesn't affect end users or the exported elements of any package,
2326
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

pkg/ottl/functions.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,25 +381,25 @@ type optionalManager interface {
381381
}
382382

383383
type Optional[T any] struct {
384-
Val T
385-
HasValue bool
384+
val T
385+
hasValue bool
386386
}
387387

388388
// This is called only by reflection.
389389
// nolint:unused
390390
func (o Optional[T]) set(val any) reflect.Value {
391391
return reflect.ValueOf(Optional[T]{
392-
Val: val.(T),
393-
HasValue: true,
392+
val: val.(T),
393+
hasValue: true,
394394
})
395395
}
396396

397397
func (o Optional[T]) IsEmpty() bool {
398-
return !o.HasValue
398+
return !o.hasValue
399399
}
400400

401401
func (o Optional[T]) Get() T {
402-
return o.Val
402+
return o.val
403403
}
404404

405405
func (o Optional[T]) get() reflect.Value {
@@ -414,7 +414,7 @@ func (o Optional[T]) get() reflect.Value {
414414
// OTTL functions.
415415
func NewTestingOptional[T any](val T) Optional[T] {
416416
return Optional[T]{
417-
Val: val,
418-
HasValue: true,
417+
val: val,
418+
hasValue: true,
419419
}
420420
}

pkg/ottl/ottlfuncs/func_replace_all_matches.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ func createReplaceAllMatchesFunction[K any](_ ottl.FunctionContext, oArgs ottl.A
3737
func replaceAllMatches[K any](target ottl.PMapGetter[K], pattern string, replacement ottl.StringGetter[K], fn ottl.Optional[ottl.FunctionGetter[K]]) (ottl.ExprFunc[K], error) {
3838
glob, err := glob.Compile(pattern)
3939
var replacementVal string
40-
var replacementExpr ottl.Expr[K]
41-
var replacementValRaw interface{}
4240
if err != nil {
4341
return nil, fmt.Errorf("the pattern supplied to replace_match is not a valid pattern: %w", err)
4442
}
@@ -54,13 +52,13 @@ func replaceAllMatches[K any](target ottl.PMapGetter[K], pattern string, replace
5452
}
5553
} else {
5654
fnVal := fn.Get()
57-
replacementExpr, err = fnVal.Get(&FuncArgs[K]{Input: replacement})
58-
if err != nil {
59-
return nil, err
55+
replacementExpr, errNew := fnVal.Get(&FuncArgs[K]{Input: replacement})
56+
if errNew != nil {
57+
return nil, errNew
6058
}
61-
replacementValRaw, err = replacementExpr.Eval(ctx, tCtx)
62-
if err != nil {
63-
return nil, err
59+
replacementValRaw, errNew := replacementExpr.Eval(ctx, tCtx)
60+
if errNew != nil {
61+
return nil, errNew
6462
}
6563
replacementVal = replacementValRaw.(string)
6664
}

pkg/ottl/ottlfuncs/func_replace_all_matches_test.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ func Test_replaceAllMatches(t *testing.T) {
2020
input.PutStr("test2", "hello")
2121
input.PutStr("test3", "goodbye")
2222

23+
ottlValue := ottl.StandardFunctionGetter[pcommon.Map]{
24+
FCtx: ottl.FunctionContext{
25+
Set: componenttest.NewNopTelemetrySettings(),
26+
},
27+
Fact: StandardConverters[pcommon.Map]()["SHA256"],
28+
}
29+
optionalArg := ottl.NewTestingOptional[ottl.FunctionGetter[pcommon.Map]](ottlValue)
30+
2331
target := &ottl.StandardPMapGetter[pcommon.Map]{
2432
Getter: func(ctx context.Context, tCtx pcommon.Map) (interface{}, error) {
2533
return tCtx, nil
@@ -35,29 +43,37 @@ func Test_replaceAllMatches(t *testing.T) {
3543
want func(pcommon.Map)
3644
}{
3745
{
38-
name: "replace only matches",
46+
name: "replace only matches (with hash function)",
3947
target: target,
4048
pattern: "hello*",
4149
replacement: ottl.StandardStringGetter[pcommon.Map]{
4250
Getter: func(context.Context, pcommon.Map) (interface{}, error) {
4351
return "hello {universe}", nil
4452
},
4553
},
46-
function: ottl.Optional[ottl.FunctionGetter[pcommon.Map]]{
47-
Val: ottl.StandardFunctionGetter[pcommon.Map]{
48-
FCtx: ottl.FunctionContext{
49-
Set: componenttest.NewNopTelemetrySettings(),
50-
},
51-
Fact: StandardConverters[pcommon.Map]()["SHA256"],
52-
},
53-
HasValue: true,
54-
},
54+
function: optionalArg,
5555
want: func(expectedMap pcommon.Map) {
5656
expectedMap.PutStr("test", "4804d6b7f03268e33f78c484977f3d81771220df07cc6aac4ad4868102141fad")
5757
expectedMap.PutStr("test2", "4804d6b7f03268e33f78c484977f3d81771220df07cc6aac4ad4868102141fad")
5858
expectedMap.PutStr("test3", "goodbye")
5959
},
6060
},
61+
{
62+
name: "replace only matches",
63+
target: target,
64+
pattern: "hello*",
65+
replacement: ottl.StandardStringGetter[pcommon.Map]{
66+
Getter: func(context.Context, pcommon.Map) (interface{}, error) {
67+
return "hello {universe}", nil
68+
},
69+
},
70+
function: ottl.Optional[ottl.FunctionGetter[pcommon.Map]]{},
71+
want: func(expectedMap pcommon.Map) {
72+
expectedMap.PutStr("test", "hello {universe}")
73+
expectedMap.PutStr("test2", "hello {universe}")
74+
expectedMap.PutStr("test3", "goodbye")
75+
},
76+
},
6177
{
6278
name: "no matches",
6379
target: target,
@@ -67,9 +83,7 @@ func Test_replaceAllMatches(t *testing.T) {
6783
return "nothing {matches}", nil
6884
},
6985
},
70-
function: ottl.Optional[ottl.FunctionGetter[pcommon.Map]]{
71-
HasValue: false,
72-
},
86+
function: ottl.Optional[ottl.FunctionGetter[pcommon.Map]]{},
7387
want: func(expectedMap pcommon.Map) {
7488
expectedMap.PutStr("test", "hello world")
7589
expectedMap.PutStr("test2", "hello")
@@ -109,9 +123,7 @@ func Test_replaceAllMatches_bad_input(t *testing.T) {
109123
return "{replacement}", nil
110124
},
111125
}
112-
function := ottl.Optional[ottl.FunctionGetter[interface{}]]{
113-
HasValue: false,
114-
}
126+
function := ottl.Optional[ottl.FunctionGetter[interface{}]]{}
115127

116128
exprFunc, err := replaceAllMatches[interface{}](target, "*", replacement, function)
117129
assert.NoError(t, err)
@@ -130,9 +142,7 @@ func Test_replaceAllMatches_get_nil(t *testing.T) {
130142
return "{anything}", nil
131143
},
132144
}
133-
function := ottl.Optional[ottl.FunctionGetter[interface{}]]{
134-
HasValue: false,
135-
}
145+
function := ottl.Optional[ottl.FunctionGetter[interface{}]]{}
136146

137147
exprFunc, err := replaceAllMatches[interface{}](target, "*", replacement, function)
138148
assert.NoError(t, err)

pkg/ottl/ottlfuncs/func_replace_all_patterns.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ func createReplaceAllPatternsFunction[K any](_ ottl.FunctionContext, oArgs ottl.
4343
func replaceAllPatterns[K any](target ottl.PMapGetter[K], mode string, regexPattern string, replacement ottl.StringGetter[K], fn ottl.Optional[ottl.FunctionGetter[K]]) (ottl.ExprFunc[K], error) {
4444
compiledPattern, err := regexp.Compile(regexPattern)
4545
var replacementVal string
46-
var replacementExpr ottl.Expr[K]
47-
var replacementValRaw interface{}
4846
if err != nil {
4947
return nil, fmt.Errorf("the regex pattern supplied to replace_all_patterns is not a valid pattern: %w", err)
5048
}
@@ -64,13 +62,13 @@ func replaceAllPatterns[K any](target ottl.PMapGetter[K], mode string, regexPatt
6462
}
6563
} else {
6664
fnVal := fn.Get()
67-
replacementExpr, err = fnVal.Get(&FuncArgs[K]{Input: replacement})
68-
if err != nil {
69-
return nil, err
65+
replacementExpr, errNew := fnVal.Get(&FuncArgs[K]{Input: replacement})
66+
if errNew != nil {
67+
return nil, errNew
7068
}
71-
replacementValRaw, err = replacementExpr.Eval(ctx, tCtx)
72-
if err != nil {
73-
return nil, err
69+
replacementValRaw, errNew := replacementExpr.Eval(ctx, tCtx)
70+
if errNew != nil {
71+
return nil, errNew
7472
}
7573
replacementVal = replacementValRaw.(string)
7674
}

0 commit comments

Comments
 (0)