Skip to content

Commit be856c2

Browse files
authored
Add support for programmatically adding closed fields (#218)
1 parent 7d31173 commit be856c2

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

ion-schema/src/isl/isl_constraint.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,14 @@ pub mod v_2_0 {
272272
}
273273

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

ion-schema/src/isl/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,18 @@ mod isl_tests {
501501
"#),
502502
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))])])
503503
),
504-
case::fields_constraint(
505-
load_isl_type(r#" // For a schema with fields constraint as below:
506-
{ fields: { name: string, id: int} }
504+
case::closed_fields_constraint(
505+
load_isl_type_v2_0(r#" // For a schema with fields constraint as below:
506+
{ fields: closed::{ name: string, id: int} }
507507
"#),
508-
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())]),
508+
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)]),
509509
),
510+
case::fields_constraint(
511+
load_isl_type(r#" // For a schema with fields constraint as below:
512+
{ fields: { name: string, id: int} }
513+
"#),
514+
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())]),
515+
),
510516
case::field_names_constraint(
511517
load_isl_type_v2_0(r#" // For a schema with field_names constraint as below:
512518
{ field_names: distinct::symbol }

0 commit comments

Comments
 (0)