Skip to content

Commit

Permalink
re-enable common type annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Shaobo He <[email protected]>
  • Loading branch information
shaobo-he-aws committed Dec 10, 2024
1 parent e6737f8 commit 28cf187
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cedar-policy-validator/src/cedar_schema/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ 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.0.data)?
}
for (n, ty) in &self.entity_types {
writeln!(f, "entity {n}{};", ty)?
Expand Down
14 changes: 12 additions & 2 deletions cedar-policy-validator/src/cedar_schema/to_json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ use super::{
},
err::{schema_warnings, SchemaWarning, ToJsonSchemaError, ToJsonSchemaErrors},
};
use crate::{annotations, cedar_schema, json_schema, RawName};
use crate::{
annotations, cedar_schema,
json_schema::{self, AnnotatedType},
RawName,
};

impl From<cedar_schema::Path> for RawName {
fn from(p: cedar_schema::Path) -> Self {
Expand Down Expand Up @@ -196,7 +200,13 @@ impl TryFrom<annotations::Annotated<Namespace>> for json_schema::NamespaceDefini
.map_err(|e| ToJsonSchemaError::reserved_name(e.name(), name_loc.clone()))?;
let ctid = json_schema::CommonTypeId::new(id)
.map_err(|e| ToJsonSchemaError::reserved_keyword(e.id, name_loc))?;
Ok((ctid, cedar_type_to_json_type(decl.data.def)))
Ok((
ctid,
AnnotatedType(annotations::Annotated {
data: cedar_type_to_json_type(decl.data.def),
annotations: decl.annotations,
}),
))
})
.collect::<Result<_, ToJsonSchemaError>>()?;

Expand Down
37 changes: 26 additions & 11 deletions cedar-policy-validator/src/json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub struct NamespaceDefinition<N> {
#[serde(default)]
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
#[serde(with = "::serde_with::rust::maps_duplicate_key_is_error")]
pub common_types: BTreeMap<CommonTypeId, Type<N>>,
pub common_types: BTreeMap<CommonTypeId, AnnotatedType<N>>,
#[serde(with = "::serde_with::rust::maps_duplicate_key_is_error")]
pub entity_types: BTreeMap<UnreservedId, EntityType<N>>,
#[serde(with = "::serde_with::rust::maps_duplicate_key_is_error")]
Expand Down Expand Up @@ -336,17 +336,25 @@ impl NamespaceDefinition<RawName> {
common_types: self
.common_types
.into_iter()
.map(|(k, v)| (k, v.conditionally_qualify_type_references(ns).into()))
.map(|(k, v)| {
(
k,
AnnotatedType(Annotated {
data: v.0.data.conditionally_qualify_type_references(ns),
annotations: v.0.annotations,
}),
)
})
.collect(),
entity_types: self
.entity_types
.into_iter()
.map(|(k, v)| (k, v.conditionally_qualify_type_references(ns).into()))
.map(|(k, v)| (k, v.conditionally_qualify_type_references(ns)))
.collect(),
actions: self
.actions
.into_iter()
.map(|(k, v)| (k, v.conditionally_qualify_type_references(ns).into()))
.map(|(k, v)| (k, v.conditionally_qualify_type_references(ns)))
.collect(),
annotations: self.annotations,
}
Expand All @@ -368,17 +376,25 @@ impl NamespaceDefinition<ConditionalName> {
common_types: self
.common_types
.into_iter()
.map(|(k, v)| Ok((k, v.fully_qualify_type_references(all_defs)?.into())))
.map(|(k, v)| {
Ok((
k,
AnnotatedType(Annotated {
data: v.0.data.fully_qualify_type_references(all_defs)?,
annotations: v.0.annotations,
}),
))
})
.collect::<std::result::Result<_, TypeNotDefinedError>>()?,
entity_types: self
.entity_types
.into_iter()
.map(|(k, v)| Ok((k, v.fully_qualify_type_references(all_defs)?.into())))
.map(|(k, v)| Ok((k, v.fully_qualify_type_references(all_defs)?)))
.collect::<std::result::Result<_, TypeNotDefinedError>>()?,
actions: self
.actions
.into_iter()
.map(|(k, v)| Ok((k, v.fully_qualify_type_references(all_defs)?.into())))
.map(|(k, v)| Ok((k, v.fully_qualify_type_references(all_defs)?)))
.collect::<Result<_>>()?,
annotations: self.annotations,
})
Expand Down Expand Up @@ -3214,10 +3230,9 @@ mod annotations {
"actions": {},
"commonTypes": {
"Task": {
// Will allow later
//"annotations": {
// "doc": "a common type representing a task"
//},
"annotations": {
"doc": "a common type representing a task"
},
"type": "Record",
"attributes": {
"id": {
Expand Down
10 changes: 8 additions & 2 deletions cedar-policy-validator/src/schema/namespace_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,14 @@ impl ValidatorNamespaceDef<ConditionalName, ConditionalName> {

// Convert the common types, actions and entity types from the schema
// file into the representation used by the validator.
let common_types =
CommonTypeDefs::from_raw_common_types(namespace_def.common_types, namespace.as_ref())?;
let common_types = CommonTypeDefs::from_raw_common_types(
namespace_def
.common_types
.into_iter()
.map(|(key, value)| (key, value.0.data))
.collect(),
namespace.as_ref(),
)?;
let actions =
ActionsDef::from_raw_actions(namespace_def.actions, namespace.as_ref(), extensions)?;
let entity_types =
Expand Down

0 comments on commit 28cf187

Please sign in to comment.