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: add regex.replace(s, p, v) to builtin #5179

Merged
merged 2 commits into from Sep 27, 2022

Conversation

boranx
Copy link
Member

@boranx boranx commented Sep 25, 2022

Fix: #5162

though I'm not very confident in this part of the codebase, I wanted to raise a small PR to implement the proposal above. Thanks for the reviews in advance :)

@netlify
Copy link

netlify bot commented Sep 25, 2022

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 8d3c2766bbd1532dc624fbbac6274b3d60b78ed8
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/63303f36510f9800085dd208
😎 Deploy Preview https://deploy-preview-5179--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Sep 25, 2022

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit bb48782f87046cb0b2b8ffb5a9c2c59a48dd4957
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/63303f6f1c27620008a7571d
😎 Deploy Preview https://deploy-preview-5179--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Sep 25, 2022

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 8ce699c
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/6332e8ff90bff60008eb017d
😎 Deploy Preview https://deploy-preview-5179--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

srenatus
srenatus previously approved these changes Sep 25, 2022
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one nitpick from my end. Thanks for contributing! 🎉

topdown/regex.go Outdated
@@ -215,4 +241,5 @@ func init() {
RegisterFunctionalBuiltin4(ast.RegexTemplateMatch.Name, builtinRegexMatchTemplate)
RegisterFunctionalBuiltin3(ast.RegexFind.Name, builtinRegexFind)
RegisterFunctionalBuiltin3(ast.RegexFindAllStringSubmatch.Name, builtinRegexFindAllStringSubmatch)
RegisterFunctionalBuiltin3(ast.RegexReplace.Name, builtinRegexReplace)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This register function is deprecated, please use RegisterBuiltinFunc, and adapt the function signature accordingly... 😳 I'm sorry it's hard to avoid this trap, we should be updating all the register calls eventually...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed! I had a branch up on my fork at one point for refactoring away the older style, at least within the standard library of builtins. I'll have to see if I can revive it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the review! I've transformed the registration to be using RegisterBuiltinFunc(). btw (just an idea) given all this transformation is straightforward and low-hanging fruits for new comers, it might be beneficial to mark those as good-first-issue

egrep -rnw "RegisterFunctionalBuiltin2|RegisterFunctionalBuiltin3|RegisterFunctionalBuiltin4" ./ 2>/dev/null | wc -l
51

@@ -0,0 +1,14 @@
cases:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think you’ll need to be that comprehensive here, but a single test for an entire built-in function seems a bit sparse. How does this work with eg capturing group substitutions? How are errors dealt with?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea! I've covered applying the pattern to subgroups and the error case. Currently, it returns the err as similar to what happens in other regex functions. please let me know if those are enough or if we need more cases to be written

Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! WDYT, @anderseknert ?

Copy link
Member

@anderseknert anderseknert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! Thank you @boranx 😃

Signed-off-by: boranx <boran.seref@gmail.com>
…and migrate to RegisterBuiltinFunc

Signed-off-by: boranx <boran.seref@gmail.com>
@boranx
Copy link
Member Author

boranx commented Sep 27, 2022

I think I can merge considering this is approved already (using squash&merge)

@boranx boranx merged commit 1d2173b into open-policy-agent:main Sep 27, 2022
@datfinesoul
Copy link

Thank you for implementing this!

byronic pushed a commit to byronic/opa that referenced this pull request Oct 17, 2022
* feat: add regex.replace(s, p, v) to builtin

Signed-off-by: boranx <boran.seref@gmail.com>
Signed-off-by: Byron Lagrone <byron.lagrone@seqster.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: regex.replace(search, pattern, value)
5 participants