Skip to content

Commit

Permalink
Add support for programmatically adding closed fields (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
desaikd authored Sep 10, 2024
1 parent 7d31173 commit be856c2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 3 additions & 2 deletions ion-schema/src/isl/isl_constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,14 @@ pub mod v_2_0 {
}

/// Creates a `fields` constraint using the field names and [IslVariablyOccurringTypeRef]s referenced inside it
pub fn fields<I>(fields: I) -> IslConstraint
/// and specify if the `fields` are closed or not (i.e. indicates whether only fields that are explicitly specified should be allowed or not).
pub fn fields<I>(fields: I, is_closed: bool) -> IslConstraint
where
I: Iterator<Item = (String, IslVariablyOccurringTypeRef)>,
{
IslConstraint::new(
IslVersion::V2_0,
IslConstraintValue::Fields(fields.collect(), false),
IslConstraintValue::Fields(fields.collect(), is_closed),
)
}

Expand Down
14 changes: 10 additions & 4 deletions ion-schema/src/isl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,18 @@ mod isl_tests {
"#),
anonymous_type([ordered_elements([variably_occurring_type_ref(named_type_ref("symbol"), UsizeRange::new_single_value(1)), variably_occurring_type_ref(anonymous_type_ref([type_constraint(named_type_ref("int"))]), UsizeRange::new_single_value(1))])])
),
case::fields_constraint(
load_isl_type(r#" // For a schema with fields constraint as below:
{ fields: { name: string, id: int} }
case::closed_fields_constraint(
load_isl_type_v2_0(r#" // For a schema with fields constraint as below:
{ fields: closed::{ name: string, id: int} }
"#),
anonymous_type([fields(vec![("name".to_owned(), variably_occurring_type_ref(named_type_ref("string"), UsizeRange::zero_or_one())), ("id".to_owned(), variably_occurring_type_ref(named_type_ref("int"), UsizeRange::zero_or_one()))].into_iter())]),
anonymous_type([isl_constraint::v_2_0::fields(vec![("name".to_owned(), variably_occurring_type_ref(named_type_ref("string"), UsizeRange::zero_or_one())), ("id".to_owned(), variably_occurring_type_ref(named_type_ref("int"), UsizeRange::zero_or_one()))].into_iter(), true)]),
),
case::fields_constraint(
load_isl_type(r#" // For a schema with fields constraint as below:
{ fields: { name: string, id: int} }
"#),
anonymous_type([fields(vec![("name".to_owned(), variably_occurring_type_ref(named_type_ref("string"), UsizeRange::zero_or_one())), ("id".to_owned(), variably_occurring_type_ref(named_type_ref("int"), UsizeRange::zero_or_one()))].into_iter())]),
),
case::field_names_constraint(
load_isl_type_v2_0(r#" // For a schema with field_names constraint as below:
{ field_names: distinct::symbol }
Expand Down

0 comments on commit be856c2

Please sign in to comment.