Skip to content

Adds data models and builder functions for remaining constraints #238

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

Merged
merged 4 commits into from
Mar 18, 2025

Conversation

popematt
Copy link
Contributor

@popematt popematt commented Mar 17, 2025

Issue #, if available:

Description of changes:

Adds all the remaining constraints, except for the variants of annotations (which still needs a little bit of work). Much of this PR is copy/paste from other constraints with some renaming done. It's probably fine to skim over them—just double check that there are no copy/paste mistakes where things should have been renamed but weren't.

  • AnyOf and OneOf are copied from AllOf
  • Not is copied from TypeConstraint
  • Scale, Precision, Exponent, ContainerLength, CodepointLength, and ByteLength are copied from Utf8ByteLength

These constraints are entirely new:

  • Regex
  • TimestampOffset
  • Contains
  • Element and FieldNames
  • Ieee754Float

I also fixed the visibility of the fields in the utf8_byte_length and type constraints.
Finally, convert_to_pattern was made pub(crate) so that it could be reused in the Regex constraint.

There are some doc comments, but I plan to fill that out in a later PR to avoid blowing up the cope of this one.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@popematt popematt requested review from zslayton and desaikd March 17, 2025 18:16
@popematt
Copy link
Contributor Author

When I started self-reviewing it to write the PR tour comments, I hit upon a solution to one of the things that was bothering me. Don't bother reviewing this yet.

}

impl<V: IslVersion> TypeDefinitionBuilder<V> {
pub fn element(self, type_argument: VersionedTypeArgument<V>) -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🗺️ I chose to make this method available for both ISL versions, so that customers can switch from ISL 1.0 to 2.0 without having to refactor any existing code that does programmatic construction. There's also element_distinct with accepts a bool flag indicating whether the elements should be distinct or not.

}

impl TypeDefinitionBuilder<ISL_2_0> {
pub fn field_names(self, type_argument: VersionedTypeArgument<ISL_2_0>) -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🗺️ Like element, field_names has two builder methods, but in this case, both are only available for ISL 2.0.

Comment on lines +31 to +32
// Sorting the vec allows us to get Bag-like equality semantics.
// TODO: Replace the vec with a HashBag or come up with a less hacky way to sort the type arguments.
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is used in a few places, consider making a naive Bag type wrapping a sorted Vec<TypeArgument> that you can optimize later.

Comment on lines +105 to +122
vec![AnyOf::new(vec![
TypeArgument::from_type_ref(
NullabilityModifier::Nothing,
TypeReference::from("foo_type")
),
TypeArgument::from_type_def(
NullabilityModifier::Nothing,
TypeDefinition::new(vec![], vec![])
),
TypeArgument::from_type_ref(
NullabilityModifier::Nothing,
TypeReference::imported("bar.isl", "baz")
),
TypeArgument::from_type_ref(
NullabilityModifier::Nullable,
TypeReference::from("quux_type")
),
],)
Copy link
Contributor

Choose a reason for hiding this comment

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

These explicitly-constructed vec!s would be good candidates for the tuple-of-Into<_>s trick. (Not in this PR.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It could be, but I'm intentionally not using it here so that if there's something wrong with the tuple-of-Into<_>s logic, it won't silently pass these tests. In other words, I'm intentionally constructing these the verbose way in order to make the tests more thorough.

Comment on lines +92 to +99
use FloatingPointNumberFormat::*;
match text {
Some("binary16") => Ok(Binary16),
Some("binary32") => Ok(Binary32),
Some("binary64") => Ok(Binary64),
_ => invalid_schema_error(format!("Not a valid IEEE754 format symbol: {value}")),
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I was curious to see if the compiler would do something clever with this, like lifting out the comparison of the string prefix. To my surprise, it did not.

image

@popematt popematt merged commit 51edafa into amazon-ion:dev-1.0.0 Mar 18, 2025
3 checks passed
@popematt popematt mentioned this pull request Mar 18, 2025
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.

2 participants