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

Object-Entity Mapping (OEM) #72

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
updates
Signed-off-by: Shaobo He <shaobohe@amazon.com>
  • Loading branch information
shaobo-he-aws committed Jul 3, 2024
commit e2a277dc485da3f3cdcf69494499640bd4fa8550
16 changes: 10 additions & 6 deletions text/0072-oem.md
Original file line number Diff line number Diff line change
@@ -23,18 +23,20 @@ The following Rust code defines how type `Team` and type `Member` map to entity
struct Team {
#[cedar(eid)]
id: String,
members: HashSet<Member>
members: HashSet<Member>,
#[cedar(skip)]
nickname: String,
}

#[derive(Debug, Clone, Cedar)]
#[cedar(transparent)]
#[cedar(type=record)]
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,
An object `Team { id: "A", nickname: "AAA", members: { Member { name: "alice", location: Some("West")}, Member { name: "bob", location: None}}}` is converted to the following entities in JSON,

```JSON
[
@@ -45,7 +47,7 @@ An object `Team { id: "A", members: { Member { name: "alice", location: Some("We
"members": [
{
"name": "alice",
"location" "West"
"region": "West"
},
{
"name": "bob"
@@ -67,9 +69,9 @@ Moreover, OEM can potentially facilitate the adoption of Cedar on applications u

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 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(type=record)]` 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.
The implementation of `Into<Entities>` for any type not annotated with the `#[cedar(type=entity)]` 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

@@ -80,6 +82,8 @@ We require all types of struct fields to implement `Into<Option<Value>>`. The `O

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.

We allow the conversion to ignore certain fields by annotating them with `#[cedar(skip)]`. Note that this makes OEM unidirectional. That is, we can only convert objects into entities, not the opposite.

### Eid mapping

We use clause `#[cedar(eid)]` to denote the field corresponding to the `Eid`. We require the field type to implement `ToString`.