add initializer for RegoValue from Encodable - #8
Open
dev-rice wants to merge 1 commit into
Open
Conversation
Call me lazy, but I didn't want to have to import Foundation or type the one-liner to encode my Encodable as JSON and pass it to the `AST.RegoValue.init(jsonData:)`. This adds a new initializer that accepts an Encodable type, which I imagine being used frequently for authorization checks using dynamic input. Do feel free to just call me lazy and force me to pass Data everywhere! Signed-off-by: Chris Rice <chrisrice@apple.com>
dev-rice
commented
May 2, 2025
| } | ||
|
|
||
| // Initialize a RegoValue from a type that can be JSON-encoded | ||
| public init(encodable: Encodable) throws { |
Contributor
Author
There was a problem hiding this comment.
I considered putting this in the init(from: Any) and adding a Encodable case in that switch, but figured a separate initializer was more clear. Interested to hear your alls' thoughts.
dev-rice
commented
May 2, 2025
| [ | ||
| "name": "Mr. Meowgi", | ||
| "age": 4, | ||
| // With the default JSONEncoder, nil Optionals are not included in the serialized JSON, so 'sibling' is missing. |
Contributor
Author
There was a problem hiding this comment.
Pretty sure this will be equivalent evaluation wise, but I just wanted to call it out since this pet in the RegoValue differs from the one used in the above test. If consistency is important, then we may want to go with the Encoder implementation instead of JSON round-trip from the start.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a new initializer that accepts an
Encodabletype, which I imagine being used frequently for authorization checks using dynamic input. The trait I want to introduce is that anyEncodabletype should be able to be turned into aRegoValuewithout a manual implementation of the conversion. For now, this is implemented by doing a JSON round-trip. But long term, I can see there being anEncoderimplementation specific toRegoValuethat does the conversion without the serialization cost.