-
Notifications
You must be signed in to change notification settings - Fork 9
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
Object-Entity Mapping (OEM) #72
Open
shaobo-he-aws
wants to merge
2
commits into
main
Choose a base branch
from
oem
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Object-Entity Mapping (OEM) | ||
|
||
## Related issues and PRs | ||
|
||
- Reference Issues: https://github.com/cedar-policy/cedar/issues/986 | ||
- Implementation PR(s): (leave this empty) | ||
|
||
## Timeline | ||
|
||
- Started: 2024-06-28 | ||
|
||
## Summary | ||
|
||
Similar to [Object-Relational Mapping (ORM)](https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping), this RFC proposes a macro-based conversion between Rust objects and entities. | ||
|
||
## Basic example | ||
|
||
The following Rust code defines how type `Team` and type `Member` map to entity type `Example::Team`. | ||
|
||
```Rust | ||
#[derive(Debug, Clone, Cedar)] | ||
#[cedar(rename="Example::Team")] | ||
struct Team { | ||
#[cedar(eid)] | ||
id: String, | ||
members: HashSet<Member> | ||
} | ||
|
||
#[derive(Debug, Clone, Cedar)] | ||
#[cedar(transparent)] | ||
struct Member { | ||
name: String, | ||
#[cedar(rename="region")] | ||
location: Option<String>, | ||
} | ||
``` | ||
An object `Team { id: "A", members: { Member { name: "alice", location: Some("West")}, Member { name: "bob", location: None}}}` is converted to the following entities in JSON, | ||
|
||
```JSON | ||
[ | ||
{ | ||
{ | ||
"uid": { "type": "Team", "id": "A"}, | ||
"attrs": { | ||
"members": [ | ||
{ | ||
"name": "alice", | ||
"location" "West" | ||
}, | ||
{ | ||
"name": "bob" | ||
} | ||
] | ||
}, | ||
"parents": [] | ||
} | ||
} | ||
] | ||
``` | ||
## Motivation | ||
|
||
There currently do not exist any convenient methods to programmatically convert Rust objects to entities or vice versa. The status quo of converting a Rust object to an entity is to create an entity without any attributes and then insert attributes. The whole process is akin to that of *manually* serializing Rust objects to JSON, the latter of which is automated by serde. Users should be able to do the same for Rust object to entity conversion. This RFC addresses this need using Rust derive macros. | ||
|
||
Moreover, OEM can potentially facilitate the adoption of Cedar on applications using databases. For instance, developers can leverage ORM to seamlessly convert results of database queries into Rust objects and then into entities. That is, developers are able to conveniently construct authorization requests by fetching data from databases. | ||
|
||
## Detailed design | ||
|
||
Given limited knowledge about Rust macros the proposer has, the design details are subject to significant changes. | ||
|
||
The macros over a Rust type eventually expand to implementations of `Into<Entities>`. In the meantime, they also give rise to implementations of `Into<Value>` for this type. By default, the implementation of `Into<Value` for a type produces an entity `uid` unless the type is annotated with the `#[cedar(transparent)]` clause, where it produces a record. | ||
|
||
The implementation of `Into<Entities>` for any type annotated with the `#[cedar(transparent)]` clause produces an empty entity slice. Those for types without such annotations construct an entity by iterating its fields. If a field type corresponds to an entity type, the entity slice derived from the field is merged into the final resulting entity slice. That is the reason why a Rust object converts to an entity slice instead of a single entity. | ||
|
||
### Value mapping | ||
|
||
We require all types of struct fields to implement `Into<Option<Value>>`. The `Option` here is to encode optional attributes. We provide default implementations for various primitive types, `Option<T:Into<Value>>`, `HashMap<S:ToString,T:Into<Value>>`, `HashMap<S:ToString,Option<T:Into<Value>>>`, `HashSet<T:Into<Value>>`. | ||
|
||
|
||
### Entity type and attribute name mapping | ||
|
||
We use clause `#[cedar(rename=...)]` to describe entity type/attribute name mappings, where the argument is the string representation of an entity type or attribute name. Entity type/attribute name is the Rust type/field name without such clause. | ||
|
||
### Eid mapping | ||
|
||
We use clause `#[cedar(eid)]` to denote the field corresponding to the `Eid`. We require the field type to implement `ToString`. | ||
|
||
## Drawbacks | ||
|
||
Rust macros are unstable and hard to write. Moreover, users may find manual construction of entities not that hard. | ||
|
||
## Alternatives | ||
|
||
Manual construction of entities. | ||
|
||
## Unresolved questions | ||
|
||
How do we describe membership relations? |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather see this made more explicit. E.g.,
#[cedar(record)]
or#[cedar(entity)]
, perhaps with the entity case being assumed by default.This could also support a nice syntax like
#[cedar(long)] struct MyLong(i32)
for wrapper types that might exist.