generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 110
Internal data structures for Generalized templates #1732
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
Open
alanwang67
wants to merge
25
commits into
cedar-policy:main
Choose a base branch
from
alanwang67:generalized_templates_branch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3149180
slots in the condition of template
alanwang67 4934e51
fixed + added testcases
alanwang67 e3a96b5
improved comments
alanwang67 b0e6683
fmt
alanwang67 41804ab
added has_slot for est & refactored
alanwang67 4a7e2f5
added missing match arm
alanwang67 d2967de
added linking with slots in the condition for the EST data structure
alanwang67 a0436e7
first step
alanwang67 85e8dd5
adding a new field to Policy
alanwang67 0b23d86
removed repeated line
alanwang67 3f40da2
updates to naming
alanwang67 e35cac5
more naming changes
alanwang67 48ad73c
changes
alanwang67 8e725ec
use only one field for values of slots
alanwang67 5913651
updates to use restricted expr as type for slots for EST format
alanwang67 d83ee15
Revert "changes"
alanwang67 92f3c36
Revert "Revert "changes""
alanwang67 4b4ed2a
Revert "updates to use restricted expr as type for slots for EST format"
alanwang67 905db6c
Revert "Revert "updates to use restricted expr as type for slots for …
alanwang67 15c1804
Revert "Revert "Revert "changes"""
alanwang67 613d171
Revert "Revert "changes""
alanwang67 bb0b339
Revert "updates to use restricted expr as type for slots for EST format"
alanwang67 bd272da
Revert "use only one field for values of slots"
alanwang67 c86dd88
validate incorrect entity types in generalized slots + removed confus…
alanwang67 a28f3e8
provided better comments clarifying invariants
alanwang67 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
cedar-policy-core/src/ast/generalized_slots_annotation.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * Copyright Cedar Contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| use std::collections::BTreeMap; | ||
|
|
||
| use crate::ast::SlotId; | ||
| use crate::extensions::Extensions; | ||
| use crate::validator::{ | ||
| json_schema::Type as JSONSchemaType, types::Type as ValidatorType, RawName, SchemaError, | ||
| ValidatorSchema, | ||
| }; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| /// Struct storing the pairs of SlotId's and their corresponding type | ||
| #[derive(Clone, Eq, PartialEq, PartialOrd, Ord, Debug, Hash, Serialize, Deserialize)] | ||
| pub struct GeneralizedSlotsAnnotation(BTreeMap<SlotId, JSONSchemaType<RawName>>); | ||
|
|
||
| impl GeneralizedSlotsAnnotation { | ||
| /// Create a new empty `GeneralizedSlotsAnnotation` (with no slots) | ||
| pub fn new() -> Self { | ||
| Self(BTreeMap::new()) | ||
| } | ||
|
|
||
| /// Get the type of the slot by key | ||
| pub fn get(&self, key: &SlotId) -> Option<&JSONSchemaType<RawName>> { | ||
| self.0.get(key) | ||
| } | ||
|
|
||
| /// Iterate over all pairs of slots and their types | ||
| pub fn iter(&self) -> impl Iterator<Item = (&SlotId, &JSONSchemaType<RawName>)> { | ||
| self.0.iter() | ||
| } | ||
|
|
||
| /// Tell if it's empty | ||
| pub fn is_empty(&self) -> bool { | ||
| self.0.is_empty() | ||
| } | ||
|
|
||
| /// Converts the types of generalized slots annotation to | ||
| /// use validator types so that they can be used by the typechecker | ||
| pub fn into_validator_generalized_slots_annotation( | ||
| self, | ||
| schema: &ValidatorSchema, | ||
| ) -> Result<ValidatorGeneralizedSlotsAnnotation, SchemaError> { | ||
| let validator_generalized_slots_annotation: Result<BTreeMap<_, _>, SchemaError> = self | ||
| .0 | ||
| .into_iter() | ||
| .map(|(k, ty)| -> Result<_, SchemaError> { | ||
| Ok(( | ||
| k, | ||
| schema.json_schema_type_to_validator_type(ty, Extensions::all_available())?, | ||
| )) | ||
| }) | ||
| .collect(); | ||
| Ok(validator_generalized_slots_annotation?.into()) | ||
| } | ||
| } | ||
|
|
||
| impl Default for GeneralizedSlotsAnnotation { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl FromIterator<(SlotId, JSONSchemaType<RawName>)> for GeneralizedSlotsAnnotation { | ||
| fn from_iter<T: IntoIterator<Item = (SlotId, JSONSchemaType<RawName>)>>(iter: T) -> Self { | ||
| Self(BTreeMap::from_iter(iter)) | ||
| } | ||
| } | ||
|
|
||
| impl From<BTreeMap<SlotId, JSONSchemaType<RawName>>> for GeneralizedSlotsAnnotation { | ||
| fn from(value: BTreeMap<SlotId, JSONSchemaType<RawName>>) -> Self { | ||
| Self(value) | ||
| } | ||
| } | ||
|
|
||
| /// Struct storing the pairs of SlotId's and their corresponding validator types | ||
| #[derive(Clone, Eq, PartialEq, PartialOrd, Ord, Debug, Hash)] | ||
| pub struct ValidatorGeneralizedSlotsAnnotation(BTreeMap<SlotId, ValidatorType>); | ||
alanwang67 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| impl FromIterator<(SlotId, ValidatorType)> for ValidatorGeneralizedSlotsAnnotation { | ||
| fn from_iter<T: IntoIterator<Item = (SlotId, ValidatorType)>>(iter: T) -> Self { | ||
| Self(BTreeMap::from_iter(iter)) | ||
| } | ||
| } | ||
|
|
||
| impl From<BTreeMap<SlotId, ValidatorType>> for ValidatorGeneralizedSlotsAnnotation { | ||
| fn from(value: BTreeMap<SlotId, ValidatorType>) -> Self { | ||
| Self(value) | ||
| } | ||
| } | ||
|
|
||
| impl Default for ValidatorGeneralizedSlotsAnnotation { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl ValidatorGeneralizedSlotsAnnotation { | ||
| /// Create a new empty `ValidatorGeneralizedSlotsAnnotation` (with no slots) | ||
| pub fn new() -> Self { | ||
| Self(BTreeMap::new()) | ||
| } | ||
|
|
||
| /// Get the validator type of the slot by key | ||
| pub fn get(&self, slot: &SlotId) -> Option<&ValidatorType> { | ||
| self.0.get(slot) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.