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

Make RustReservedWordsSymbolProvider configurable #2382

Merged
merged 7 commits into from
Mar 13, 2023

Conversation

jdisanti
Copy link
Collaborator

@jdisanti jdisanti commented Feb 15, 2023

Motivation and Context

Fixes #1766.

Checklist

  • I have updated CHANGELOG.next.toml if I made changes to the smithy-rs codegen or 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.

The `toEnumVariantName` function existed on symbol provider to work
around enum definitions not being shapes. In the future when we refactor
to use `EnumShape` instead of `EnumTrait`, there will be `MemberShape`s
for each enum member. This change incrementally moves us to that future
by creating fake `MemberShape`s in the enum generator from the enum
definition.
@jdisanti jdisanti changed the base branch from main to jdisanti-fix-escape-self February 15, 2023 23:13
@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@jdisanti jdisanti force-pushed the jdisanti-reserved-word-configurability branch from ee7aade to 4860d89 Compare February 16, 2023 00:35
@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@jdisanti jdisanti force-pushed the jdisanti-reserved-word-configurability branch from 4860d89 to 4f59076 Compare February 16, 2023 01:36
@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@jdisanti jdisanti marked this pull request as ready for review February 16, 2023 02:12
@jdisanti jdisanti requested review from a team as code owners February 16, 2023 02:12
@jdisanti jdisanti requested review from drganjoo, weihanglo and PC-LoadLetter and removed request for a team February 16, 2023 02:12
@jdisanti jdisanti added needs-review A tag for PRs waiting on a review from one of the repo admins needs-sdk-review needs-server-review and removed needs-review A tag for PRs waiting on a review from one of the repo admins labels Feb 16, 2023
),
enumMemberMap = mapOf(
// Unknown is used as the name of the variant containing unexpected values
"Unknown" to "UnknownValue",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

From a comment on #2377, these should have constants.

Base automatically changed from jdisanti-fix-escape-self to main February 23, 2023 00:02
Copy link
Contributor

@hlbarber hlbarber left a comment

Choose a reason for hiding this comment

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

Is the change from send to send_value shown in the Server Test intended?

It looks like we've lost this mapping

                "build" -> "build_value"
                "builder" -> "builder_value"
                "default" -> "default_value"
                "send" -> "send_value"
                // To avoid conflicts with the `make_operation` and `presigned` functions on generated inputs
                "make_operation" -> "make_operation_value"
                "presigned" -> "presigned_value"
                "customize" -> "customize_value"
                // To avoid conflicts with the error metadata `meta` field
                "meta" -> "meta_value"

on the server side, and it's been restored for the client side only?

@jdisanti
Copy link
Collaborator Author

Is the change from send to send_value shown in the Server Test intended?

Does send need to be reserved on the server side? My thinking on this was that send is reserved on the client side due to us adding a send() function to fluent builders to actually send a request, but that seems unnecessary on the server side.

Good catch though, I need to add a breaking-change label to this PR.

@jdisanti jdisanti added the breaking-change This will require a breaking change label Mar 10, 2023
@jdisanti
Copy link
Collaborator Author

jdisanti commented Mar 10, 2023

These are preserved in ServerReservedWords:

"build" -> "build_value"
"builder" -> "builder_value"
"default" -> "default_value"

These seem unnecessary for server, but you tell me:

"send" -> "send_value"
"make_operation" -> "make_operation_value"
"presigned" -> "presigned_value"
"customize" -> "customize_value"
"meta" -> "meta_value"

@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

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.

The list of server reserved words looks good.


class RustReservedWordSymbolProvider(private val base: RustSymbolProvider, private val model: Model) :
WrappingSymbolProvider(base) {
data class RustReservedWordConfig(
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: RustReservedWordsConfig to match the rest of the uses?

import software.amazon.smithy.rust.codegen.core.smithy.generators.UnionGenerator

val ClientReservedWords = RustReservedWordConfig(
structMemberMap = StructureGenerator.structMemberMap +
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 consciously make the effort to use "structure" to refer to Smithy structure shapes, and use struct only to refer to Rust structs.

Comment on lines 47 to 50
container is StructureShape -> when (val mapped = reservedWordConfig.structMemberMap[baseName]) {
null -> reservedWordReplacedName
else -> mapped
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
container is StructureShape -> when (val mapped = reservedWordConfig.structMemberMap[baseName]) {
null -> reservedWordReplacedName
else -> mapped
}
container is StructureShape ->
reservedWordConfig.structMemberMap.getOrDefault(baseName, reservedWordReplacedName)

These can be slightly simplified.

// Real models won't end in `_` so it's safe to stop here
"UnknownValue" -> "UnknownValue_"
else -> reservedWordReplacedName
container is EnumShape || container.hasTrait<EnumTrait>() -> when (val mapped = reservedWordConfig.enumMemberMap[baseName]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

EnumTrait is only applicable to string shapes; a member shape's container cannot be a string shape.

Suggested change
container is EnumShape || container.hasTrait<EnumTrait>() -> when (val mapped = reservedWordConfig.enumMemberMap[baseName]) {
container is EnumShape -> when (val mapped = reservedWordConfig.enumMemberMap[baseName]) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmmm... Yeah, I think this is dead code. toMemberName isn't even called for enum rendering. I think I'll keep it around in the event that someone refactors EnumGenerator to use toMemberName though since this is technically correct.

@@ -22,18 +22,108 @@ import software.amazon.smithy.rust.codegen.core.util.lookup
internal class RustReservedWordSymbolProviderTest {
Copy link
Contributor

Choose a reason for hiding this comment

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

Great tests.

@jdisanti jdisanti enabled auto-merge March 13, 2023 18:53
@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@jdisanti jdisanti added this pull request to the merge queue Mar 13, 2023
Merged via the queue into main with commit 4835af9 Mar 13, 2023
@jdisanti jdisanti deleted the jdisanti-reserved-word-configurability branch March 13, 2023 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change This will require a breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Split RustReservedWords into composed classes
5 participants