Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add APIs to get schema annotations #1389

Merged
merged 9 commits into from
Jan 13, 2025

Conversation

shaobo-he-aws
Copy link
Contributor

@shaobo-he-aws shaobo-he-aws commented Dec 20, 2024

Description of changes

Issue #, if available

Checklist for requesting a review

The change in this PR is (choose one, and delete the other options):

  • A backwards-compatible change requiring a minor version bump to cedar-policy (e.g., addition of a new API).

I confirm that this PR (choose one, and delete the other options):

  • Updates the "Unreleased" section of the CHANGELOG with a description of my change (required for major/minor version bumps).

I confirm that cedar-spec (choose one, and delete the other options):

  • Does not require updates because my change does not impact the Cedar formal model or DRT infrastructure.

I confirm that docs.cedarpolicy.com (choose one, and delete the other options):

  • Does not require updates because my change does not impact the Cedar language specification.

Signed-off-by: Shaobo He <shaobohe@amazon.com>
Signed-off-by: Shaobo He <shaobohe@amazon.com>
Comment on lines +1387 to +1389
/// Returns `None` if `namespace` is not found in the [`SchemaFragment`] or
/// `ty` is not a valid common type ID or `ty` is not found in the
/// corresponding namespace definition
Copy link
Contributor

Choose a reason for hiding this comment

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

overloading the None case here isn't great. Maybe we could publicly expose an Id data type so that error can be reported sooner? Though, using that in other places in the API would probably be a breaking change, so perhaps it's better to stick with this signature for now

Copy link
Contributor

@john-h-kastner-aws john-h-kastner-aws left a comment

Choose a reason for hiding this comment

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

Also, this doesn't add an API for getting annotations on entity/context attributes. That's probably a more complicated API due to the recursive structure of types, but can you open a issue so we don't forget about it if you don't plan on doing it in this PR?

Co-authored-by: John Kastner <130772734+john-h-kastner-aws@users.noreply.github.com>
@shaobo-he-aws
Copy link
Contributor Author

Also, this doesn't add an API for getting annotations on entity/context attributes. That's probably a more complicated API due to the recursive structure of types, but can you open a issue so we don't forget about it if you don't plan on doing it in this PR?

Good idea. Issue: #1404

Signed-off-by: Shaobo He <shaobohe@amazon.com>
pub fn namespace_annotations(
&self,
namespace: EntityNamespace,
) -> Option<impl Iterator<Item = (&str, &str)>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Having all of these APIs return Iterator<Item = (&str, &str)> is good, but I wonder if we need an additional API to get the value of an annotation with a user-specified key. For instance if I want to specifically look up the value of the foo annotation on a particular namespace. As is, I'd be forced to iterate through the entire iterator looking for the key foo. We could solve this either by adding a separate API namespace_annotation(&self, namespace, key) or perhaps by returning a map structure (HashMap or BTreeMap) instead of an iterator in this API, so that users could do their own lookups.

Same for the other new annotation-getting APIs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I think that having an iterator is a generic. That is, users can collect them into a HashMap or BTreeMap given their preferences (e.g., performance vs order). I also think it may be a good idea to let user collect the iterator and get the value by key, instead of having APIs. Maybe I'm too paranoid about breaking changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess I'm concerned that having the user collect the iterator in order to get the value by key is

  1. a little complicated, compared to having a direct method for it -- so, creates a suboptimal user experience
  2. potentially poor performance, as getting any value by key at all requires collecting the entire iterator

If we're worried about breaking changes, we don't have to return a map structure, we can just implement our own getter, something like:

pub fn namespace_annotation(
    &self,
    namespace: EntityNamespace,
    annotation_key: &str,
) -> Option<&str> { ... }

that gets a particular annotation value given a key. I don't think adding a method like this is overly committing to any particular implementation details

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I can do it.

Co-authored-by: Craig Disselkoen <cdiss@amazon.com>
Copy link
Contributor

@john-h-kastner-aws john-h-kastner-aws left a comment

Choose a reason for hiding this comment

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

Approving since the code looks good to merge as is. I agree with craig that we should provide direct accessor methods also/instead of the iterators

@shaobo-he-aws
Copy link
Contributor Author

Approving since the code looks good to merge as is. I agree with craig that we should provide direct accessor methods also/instead of the iterators

Thanks for the review. I'll add these APIs.

shaobo-he-aws and others added 4 commits January 8, 2025 10:11
Co-authored-by: John Kastner <130772734+john-h-kastner-aws@users.noreply.github.com>
Signed-off-by: Shaobo He <shaobohe@amazon.com>
@shaobo-he-aws shaobo-he-aws merged commit 0252988 into main Jan 13, 2025
19 checks passed
@shaobo-he-aws shaobo-he-aws deleted the feature/shaobo/annotation-getters branch January 13, 2025 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants