Skip to content

Commit

Permalink
Fix protobuf deserialization of ValidatorEntityType
Browse files Browse the repository at this point in the history
Signed-off-by: Shaobo He <shaobohe@amazon.com>
  • Loading branch information
shaobo-he-aws committed Feb 6, 2025
1 parent 2305dba commit 919ef6c
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions cedar-policy-validator/src/schema/entity_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,28 +193,41 @@ impl From<&proto::ValidatorEntityType> for ValidatorEntityType {
// PANIC SAFETY: experimental feature
#[allow(clippy::expect_used)]
fn from(v: &proto::ValidatorEntityType) -> Self {
let tags = match &v.tags {
Some(tags) => tags.optional_type.as_ref().map(Type::from),
None => None,
};
let name = ast::EntityType::from(
v.name
.as_ref()
.expect("`as_ref()` for field that should exist"),
);
let descendants = v.descendants.iter().map(ast::EntityType::from).collect();
Self {
name: ast::EntityType::from(
v.name
.as_ref()
.expect("`as_ref()` for field that should exist"),
),
descendants: v.descendants.iter().map(ast::EntityType::from).collect(),
kind: ValidatorEntityTypeKind::Standard(StandardValidatorEntityType {
attributes: Attributes::from(
v.attributes
.as_ref()
.expect("`as_ref()` for field that should exist"),
name,
descendants,
// We use emptiness of the `enums` field as an indictor to tell if `v` represents an enumerated entity type or not
// In other words, we essentially ignore other fields like `attributes` when `enums` is empty
kind: match &v.enums[..] {
[] => {
let tags = match &v.tags {
Some(tags) => tags.optional_type.as_ref().map(Type::from),
None => None,
};
ValidatorEntityTypeKind::Standard(StandardValidatorEntityType {
attributes: Attributes::from(
v.attributes
.as_ref()
.expect("`as_ref()` for field that should exist"),
),
open_attributes: OpenTag::from(
&proto::OpenTag::try_from(v.open_attributes)
.expect("decode should succeed"),
),
tags,
})
}
enums => ValidatorEntityTypeKind::Enum(
NonEmpty::collect(enums.iter().map(SmolStr::new))
.expect("enums should be nonempty"),
),
open_attributes: OpenTag::from(
&proto::OpenTag::try_from(v.open_attributes).expect("decode should succeed"),
),
tags,
}),
},
}
}
}

0 comments on commit 919ef6c

Please sign in to comment.