Skip to content

Commit 0289dfc

Browse files
Add methods to get number of policies and templates in a PolicySet (#1180)
Signed-off-by: Juan Vicente Garcia Orozco <[email protected]> Co-authored-by: Juan Vicente Garcia Orozco <[email protected]>
1 parent 12cbc25 commit 0289dfc

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

cedar-policy/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Cedar Language Version: 4.0
2929
typename that can resolve to either an entity or common type, matching the
3030
behavior of typenames written in the human-readable (Cedar) syntax. (#1060, as
3131
part of resolving #579)
32+
- Add convenience methods to see how many policies and templates a policy set has (#1179)
3233

3334
### Changed
3435

cedar-policy/src/api.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,18 @@ impl PolicySet {
20572057
self.ast.is_empty()
20582058
}
20592059

2060+
/// Returns the number of `Policy`s in the `PolicySet`.
2061+
///
2062+
/// This will include both static and template-linked policies.
2063+
pub fn num_of_policies(&self) -> usize {
2064+
self.policies.len()
2065+
}
2066+
2067+
/// Returns the number of `Template`s in the `PolicySet`.
2068+
pub fn num_of_templates(&self) -> usize {
2069+
self.templates.len()
2070+
}
2071+
20602072
/// Attempt to link a template and add the new template-linked policy to the policy set.
20612073
/// If link fails, the `PolicySet` is not modified.
20622074
/// Failure can happen for three reasons

cedar-policy/src/tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,8 @@ mod policy_set_tests {
720720
)
721721
.expect("Link failure");
722722

723-
assert_eq!(pset.templates().count(), 1);
724-
assert_eq!(pset.policies().count(), 2);
723+
assert_eq!(pset.num_of_templates(), 1);
724+
assert_eq!(pset.num_of_policies(), 2);
725725
assert_eq!(pset.policies().filter(|p| p.is_static()).count(), 1);
726726

727727
assert_eq!(
@@ -4211,7 +4211,7 @@ mod policy_set_est_tests {
42114211
let pset2 = PolicySet::from_json_value(json).unwrap();
42124212

42134213
// There should be 2 policies, one static and two links
4214-
assert_eq!(pset2.policies().count(), 3);
4214+
assert_eq!(pset2.num_of_policies(), 3);
42154215
let static_policy = pset2.policy(&PolicyId::new("policy")).unwrap();
42164216
assert!(static_policy.is_static());
42174217

@@ -4297,8 +4297,8 @@ mod policy_set_est_tests {
42974297
});
42984298

42994299
let policyset = PolicySet::from_json_value(value).unwrap();
4300-
assert_eq!(policyset.templates().count(), 0);
4301-
assert_eq!(policyset.policies().count(), 1);
4300+
assert_eq!(policyset.num_of_templates(), 0);
4301+
assert_eq!(policyset.num_of_policies(), 1);
43024302
assert!(policyset.policy(&PolicyId::new("policy1")).is_some());
43034303
}
43044304

@@ -4370,8 +4370,8 @@ mod policy_set_est_tests {
43704370
});
43714371

43724372
let policyset = PolicySet::from_json_value(value).unwrap();
4373-
assert_eq!(policyset.policies().count(), 2);
4374-
assert_eq!(policyset.templates().count(), 1);
4373+
assert_eq!(policyset.num_of_policies(), 2);
4374+
assert_eq!(policyset.num_of_templates(), 1);
43754375
assert!(policyset.template(&PolicyId::new("template")).is_some());
43764376
let link = policyset.policy(&PolicyId::new("link")).unwrap();
43774377
assert_eq!(link.template_id(), Some(&PolicyId::new("template")));

0 commit comments

Comments
 (0)