Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OTTL] Fix empty string matching for replace_all_patterns #36408

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/ottl/ottlfuncs/func_replace_all_matches.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func replaceAllMatches[K any](target ottl.PMapGetter[K], pattern string, replace
}
}
val.Range(func(_ string, value pcommon.Value) bool {
if value.Type() != pcommon.ValueTypeStr {
return true
}
if glob.Match(value.Str()) {
value.SetStr(replacementVal)
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/ottl/ottlfuncs/func_replace_all_matches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func Test_replaceAllMatches(t *testing.T) {
input.PutStr("test", "hello world")
input.PutStr("test2", "hello")
input.PutStr("test3", "goodbye")
input.PutStr("test4", "")
input.PutInt("test5", 123)

ottlValue := ottl.StandardFunctionGetter[pcommon.Map]{
FCtx: ottl.FunctionContext{
Expand Down Expand Up @@ -64,6 +66,8 @@ func Test_replaceAllMatches(t *testing.T) {
expectedMap.PutStr("test", "prefix=hash(hello {universe})")
expectedMap.PutStr("test2", "prefix=hash(hello {universe})")
expectedMap.PutStr("test3", "goodbye")
expectedMap.PutStr("test4", "")
expectedMap.PutInt("test5", 123)
},
},
{
Expand All @@ -81,6 +85,8 @@ func Test_replaceAllMatches(t *testing.T) {
expectedMap.PutStr("test", "hello {universe}")
expectedMap.PutStr("test2", "hello {universe}")
expectedMap.PutStr("test3", "goodbye")
expectedMap.PutStr("test4", "")
expectedMap.PutInt("test5", 123)
},
},
{
Expand All @@ -98,6 +104,27 @@ func Test_replaceAllMatches(t *testing.T) {
expectedMap.PutStr("test", "hello world")
expectedMap.PutStr("test2", "hello")
expectedMap.PutStr("test3", "goodbye")
expectedMap.PutStr("test4", "")
expectedMap.PutInt("test5", 123)
},
},
{
name: "replace empty string",
target: target,
pattern: "",
replacement: ottl.StandardStringGetter[pcommon.Map]{
Getter: func(context.Context, pcommon.Map) (any, error) {
return "empty_string_replacement", nil
},
},
function: ottl.Optional[ottl.FunctionGetter[pcommon.Map]]{},
replacementFormat: ottl.Optional[ottl.StringGetter[pcommon.Map]]{},
want: func(expectedMap pcommon.Map) {
expectedMap.PutStr("test", "hello world")
expectedMap.PutStr("test2", "hello")
expectedMap.PutStr("test3", "goodbye")
expectedMap.PutStr("test4", "empty_string_replacement")
expectedMap.PutInt("test5", 123)
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ottl/ottlfuncs/func_replace_all_patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func replaceAllPatterns[K any](target ottl.PMapGetter[K], mode string, regexPatt
val.Range(func(key string, originalValue pcommon.Value) bool {
switch mode {
case modeValue:
if compiledPattern.MatchString(originalValue.Str()) {
if originalValue.Type() == pcommon.ValueTypeStr && compiledPattern.MatchString(originalValue.Str()) {
if !fn.IsEmpty() {
updatedString, err := applyOptReplaceFunction(ctx, tCtx, compiledPattern, fn, originalValue.Str(), replacementVal, replacementFormat)
if err != nil {
Expand Down
44 changes: 44 additions & 0 deletions pkg/ottl/ottlfuncs/func_replace_all_patterns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func Test_replaceAllPatterns(t *testing.T) {
input.PutInt("test4", 1234)
input.PutDouble("test5", 1234)
input.PutBool("test6", true)
input.PutStr("test7", "")

ottlValue := ottl.StandardFunctionGetter[pcommon.Map]{
FCtx: ottl.FunctionContext{
Expand Down Expand Up @@ -77,6 +78,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -97,6 +99,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -117,6 +120,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -137,6 +141,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -158,6 +163,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand Down Expand Up @@ -196,6 +202,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -217,6 +224,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -238,6 +246,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -258,6 +267,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -278,6 +288,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -298,6 +309,8 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -318,6 +331,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -338,6 +352,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -360,6 +375,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -382,6 +398,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -404,6 +421,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test.4", 1234)
expectedMap.PutDouble("test.5", 1234)
expectedMap.PutBool("test.6", true)
expectedMap.PutStr("test.7", "")
},
},
{
Expand All @@ -426,6 +444,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
Expand All @@ -447,6 +466,7 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test-4", 1234)
expectedMap.PutDouble("test-5", 1234)
expectedMap.PutBool("test-6", true)
expectedMap.PutStr("test-7", "")
},
},
{
Expand All @@ -469,6 +489,30 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "")
},
},
{
name: "replacement for empty string",
target: target,
mode: modeValue,
pattern: `^$`,
replacement: ottl.StandardStringGetter[pcommon.Map]{
Getter: func(context.Context, pcommon.Map) (any, error) {
return "empty_string_replacement", nil
},
},
replacementFormat: ottl.Optional[ottl.StringGetter[pcommon.Map]]{},
function: ottl.Optional[ottl.FunctionGetter[pcommon.Map]]{},
want: func(expectedMap pcommon.Map) {
expectedMap.Clear()
expectedMap.PutStr("test", "hello world")
expectedMap.PutStr("test2", "hello")
expectedMap.PutStr("test3", "goodbye world1 and world2")
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
expectedMap.PutStr("test7", "empty_string_replacement")
},
},
}
Expand Down