-
Notifications
You must be signed in to change notification settings - Fork 195
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
For enum values starting with a digit, prefix the generated symbol with an underscore. #3961
base: main
Are you sure you want to change the base?
Conversation
Hi @deven, thanks for the contribution! I have kicked off the CI tests and expect them all to pass. Would it be possible to get a test added for this to ensure that it isn't reverted at some later point and maybe a comment on the regex replacements just to indicate their purpose? |
It is unclear which error is this PR trying to resolve. Enum variant names follow the same rules as identifiers in Smithy, and Smithy does not allow identifiers to begin with a digit. For example, this is invalid in Smithy:
However, this restriction does not apply to enum values. For example, the following is perfectly valid and will not raise any errors:
|
Will do, thanks! |
I tried to keep it simple, but I guess I need to walk through the gory details. I've been working on creating AWS-style Rust SDKs for various sections of Amazon SP-API, using a custom script I wrote to translate the original Swagger IDL files into Smithy IDL files, then building the SDKs using In this particular case, I was working on the Fulfillment Inbound API. Here is one of the enums in the original
My script translated this Swagger enum into the following Smithy enum in the generated Smithy IDL file:
Of course, simply using
This exception is generated because "2DBarcode" is not a valid identifier because of the leading digit. However, the exact string "2DBarcode" was NOT in the Smithy IDL file at all! Some algorithm inside My commit uses the following code fragment to fix the invalid generated symbols starting with a digit:
This will prefix the symbol name with an underscore if it begins with a digit, making the generated symbol valid. To guarantee that this modification can't conflict with another enum variant, it also matches any number of leading underscores before that leading digit. Otherwise, prefixing Originally, I only added this code fragment to
When I investigated, I found that the generated Rust code had the following enum definition:
Since I needed to investigate the
This now contains a valid enum variant name ( |
Have you tried changing your original transformation to not use underscore as a prefix and something like |
@deven Thanks for the detailed explanation. @aajtodd I agree that not appending enum Dimensions {
_2D = "2D"
_3D = "3D"
} However, I would like to dig a bit deeper to understand why we need to use |
For the record, using |
e16a2af
to
c14bf4d
Compare
…th an underscore.
c14bf4d
to
fbd70dd
Compare
Hi @landonxjames, I've replaced the original commit with a newer one which includes comments and test cases. |
Motivation and Context
If an enum uses values which start with a digit, the generated symbols are invalid, causing both Smithy errors and Rust errors.
Description
Prefixes invalid symbols starting with a digit with underscores to make the symbols valid.
Testing
This commit fixes the Smithy error that I was getting for an enum using a value of "2DBarcode", and also fixes the generated Rust code to use "_2DBarcode" as the enum value instead of "2DBarcode".
Checklist
.changelog
directory, specifying "client," "server," or both in theapplies_to
key..changelog
directory, specifying "aws-sdk-rust" in theapplies_to
key.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.