From d08877352bf51b70e1aef864e892f451abf9d95a Mon Sep 17 00:00:00 2001 From: Raj Nishtala Date: Fri, 15 Nov 2024 17:33:26 -0700 Subject: [PATCH] Adding an option to specify the string to trim --- pkg/ottl/ottlfuncs/README.md | 8 +++++--- pkg/ottl/ottlfuncs/func_trim.go | 13 +++++++++---- pkg/ottl/ottlfuncs/func_trim_test.go | 25 +++++++++++++++++++------ 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index a99445b27ac4..b2fb08c5127b 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -1782,17 +1782,19 @@ If the `target` is not a string or does not exist, the `Split` Converter will re ### Trim -```Trim(target)``` +```Trim(target, replacement)``` -The `Trim` Converter removes the leading and trailing whitespace. +The `Trim` Converter removes the leading and trailing character (default: whitespace). If the `target` is not a string or does not exist, the `Trim` Converter will return an error. `target` is a string. +`replacement` is an optional string representing the character to replace with. Examples: -- `Trim(" this is a test ")` +- `Trim(" this is a test ", " ")` +- `Trim("!!this is a test!!", "!!")` ### String diff --git a/pkg/ottl/ottlfuncs/func_trim.go b/pkg/ottl/ottlfuncs/func_trim.go index fadaf087fffa..f732b0116aca 100644 --- a/pkg/ottl/ottlfuncs/func_trim.go +++ b/pkg/ottl/ottlfuncs/func_trim.go @@ -12,7 +12,8 @@ import ( ) type TrimArguments[K any] struct { - Target ottl.StringGetter[K] + Target ottl.StringGetter[K] + Replacement ottl.Optional[string] } func NewTrimFactory[K any]() ottl.Factory[K] { @@ -26,15 +27,19 @@ func createTrimFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ot return nil, fmt.Errorf("TrimFactory args must be of type *TrimArguments[K]") } - return trim(args.Target), nil + return trim(args.Target, args.Replacement), nil } -func trim[K any](target ottl.StringGetter[K]) ottl.ExprFunc[K] { +func trim[K any](target ottl.StringGetter[K], replacement ottl.Optional[string]) ottl.ExprFunc[K] { return func(ctx context.Context, tCtx K) (any, error) { + replacementString := replacement.Get() val, err := target.Get(ctx, tCtx) if err != nil { return nil, err } - return strings.Trim(val, " "), nil + if replacementString == "" { + return strings.Trim(val, " "), nil + } + return strings.Trim(val, replacementString), nil } } diff --git a/pkg/ottl/ottlfuncs/func_trim_test.go b/pkg/ottl/ottlfuncs/func_trim_test.go index a69b71b6f509..8814857258b0 100644 --- a/pkg/ottl/ottlfuncs/func_trim_test.go +++ b/pkg/ottl/ottlfuncs/func_trim_test.go @@ -14,9 +14,10 @@ import ( func Test_trim(t *testing.T) { tests := []struct { - name string - target ottl.StringGetter[any] - expected any + name string + target ottl.StringGetter[any] + replacement ottl.Optional[string] + expected any }{ { name: "trim string", @@ -25,7 +26,8 @@ func Test_trim(t *testing.T) { return " this is a test ", nil }, }, - expected: "this is a test", + replacement: ottl.NewTestingOptional[string](" "), + expected: "this is a test", }, { name: "trim empty string", @@ -34,12 +36,23 @@ func Test_trim(t *testing.T) { return "", nil }, }, - expected: "", + replacement: ottl.NewTestingOptional[string](" "), + expected: "", + }, + { + name: "trim empty replacement string", + target: &ottl.StandardStringGetter[any]{ + Getter: func(_ context.Context, _ any) (any, error) { + return " this is a test ", nil + }, + }, + replacement: ottl.Optional[string]{}, + expected: "this is a test", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - exprFunc := trim(tt.target) + exprFunc := trim(tt.target, tt.replacement) result, err := exprFunc(nil, nil) assert.NoError(t, err) assert.Equal(t, tt.expected, result)