Skip to content
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

Migrate to EnumShape #1932

Closed
wants to merge 6 commits into from
Closed

Migrate to EnumShape #1932

wants to merge 6 commits into from

Conversation

82marbag
Copy link
Contributor

@82marbag 82marbag commented Nov 1, 2022

Deprecate EnumTrait and migrate to EnumShape.

Closes #1671

Checklist

  • I have updated CHANGELOG.next.toml if I made changes to the smithy-rs codegen or runtime crates
  • I have updated CHANGELOG.next.toml if I made changes to the AWS SDK, generated SDK code, or SDK runtime crates

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@github-actions
Copy link

github-actions bot commented Nov 1, 2022

A new generated diff is ready to view.

A new doc preview is ready to view.

@github-actions
Copy link

github-actions bot commented Nov 1, 2022

A new generated diff is ready to view.

A new doc preview is ready to view.

Copy link
Collaborator

@jdisanti jdisanti left a comment

Choose a reason for hiding this comment

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

This looks good! Just a couple minor comments.

writer.rustBlock("impl <T> #T<T> for $enumName where T: #T<str>", RuntimeType.From, RuntimeType.AsRef) {
rustBlock("fn from(s: T) -> Self") {
rust("$enumName(s.as_ref().to_owned())")
// TODO(https://github.com/awslabs/smithy-rs/issues/1700): handle IntEnum
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does it generate invalid code or throw an exception when encountering an integer enum? If not, we should make it do so for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed so that it throws an exception both here and when the conversion fails

@jdisanti
Copy link
Collaborator

jdisanti commented Nov 2, 2022

Projection s3 failed: software.amazon.smithy.codegen.core.CodegenException: Unnamed @enum shapes are unsupported.

Heh. We may need to upgrade the SDK models for this to pass. The current S3 model is IDLv1.

Unfortunately, we can't upgrade the models until our endpoints 2.0 implementation lands though.

@82marbag
Copy link
Contributor Author

82marbag commented Nov 3, 2022

Projection s3 failed: software.amazon.smithy.codegen.core.CodegenException: Unnamed @enum shapes are unsupported.

Heh. We may need to upgrade the SDK models for this to pass. The current S3 model is IDLv1.

Unfortunately, we can't upgrade the models until our endpoints 2.0 implementation lands though.

It fails on EC2 and S3. For EC2 v2:

[ERROR] com.amazonaws.ec2#AmazonEC2: Unable to resolve trait smithy.rules#endpointTests. If this is a custom trait, then it must be defined before it can be used in a model.

For S3 v2:

[ERROR] com.amazonaws.s3#DeleteBucketWebsiteRequest$Bucket: Unable to resolve trait smithy.rules#contextParam. If this is a custom trait, then it must be defined before it can be used in a model.

@enums work. We can either remove this check until v2 or I'm ok waiting for the model upgrades (there might be some merges / refactoring to be done by then)

@82marbag
Copy link
Contributor Author

82marbag commented Nov 8, 2022

This PR is waiting for the migration to v2 models; there are missing features on the client before this migration can be done. When test models are in v2, this PR can be merged. (agreed with @jdisanti )

@jdisanti
Copy link
Collaborator

jdisanti commented Nov 9, 2022

I can update the models once #1972 is merged.

@jdisanti
Copy link
Collaborator

The models are updated. This should be unblocked now.

Signed-off-by: Daniele Ahmed <[email protected]>
Signed-off-by: Daniele Ahmed <[email protected]>
Signed-off-by: Daniele Ahmed <[email protected]>
Signed-off-by: Daniele Ahmed <[email protected]>
Copy link
Contributor

@david-perez david-perez left a comment

Choose a reason for hiding this comment

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

Looks good! Just a few comments.

Can you also rename renderBuildEnforcingRequiredAndEnumTraitsFn from ServerBuilderGeneratorWithoutPublicConstrainedTypes.kt, to, perhaps, renderBuildEnforcingRequiredTraitAndEnumValuesFn?

EnumGenerator(model, symbolProvider, this, shape, enum).render()
}
if (shape.hasTrait<EnumTrait>()) {
throw CodegenException("Unnamed @enum shapes are unsupported: $shape")
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this error message accurate? My understanding from looking at this is that the model transformer might fail to convert an unnamed enum if e.g. one of its values cannot be safely synthesized into a valid name. So it's not like we don't support unnamed @enum shapes entirely, just the ones that we failed to convert when synthesizeEnumNames is set, right?. Aside: is that flag enabled by default?

If the above is correct, I'd fail here with a more informative error message, perhaps instructing the user to look in the log output above for the INFO messages that the transformer from awslabs/smithy printed indicating the exact reason why the enum could not be converted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, I've changed to a better log message, thanks!

@@ -62,4 +63,42 @@ class CodegenVisitorTest {
val baselineModel = visitor.baselineTransform(model)
baselineModel.getShapesWithTrait(ShapeId.from("smithy.api#mixin")).isEmpty() shouldBe true
}

@Test
fun `baseline transform verify string enum converted to EnumShape`() {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I'd add a negative test too that asserts we throw an exception when an unnamed enum could not be converted to an enum shape.

Signed-off-by: Daniele Ahmed <[email protected]>
@82marbag 82marbag requested a review from a team as a code owner December 1, 2022 14:49
is StringShape -> if (shape.hasTrait<EnumTrait>()) {
enumMeta(shape)
} else null
is EnumShape -> enumMeta(shape)
Copy link
Contributor

Choose a reason for hiding this comment

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

We can now make enumMeta accept EnumShape instead of StringShape.

@jmklix
Copy link
Collaborator

jmklix commented Apr 12, 2024

we've decided to begin closing old PRs that we don't plan to work on in the near future. Please let us know if you still think this PR is needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client server Rust server SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check if updating usage of EnumTrait to EnumShape improves code generated by smithy-rs
6 participants