Skip to content

Commit

Permalink
changes needed for RFC 52 (#391)
Browse files Browse the repository at this point in the history
Signed-off-by: Shaobo He <[email protected]>
  • Loading branch information
shaobo-he-aws authored Jul 15, 2024
1 parent 2a6f89f commit 1f2928d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
6 changes: 3 additions & 3 deletions cedar-policy-generators/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::settings::ABACSettings;
use crate::size_hint_utils::{size_hint_for_choose, size_hint_for_range, size_hint_for_ratio};
use crate::{accum, gen, gen_inner, uniform};
use arbitrary::{Arbitrary, Unstructured};
use cedar_policy_core::ast::{self, Id};
use cedar_policy_core::ast::{self, UnreservedId};
use smol_str::SmolStr;
use std::collections::BTreeMap;

Expand Down Expand Up @@ -1052,8 +1052,8 @@ impl<'a> ExprGenerator<'a> {
})];
Ok(ast::Expr::call_extension_fn(constructor.name.clone(), args))
} else {
let type_name: Id = match target_type {
Type::IPAddr => "ipaddr".parse::<Id>().unwrap(),
let type_name: UnreservedId = match target_type {
Type::IPAddr => "ipaddr".parse::<UnreservedId>().unwrap(),
Type::Decimal => "decimal".parse().unwrap(),
_ => unreachable!("target type is deemed to be an extension type!"),
};
Expand Down
23 changes: 14 additions & 9 deletions cedar-policy-generators/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::settings::ABACSettings;
use crate::size_hint_utils::{size_hint_for_choose, size_hint_for_range, size_hint_for_ratio};
use crate::{accum, gen, gen_inner, uniform};
use arbitrary::{self, Arbitrary, Unstructured};
use cedar_policy_core::ast::{self, Effect, Id, PolicyID};
use cedar_policy_core::ast::{self, Effect, PolicyID, UnreservedId};
use cedar_policy_core::extensions::Extensions;
use cedar_policy_validator::{
ActionEntityUID, ActionType, ApplySpec, AttributesOrContext, EntityType, NamespaceDefinition,
Expand Down Expand Up @@ -380,7 +380,12 @@ fn attrs_in_schematype(
/// Build `attributes_by_type` from other components of `Schema`
fn build_attributes_by_type<'a>(
schema: &cedar_policy_validator::NamespaceDefinition<ast::Name>,
entity_types: impl IntoIterator<Item = (&'a Id, &'a cedar_policy_validator::EntityType<ast::Name>)>,
entity_types: impl IntoIterator<
Item = (
&'a UnreservedId,
&'a cedar_policy_validator::EntityType<ast::Name>,
),
>,
namespace: Option<&ast::Name>,
) -> HashMap<Type, Vec<(ast::EntityType, SmolStr)>> {
let triples = entity_types
Expand Down Expand Up @@ -412,7 +417,7 @@ struct Bindings {
// Bindings from `SchemaType` to a list of `Id`
// The `ids` field ensures that `Id`s are unique
// Note that `SchemaType`s should not contain any common type references
bindings: BTreeMap<SchemaType<ast::Name>, Vec<Id>>,
bindings: BTreeMap<SchemaType<ast::Name>, Vec<UnreservedId>>,
// The set of `Id`s used in the bindings
ids: HashSet<SmolStr>,
}
Expand All @@ -427,7 +432,7 @@ impl Bindings {
// Add a `SchemaType` and `Id` binding
// Note that this function always succeeds even if the `Id` already exists
// Under that situation, we create a new `Id` based on the existing `Id`
fn add_binding(&mut self, binding: (SchemaType<ast::Name>, Id)) {
fn add_binding(&mut self, binding: (SchemaType<ast::Name>, UnreservedId)) {
let (ty, id) = binding;
// create a new id when the provided id has been used
let new_id = if self.ids.contains(id.as_ref()) {
Expand Down Expand Up @@ -537,7 +542,7 @@ impl Bindings {
fn to_common_types(
&self,
u: &mut Unstructured<'_>,
) -> Result<HashMap<Id, SchemaType<ast::Name>>> {
) -> Result<HashMap<UnreservedId, SchemaType<ast::Name>>> {
let mut common_types = HashMap::new();
for (ty, ids) in &self.bindings {
if ids.len() == 1 {
Expand Down Expand Up @@ -603,7 +608,7 @@ impl Schema {
}

let common_types = bindings.to_common_types(u)?;
let entity_types: HashMap<Id, EntityType<ast::Name>> = HashMap::from_iter(
let entity_types: HashMap<UnreservedId, EntityType<ast::Name>> = HashMap::from_iter(
self.schema
.entity_types
.iter()
Expand Down Expand Up @@ -787,10 +792,10 @@ impl Schema {
// first generate the pool of names. we generate a set (so there are no
// duplicates), but then convert it to a Vec (because we want them
// ordered, even though we want the order to be arbitrary)
let entity_type_ids: HashSet<ast::Id> = u
let entity_type_ids: HashSet<ast::UnreservedId> = u
.arbitrary()
.map_err(|e| while_doing("generating entity type ids".into(), e))?;
let entity_type_ids: Vec<ast::Id> = if entity_type_ids.is_empty() {
let entity_type_ids: Vec<ast::UnreservedId> = if entity_type_ids.is_empty() {
// we want there to be at least one valid Name
vec!["a".parse().expect("should be a valid Name")]
} else {
Expand All @@ -810,7 +815,7 @@ impl Schema {

// now turn each of those names into an EntityType, no
// member-relationships yet
let mut entity_types: Vec<(Id, cedar_policy_validator::EntityType<ast::Name>)> =
let mut entity_types: Vec<(UnreservedId, cedar_policy_validator::EntityType<ast::Name>)> =
entity_type_ids
.iter()
.filter(|id| settings.enable_action_groups_and_attrs || id.to_string() != "Action")
Expand Down

0 comments on commit 1f2928d

Please sign in to comment.