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

RFC 48 prototype #1316

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 8 additions & 6 deletions cedar-policy-validator/src/cedar_schema/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ impl<N: Display> Display for json_schema::Fragment<N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (ns, def) in &self.0 {
match ns {
None => write!(f, "{def}")?,
Some(ns) => write!(f, "namespace {ns} {{\n{def}}}\n")?,
None => write!(f, "{}", def.data)?,
Some(ns) => write!(f, "namespace {ns} {{\n{}}}\n", def.data)?,
}
}
Ok(())
Expand All @@ -42,13 +42,13 @@ impl<N: Display> Display for json_schema::Fragment<N> {
impl<N: Display> Display for json_schema::NamespaceDefinition<N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (n, ty) in &self.common_types {
writeln!(f, "type {n} = {ty};")?
writeln!(f, "type {n} = {};", ty.data)?
}
for (n, ty) in &self.entity_types {
writeln!(f, "entity {n}{ty};")?
writeln!(f, "entity {n}{};", ty.data)?
}
for (n, a) in &self.actions {
writeln!(f, "action \"{}\"{a};", n.escape_debug())?
writeln!(f, "action \"{}\"{};", n.escape_debug(), a.data)?
}
Ok(())
}
Expand Down Expand Up @@ -83,7 +83,7 @@ impl<N: Display> Display for json_schema::RecordType<N> {
"\"{}\"{}: {}",
n.escape_debug(),
if ty.required { "" } else { "?" },
ty.ty
ty.ty.0.data
)?;
if i < (self.attributes.len() - 1) {
write!(f, ", ")?;
Expand Down Expand Up @@ -209,6 +209,7 @@ pub fn json_schema_to_cedar_schema_str<N: Display>(
let mut name_collisions: Vec<SmolStr> = Vec::new();
for (name, ns) in json_schema.0.iter().filter(|(name, _)| !name.is_none()) {
let entity_types: HashSet<SmolStr> = ns
.data
.entity_types
.keys()
.map(|ty_name| {
Expand All @@ -218,6 +219,7 @@ pub fn json_schema_to_cedar_schema_str<N: Display>(
})
.collect();
let common_types: HashSet<SmolStr> = ns
.data
.common_types
.keys()
.map(|ty_name| {
Expand Down
21 changes: 14 additions & 7 deletions cedar-policy-validator/src/cedar_schema/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,18 @@ match {

// other tokens
",", ";", ":", "::", "{", "}", "[", "]",
"<", ">", "=", "?",
"<", ">", "=", "?", "@", "(", ")",

}

// Annotations := {'@' Ident '(' String ')'}
Annotation: Node<()> = {
Copy link
Contributor

@john-h-kastner-aws john-h-kastner-aws Nov 15, 2024

Choose a reason for hiding this comment

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

I wonder if we can start sharing some code between this grammar and the policy grammar. There are probably some other common bits like idents and names at least

<l:@L> "@" <key:Ident> <value: ("(" <STR> ")")?> <r:@R> => Node::with_source_loc((), Loc::new(l..r, Arc::clone(src)))
}

Annotated<E>: E = {
<annotations: Annotation*> <e:E> => e,
}

// Schema := {Namespace}
pub Schema: ASchema = {
Expand All @@ -94,9 +102,9 @@ pub Schema: ASchema = {

// Namespace := 'namespace' Path '{' {Decl} '}'
Namespace: Node<Namespace> = {
<l:@L> NAMESPACE <p: Path> "{" <decls: Decl*> "}" <r:@R>
<l:@L> <annotations: Annotation*> NAMESPACE <p: Path> "{" <decls: Annotated<Decl>*> "}" <r:@R>
=> Node::with_source_loc(Namespace { name: Some(Node::with_source_loc(p, Loc::new(l..r, Arc::clone(src)))), decls}, Loc::new(l..r, Arc::clone(src))),
<l:@L> <decl: Decl> <r:@R> => Node::with_source_loc(Namespace {name: None, decls: vec![decl]}, Loc::new(l..r, Arc::clone(src))),
<l:@L> <decl: Annotated<Decl>> <r:@R> => Node::with_source_loc(Namespace {name: None, decls: vec![decl]}, Loc::new(l..r, Arc::clone(src))),
}

// Decl := Entity | Action | TypeDecl
Expand Down Expand Up @@ -183,15 +191,14 @@ pub Type: Node<SType> = {
=> Node::with_source_loc(SType::Record(ds.unwrap_or_default()), Loc::new(l..r, Arc::clone(src))),
}

// AttrDecls := Name ['?'] ':' Type [',' | ',' AttrDecls]
// AttrDecls := Annotation* Name ['?'] ':' Type [',' | ',' AttrDecls]
AttrDecls: Vec<Node<AttrDecl>> = {
<l:@L> <name: Name> <required:"?"?> ":" <ty:Type> ","? <r:@R>
<l:@L> <annotations: Annotation*> <name: Name> <required:"?"?> ":" <ty:Type> ","? <r:@R>
=> vec![Node::with_source_loc(AttrDecl { name, required: required.is_none(), ty}, Loc::new(l..r, Arc::clone(src)))],
<l:@L> <name: Name> <required:"?"?> ":" <ty:Type> "," <r:@R> <mut ds: AttrDecls>
<l:@L> <annotations: Annotation*> <name: Name> <required:"?"?> ":" <ty:Type> "," <r:@R> <mut ds: AttrDecls>
=> {ds.insert(0, Node::with_source_loc(AttrDecl { name, required: required.is_none(), ty}, Loc::new(l..r, Arc::clone(src)))); ds},
}


Comma<E>: Vec<E> = {
<e:E?> => e.into_iter().collect(),
<mut es:(<E> ",")+> <e:E> => {
Expand Down
Loading
Loading