Skip to content
Merged
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
20 changes: 20 additions & 0 deletions cedar-policy-core/src/ast/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,26 @@ impl Entity {
pub fn add_ancestor(&mut self, uid: EntityUID) {
self.ancestors.insert(uid);
}

/// Consume the entity and return the entity's owned Uid, attributes and parents.
pub fn into_inner(
self,
) -> (
EntityUID,
HashMap<SmolStr, PartialValue>,
HashSet<EntityUID>,
) {
let Self {
uid,
attrs,
ancestors,
} = self;
(
uid,
attrs.into_iter().map(|(k, v)| (k, v.0)).collect(),
ancestors,
)
}
}

impl PartialEq for Entity {
Expand Down
1 change: 1 addition & 0 deletions cedar-policy/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `wasm` Cargo feature for targeting Wasm
- For the `partial-eval` experimental feature: added
`Authorizer::evaluate_policies_partial()` (#474)
- `Entity::into_inner` (resolving #636)

### Changed

Expand Down
36 changes: 35 additions & 1 deletion cedar-policy/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,40 @@ impl Entity {
};
Some(Ok(EvalResult::from(v)))
}

/// Consume the entity and return the entity's owned Uid, attributes and parents.
pub fn into_inner(
self,
) -> (
EntityUid,
HashMap<String, RestrictedExpression>,
HashSet<EntityUid>,
) {
let (uid, attrs, ancestors) = self.0.into_inner();

let attrs = attrs
.into_iter()
.map(|(k, v)| {
(
k.to_string(),
match v {
ast::PartialValue::Value(val) => RestrictedExpression(
ast::RestrictedExpr::new_unchecked(ast::Expr::from(val)),
),
ast::PartialValue::Residual(exp) => {
RestrictedExpression(ast::RestrictedExpr::new_unchecked(exp))
}
},
)
})
.collect();

(
EntityUid(uid),
attrs,
ancestors.into_iter().map(EntityUid).collect(),
)
}
}

impl std::fmt::Display for Entity {
Expand Down Expand Up @@ -759,7 +793,7 @@ impl Authorizer {
EvaluationResponse {
satisfied_permits: satisfied_permits.into_iter().map(PolicyId).collect(),
satisfied_forbids: satisfied_forbids.into_iter().map(PolicyId).collect(),
errors: errors.into_iter().map(|e| e.into()).collect(),
errors: errors.into_iter().map(Into::into).collect(),
permit_residuals: PolicySet::from_ast(permit_residuals),
forbid_residuals: PolicySet::from_ast(forbid_residuals),
}
Expand Down
4 changes: 3 additions & 1 deletion cedar-policy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,9 @@ mod entity_validate_tests {
HashSet::new(),
)
.unwrap();
validate_entity(entity, &schema()).unwrap();
validate_entity(entity.clone(), &schema()).unwrap();
let (uid, attrs, parents) = entity.into_inner();
validate_entity(Entity::new(uid, attrs, parents).unwrap(), &schema()).unwrap();
}

#[test]
Expand Down