Skip to content

Commit 72942c1

Browse files
committed
Add Entity::into_inner method
1 parent 501b2ab commit 72942c1

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

cedar-policy-core/src/ast/entity.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,26 @@ impl Entity {
356356
pub fn add_ancestor(&mut self, uid: EntityUID) {
357357
self.ancestors.insert(uid);
358358
}
359+
360+
/// Consume the entity and return the entity's owned Uid, attributes and parents.
361+
pub fn into_inner(
362+
self,
363+
) -> (
364+
EntityUID,
365+
HashMap<SmolStr, PartialValue>,
366+
HashSet<EntityUID>,
367+
) {
368+
let Self {
369+
uid,
370+
attrs,
371+
ancestors,
372+
} = self;
373+
(
374+
uid,
375+
attrs.into_iter().map(|(k, v)| (k, v.0)).collect(),
376+
ancestors,
377+
)
378+
}
359379
}
360380

361381
impl PartialEq for Entity {

cedar-policy/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- `wasm` Cargo feature for targeting Wasm
2121
- For the `partial-eval` experimental feature: added
2222
`Authorizer::evaluate_policies_partial()` (#474)
23+
- `Entity::into_inner` (resolving #636)
2324

2425
### Changed
2526

cedar-policy/src/api.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,40 @@ impl Entity {
210210
};
211211
Some(Ok(EvalResult::from(v)))
212212
}
213+
214+
/// Consume the entity and return the entity's owned Uid, attributes and parents.
215+
pub fn into_inner(
216+
self,
217+
) -> (
218+
EntityUid,
219+
HashMap<String, RestrictedExpression>,
220+
HashSet<EntityUid>,
221+
) {
222+
let (uid, attrs, ancestors) = self.0.into_inner();
223+
224+
let attrs = attrs
225+
.into_iter()
226+
.map(|(k, v)| {
227+
(
228+
k.to_string(),
229+
match v {
230+
ast::PartialValue::Value(val) => RestrictedExpression::new(
231+
ast::RestrictedExpr::new_unchecked(ast::Expr::from(val)),
232+
),
233+
ast::PartialValue::Residual(exp) => {
234+
RestrictedExpression::new(ast::RestrictedExpr::new_unchecked(exp))
235+
}
236+
},
237+
)
238+
})
239+
.collect();
240+
241+
(
242+
EntityUid(uid),
243+
attrs,
244+
ancestors.into_iter().map(EntityUid).collect(),
245+
)
246+
}
213247
}
214248

215249
impl std::fmt::Display for Entity {
@@ -759,7 +793,7 @@ impl Authorizer {
759793
EvaluationResponse {
760794
satisfied_permits: satisfied_permits.into_iter().map(PolicyId).collect(),
761795
satisfied_forbids: satisfied_forbids.into_iter().map(PolicyId).collect(),
762-
errors: errors.into_iter().map(|e| e.into()).collect(),
796+
errors: errors.into_iter().map(Into::into).collect(),
763797
permit_residuals: PolicySet::from_ast(permit_residuals),
764798
forbid_residuals: PolicySet::from_ast(forbid_residuals),
765799
}
@@ -3571,6 +3605,11 @@ impl RestrictedExpression {
35713605
[src_expr],
35723606
))
35733607
}
3608+
3609+
/// Create an expression from the AST expression
3610+
pub fn new(exp: ast::RestrictedExpr) -> Self {
3611+
Self(exp)
3612+
}
35743613
}
35753614

35763615
fn decimal_extension_name() -> ast::Name {

cedar-policy/src/tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,9 @@ mod entity_validate_tests {
15851585
HashSet::new(),
15861586
)
15871587
.unwrap();
1588-
validate_entity(entity, &schema()).unwrap();
1588+
validate_entity(entity.clone(), &schema()).unwrap();
1589+
let (uid, attrs, parents) = entity.into_inner();
1590+
validate_entity(Entity::new(uid, attrs, parents).unwrap(), &schema()).unwrap();
15891591
}
15901592

15911593
#[test]

0 commit comments

Comments
 (0)