-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
feat(ottl): Add a new ottl trim function that trims leading and trailing whitespace from a string #36400
base: main
Are you sure you want to change the base?
feat(ottl): Add a new ottl trim function that trims leading and trailing whitespace from a string #36400
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add an entry to ottlfuncs/README
for this new converter
@rnishtala-sumo can you add to the description the issue this PR closes |
…ing whitespace from a string
c73ca02
to
b2c24fd
Compare
9c7f12c
to
22bb9aa
Compare
22bb9aa
to
d088773
Compare
pkg/ottl/ottlfuncs/func_trim.go
Outdated
if err != nil { | ||
return nil, err | ||
} | ||
if replacement.IsEmpty() || replacementString == "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Above line 34, lets to a check for if replacement == ""
and error if it is. Calling Trim(name, "")
implies a nop, but we'd be trimming " "
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TylerHelmuth isn't an empty replacement string expected since the argument is optional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm saying we shouldn't allow a user to set the optional parameter to be "".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IsEmpty returns false if the user sets the optional parameter to any value I thought, even default values like "".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, replacement.isEmpty()
returns false with a default value of ""
for the replacementString
.
However, the replacementString is set to ""
under two conditions
- When the replacement isn't provided (uses default
""
) - When the
replacementString
is explicitly set to""
If we return an error when the replacementString
is set to ""
, we would then make it a mandatory parameter
I've pushed a commit such that when the replacementString is set to ""
in either of the above conditions, there is no effect on the original string (default behavior).
… nothing gets trimmed (default)
} | ||
|
||
func trim[K any](target ottl.StringGetter[K], replacement ottl.Optional[string]) ottl.ExprFunc[K] { | ||
replacementString := replacement.Get() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rnishtala-sumo we do need to still check if replacement
is empty or not. If IsEmpty()
returns true then we need to use a default cut string of " "
. If IsEmpty()
returns false then we can use whatever the value of Get
returns.
Description
A field test exists in an event, which contains the string " this is a test ".
I would like to be able to transform this using an ottl function trim, that I would define as trim(attribute["test"]), which would trim this down to "this is a test".
this is a test
Link to the issue - #34100