Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion cedar-policy-core/src/ast/policy_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,16 @@ impl PolicySet {
self.templates.is_empty() && self.links.is_empty()
}

/// Lookup a template by policy id
/// Lookup a template by policy id, returns [`Option<Arc<Template>>`]
pub fn get_template(&self, id: &PolicyID) -> Option<Arc<Template>> {
self.templates.get(id).cloned()
}

/// Lookup a template by policy id, returns [`Option<&Template>`]
pub fn get_template_ref(&self, id: &PolicyID) -> Option<&Template> {
self.templates.get(id).map(AsRef::as_ref)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in other places we call these get_template_arc and just get_template. Should we change the other places to match this convention?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think get_template_arc and get_template make more sense to me.


/// Lookup an policy by policy id
pub fn get(&self, id: &PolicyID) -> Option<&Policy> {
self.links.get(id)
Expand Down
12 changes: 4 additions & 8 deletions cedar-policy/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1913,23 +1913,19 @@ impl PolicySet {
}

/// Extract annotation data from a `Policy` by its `PolicyId` and annotation key
pub fn annotation<'a>(&'a self, id: &PolicyId, key: impl AsRef<str>) -> Option<&'a str> {
pub fn annotation(&self, id: &PolicyId, key: impl AsRef<str>) -> Option<&str> {
self.ast
.get(id.as_ref())?
.annotation(&key.as_ref().parse().ok()?)
.map(AsRef::as_ref)
}

/// Extract annotation data from a `Template` by its `PolicyId` and annotation key.
//
// TODO: unfortunate that this method returns `Option<String>` and the corresponding method
// for policies (`.annotation()`) above returns `Option<&str>`, but this can't be changed
// without a semver break
pub fn template_annotation(&self, id: &PolicyId, key: impl AsRef<str>) -> Option<String> {
pub fn template_annotation(&self, id: &PolicyId, key: impl AsRef<str>) -> Option<&str> {
self.ast
.get_template(id.as_ref())?
.get_template_ref(id.as_ref())?
.annotation(&key.as_ref().parse().ok()?)
.map(|annot| annot.val.to_string())
.map(AsRef::as_ref)
}

/// Returns true iff the `PolicySet` is empty
Expand Down
2 changes: 1 addition & 1 deletion cedar-policy/tests/public_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ fn policy_annotations() {
assert_eq!(s.annotation(&pid, "anno"), Some("good annotation"));
assert_eq!(
s.template_annotation(&tid, "tanno"),
Some("good annotation".to_string())
Some("good annotation")
);
}

Expand Down