Skip to content

Conversation

@dev-rice
Copy link
Contributor

@dev-rice dev-rice commented May 2, 2025

This adds a new initializer that accepts an Encodable type, which I imagine being used frequently for authorization checks using dynamic input. The trait I want to introduce is that any Encodable type should be able to be turned into a RegoValue without 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 an Encoder implementation specific to RegoValue that does the conversion without the serialization cost.

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 <[email protected]>
}

// Initialize a RegoValue from a type that can be JSON-encoded
public init(encodable: Encodable) throws {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

[
"name": "Mr. Meowgi",
"age": 4,
// With the default JSONEncoder, nil Optionals are not included in the serialized JSON, so 'sibling' is missing.
Copy link
Contributor Author

@dev-rice dev-rice May 2, 2025

Choose a reason for hiding this comment

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

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.

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.

1 participant