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
1 change: 1 addition & 0 deletions cedar-policy/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Cedar Language Version: 4.0
typename that can resolve to either an entity or common type, matching the
behavior of typenames written in the human-readable (Cedar) syntax. (#1060, as
part of resolving #579)
- Add convenience methods to see how many policies and templates a policy set has (#1179)

### Changed

Expand Down
12 changes: 12 additions & 0 deletions cedar-policy/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,18 @@ impl PolicySet {
self.ast.is_empty()
}

/// Returns the number of `Policy`s in the `PolicySet`.
///
/// This will include both static and template-linked policies.
pub fn num_of_policies(&self) -> usize {
self.policies.len()
}

/// Returns the number of `Template`s in the `PolicySet`.
pub fn num_of_templates(&self) -> usize {
self.templates.len()
}

/// Attempt to link a template and add the new template-linked policy to the policy set.
/// If link fails, the `PolicySet` is not modified.
/// Failure can happen for three reasons
Expand Down
14 changes: 7 additions & 7 deletions cedar-policy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,8 +720,8 @@ mod policy_set_tests {
)
.expect("Link failure");

assert_eq!(pset.templates().count(), 1);
assert_eq!(pset.policies().count(), 2);
assert_eq!(pset.num_of_templates(), 1);
assert_eq!(pset.num_of_policies(), 2);
assert_eq!(pset.policies().filter(|p| p.is_static()).count(), 1);

assert_eq!(
Expand Down Expand Up @@ -4211,7 +4211,7 @@ mod policy_set_est_tests {
let pset2 = PolicySet::from_json_value(json).unwrap();

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

Expand Down Expand Up @@ -4297,8 +4297,8 @@ mod policy_set_est_tests {
});

let policyset = PolicySet::from_json_value(value).unwrap();
assert_eq!(policyset.templates().count(), 0);
assert_eq!(policyset.policies().count(), 1);
assert_eq!(policyset.num_of_templates(), 0);
assert_eq!(policyset.num_of_policies(), 1);
assert!(policyset.policy(&PolicyId::new("policy1")).is_some());
}

Expand Down Expand Up @@ -4370,8 +4370,8 @@ mod policy_set_est_tests {
});

let policyset = PolicySet::from_json_value(value).unwrap();
assert_eq!(policyset.policies().count(), 2);
assert_eq!(policyset.templates().count(), 1);
assert_eq!(policyset.num_of_policies(), 2);
assert_eq!(policyset.num_of_templates(), 1);
assert!(policyset.template(&PolicyId::new("template")).is_some());
let link = policyset.policy(&PolicyId::new("link")).unwrap();
assert_eq!(link.template_id(), Some(&PolicyId::new("template")));
Expand Down
Loading