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

Proposal: regex.replace(search, pattern, value) #5162

Closed
datfinesoul opened this issue Sep 21, 2022 · 7 comments · Fixed by #5179
Closed

Proposal: regex.replace(search, pattern, value) #5162

datfinesoul opened this issue Sep 21, 2022 · 7 comments · Fixed by #5179

Comments

@datfinesoul
Copy link

datfinesoul commented Sep 21, 2022

What is the underlying problem you're trying to solve?

See the un-commented parts of the example
https://play.openpolicyagent.org/p/aApnhKICLK

We were asking around about ways to do some more complex string manipulation, in this case taking the "friendly" github usernames, and converting them into their ID type format. The best thing we could find after asking on the OPA slack was the solution in this example.

trim_whitespace(s) := concat("-", parts) {
	parts := [lower(part) |
		some part in split(s, " ")
		not part in {"-"}
	]
}
teams := { x | x := trim_whitespace(input.session.teams[_])}

Describe the ideal solution

See the example, the commented "preferred" block
https://play.openpolicyagent.org/p/aApnhKICLK

preferred := { x |
    some part in input.session.teams
    x := regex.replace(part, "[ -]+", "-")
}

Describe a "Good Enough" solution

I'm not sure I can think of a different option. Since rego already has support for regex matches my assumption is that the above option might be possible.

Additional Context

@anderseknert
Copy link
Member

Thanks for suggesting that, @datfinesoul 👍 Would you like to take a shot at an implementation? :)

@datfinesoul
Copy link
Author

@anderseknert My golang skills are practically non-existent at the moment so that might be somewhat of a problem, but if no one has time to tackle this in the coming months, I'll give it a shot as I improve.

@anderseknert
Copy link
Member

Cool! I've labeled it as a good first issue, so it's not unthinkable that someone else wants to give it a shot. But if that happens, we'll have other issues for you if you're up for it :)

@boranx
Copy link
Member

boranx commented Sep 23, 2022

wouldn't the following work for this case? afaik, there's already one builtin replace method but probably it doesn't support general regex syntax

preferred := { x |
    some part in input.session.teams
    x := strings.replace_n({" ": "-"}, part)
}

@anderseknert
Copy link
Member

@boranx yeah, for simple cases you can probably achieve what you need with a combination of the current builtin functions, iteration and some other constructs. Having the option to do replace on patterns rather than values seems like a good addition nonetheless :)

@boranx
Copy link
Member

boranx commented Sep 25, 2022

Hey folks, I wanted to give it a shot and created a small PR to implement the proposal

@anderseknert
Copy link
Member

Awesome, @boranx 👏 😃

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

Successfully merging a pull request may close this issue.

3 participants