Skip to content

Commit

Permalink
Adding an option to specify the string to trim
Browse files Browse the repository at this point in the history
  • Loading branch information
rnishtala-sumo committed Nov 18, 2024
1 parent b2c24fd commit d088773
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
8 changes: 5 additions & 3 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions pkg/ottl/ottlfuncs/func_trim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -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
}
}
25 changes: 19 additions & 6 deletions pkg/ottl/ottlfuncs/func_trim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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)
Expand Down

0 comments on commit d088773

Please sign in to comment.