Skip to content

Conversation

@andreaTP
Copy link
Collaborator

Implement the sprintf String builtin.

@anderseknert I was unable to find testcases to cover the edge cases, do you have a link to spare?

Copy link
Contributor

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

There aren't that many tests for sprintf I'm afraid, as the OPA implementation simply mirrors the Go one. This was a mistake that has been problematic for all alternative implementations, as some things that Go's sprintf allows isn't feasible to implement in other languages. So the best thing is likely just to provide a best-effort implementation and to make it very clear that for anything non-trivial, the result is likely going to differ or not work at all.

The code looks good to me, but if you have somewhere where you've documented the supported built-is, it would be good to add a note about this there.

@andreaTP
Copy link
Collaborator Author

So the best thing is likely just to provide a best-effort implementation and to make it very clear that for anything non-trivial, the result is likely going to differ or not work at all.

I see that it's already marked as "SDK-dependent":
https://www.openpolicyagent.org/docs/latest/policy-reference/#builtin-strings-sprintf

do you think we should do anything more?

it would be good to add a note about this there.

right, done, thanks for the reminder!

Readme.md Outdated
At the moment the following builtins are supported(and, by default, automatically injected when needed):

- String
- `sprintf`
Copy link
Contributor

Choose a reason for hiding this comment

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

Good! What I alluded to before is that it would be really good to mention here how this built-in won't behave exactly as sprintf in OPA, and should ideally only be used for simple operations, like %s, %d and such.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the feedback, I added a note, let me know how it reads 🙏

Copy link
Contributor

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

Great! Thanks 👍

@andreaTP andreaTP merged commit 9967fe5 into StyraOSS:main Jan 15, 2025
5 checks passed
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.

2 participants