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

feat(ottl): Add a new ottl trim function that trims leading and trailing whitespace from a string #36400

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

rnishtala-sumo
Copy link
Contributor

@rnishtala-sumo rnishtala-sumo commented Nov 16, 2024

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".

  • Trim(" this is a test ") results in this is a test

Link to the issue - #34100

Copy link
Member

@TylerHelmuth TylerHelmuth left a 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

@TylerHelmuth
Copy link
Member

@rnishtala-sumo can you add to the description the issue this PR closes

.chloggen/ottl-trim-function.yaml Outdated Show resolved Hide resolved
pkg/ottl/ottlfuncs/func_trim.go Outdated Show resolved Hide resolved
pkg/ottl/ottlfuncs/README.md Show resolved Hide resolved
pkg/ottl/ottlfuncs/README.md Show resolved Hide resolved
if err != nil {
return nil, err
}
if replacement.IsEmpty() || replacementString == "" {
Copy link
Member

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 " ".

Copy link
Contributor Author

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?

Copy link
Member

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 "".

Copy link
Member

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 "".

Copy link
Contributor Author

@rnishtala-sumo rnishtala-sumo Dec 9, 2024

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).

}

func trim[K any](target ottl.StringGetter[K], replacement ottl.Optional[string]) ottl.ExprFunc[K] {
replacementString := replacement.Get()
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants