-
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
Make RustReservedWordsSymbolProvider
configurable
#2382
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d3c95a2
Remove `toEnumVariantName` from `RustSymbolProvider`
jdisanti c1add65
Fix escaping of `Self` in symbol providers
jdisanti 3337f93
Clean up an old hack
jdisanti 4f59076
Make `RustReservedWordsSymbolProvider` configurable
jdisanti bedd1f8
Merge origin/main
jdisanti 4097d45
Update changelog
jdisanti a88b847
Incorporate feedback
jdisanti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
.../src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/ClientReservedWords.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rust.codegen.client.smithy | ||
|
||
import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWordConfig | ||
import software.amazon.smithy.rust.codegen.core.smithy.generators.StructureGenerator | ||
import software.amazon.smithy.rust.codegen.core.smithy.generators.UnionGenerator | ||
|
||
val ClientReservedWords = RustReservedWordConfig( | ||
structureMemberMap = StructureGenerator.structureMemberNameMap + | ||
mapOf( | ||
"send" to "send_value", | ||
// To avoid conflicts with the `make_operation` and `presigned` functions on generated inputs | ||
"make_operation" to "make_operation_value", | ||
"presigned" to "presigned_value", | ||
"customize" to "customize_value", | ||
// To avoid conflicts with the error metadata `meta` field | ||
"meta" to "meta_value", | ||
), | ||
unionMemberMap = mapOf( | ||
// Unions contain an `Unknown` variant. This exists to support parsing data returned from the server | ||
// that represent union variants that have been added since this SDK was generated. | ||
UnionGenerator.UnknownVariantName to "${UnionGenerator.UnknownVariantName}Value", | ||
"${UnionGenerator.UnknownVariantName}Value" to "${UnionGenerator.UnknownVariantName}Value_", | ||
), | ||
enumMemberMap = mapOf( | ||
// Unknown is used as the name of the variant containing unexpected values | ||
"Unknown" to "UnknownValue", | ||
// Real models won't end in `_` so it's safe to stop here | ||
"UnknownValue" to "UnknownValue_", | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,23 @@ import software.amazon.smithy.model.shapes.UnionShape | |
import software.amazon.smithy.model.traits.EnumTrait | ||
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider | ||
import software.amazon.smithy.rust.codegen.core.smithy.WrappingSymbolProvider | ||
import software.amazon.smithy.rust.codegen.core.smithy.generators.UnionGenerator | ||
import software.amazon.smithy.rust.codegen.core.smithy.renamedFrom | ||
import software.amazon.smithy.rust.codegen.core.util.hasTrait | ||
import software.amazon.smithy.rust.codegen.core.util.letIf | ||
|
||
class RustReservedWordSymbolProvider(private val base: RustSymbolProvider) : WrappingSymbolProvider(base) { | ||
data class RustReservedWordConfig( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
/** Map of struct member names that should get renamed */ | ||
val structureMemberMap: Map<String, String>, | ||
/** Map of union member names that should get renamed */ | ||
val unionMemberMap: Map<String, String>, | ||
/** Map of enum member names that should get renamed */ | ||
val enumMemberMap: Map<String, String>, | ||
) | ||
|
||
class RustReservedWordSymbolProvider( | ||
private val base: RustSymbolProvider, | ||
private val reservedWordConfig: RustReservedWordConfig, | ||
) : WrappingSymbolProvider(base) { | ||
private val internal = | ||
ReservedWordSymbolProvider.builder().symbolProvider(base) | ||
.nameReservedWords(RustReservedWords) | ||
|
@@ -33,35 +44,14 @@ class RustReservedWordSymbolProvider(private val base: RustSymbolProvider) : Wra | |
val reservedWordReplacedName = internal.toMemberName(shape) | ||
val container = model.expectShape(shape.container) | ||
return when { | ||
container is StructureShape -> when (baseName) { | ||
"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" | ||
else -> reservedWordReplacedName | ||
} | ||
container is StructureShape -> | ||
reservedWordConfig.structureMemberMap.getOrDefault(baseName, reservedWordReplacedName) | ||
|
||
container is UnionShape -> when (baseName) { | ||
// Unions contain an `Unknown` variant. This exists to support parsing data returned from the server | ||
// that represent union variants that have been added since this SDK was generated. | ||
UnionGenerator.UnknownVariantName -> "${UnionGenerator.UnknownVariantName}Value" | ||
"${UnionGenerator.UnknownVariantName}Value" -> "${UnionGenerator.UnknownVariantName}Value_" | ||
else -> reservedWordReplacedName | ||
} | ||
container is UnionShape -> | ||
reservedWordConfig.unionMemberMap.getOrDefault(baseName, reservedWordReplacedName) | ||
|
||
container is EnumShape || container.hasTrait<EnumTrait>() -> when (baseName) { | ||
// Unknown is used as the name of the variant containing unexpected values | ||
"Unknown" -> "UnknownValue" | ||
// Real models won't end in `_` so it's safe to stop here | ||
"UnknownValue" -> "UnknownValue_" | ||
else -> reservedWordReplacedName | ||
} | ||
container is EnumShape || container.hasTrait<EnumTrait>() -> | ||
reservedWordConfig.enumMemberMap.getOrDefault(baseName, reservedWordReplacedName) | ||
|
||
else -> error("unexpected container: $container") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,18 +22,108 @@ import software.amazon.smithy.rust.codegen.core.util.lookup | |
internal class RustReservedWordSymbolProviderTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great tests. |
||
private class TestSymbolProvider(model: Model) : | ||
WrappingSymbolProvider(SymbolVisitor(testRustSettings(), model, null, TestRustSymbolProviderConfig)) | ||
private val emptyConfig = RustReservedWordConfig(emptyMap(), emptyMap(), emptyMap()) | ||
|
||
@Test | ||
fun `structs are escaped`() { | ||
val model = """ | ||
namespace test | ||
structure Self {} | ||
""".asSmithyModel() | ||
val provider = RustReservedWordSymbolProvider(TestSymbolProvider(model)) | ||
val provider = RustReservedWordSymbolProvider(TestSymbolProvider(model), emptyConfig) | ||
val symbol = provider.toSymbol(model.lookup("test#Self")) | ||
symbol.name shouldBe "SelfValue" | ||
} | ||
|
||
private fun mappingTest(config: RustReservedWordConfig, model: Model, id: String, test: (String) -> Unit) { | ||
val provider = RustReservedWordSymbolProvider(TestSymbolProvider(model), config) | ||
val symbol = provider.toMemberName(model.lookup("test#Container\$$id")) | ||
test(symbol) | ||
} | ||
|
||
@Test | ||
fun `structs member names are mapped via config`() { | ||
val config = emptyConfig.copy( | ||
structureMemberMap = mapOf( | ||
"name_to_map" to "mapped_name", | ||
"NameToMap" to "MappedName", | ||
), | ||
) | ||
var model = """ | ||
namespace test | ||
structure Container { | ||
name_to_map: String | ||
} | ||
""".asSmithyModel() | ||
mappingTest(config, model, "name_to_map") { memberName -> | ||
memberName shouldBe "mapped_name" | ||
} | ||
|
||
model = """ | ||
namespace test | ||
enum Container { | ||
NameToMap = "NameToMap" | ||
} | ||
""".asSmithyModel(smithyVersion = "2.0") | ||
mappingTest(config, model, "NameToMap") { memberName -> | ||
// Container was not a struct, so the field keeps its old name | ||
memberName shouldBe "NameToMap" | ||
} | ||
|
||
model = """ | ||
namespace test | ||
union Container { | ||
NameToMap: String | ||
} | ||
""".asSmithyModel() | ||
mappingTest(config, model, "NameToMap") { memberName -> | ||
// Container was not a struct, so the field keeps its old name | ||
memberName shouldBe "NameToMap" | ||
} | ||
} | ||
|
||
@Test | ||
fun `union member names are mapped via config`() { | ||
val config = emptyConfig.copy( | ||
unionMemberMap = mapOf( | ||
"name_to_map" to "mapped_name", | ||
"NameToMap" to "MappedName", | ||
), | ||
) | ||
|
||
var model = """ | ||
namespace test | ||
union Container { | ||
NameToMap: String | ||
} | ||
""".asSmithyModel() | ||
mappingTest(config, model, "NameToMap") { memberName -> | ||
memberName shouldBe "MappedName" | ||
} | ||
|
||
model = """ | ||
namespace test | ||
structure Container { | ||
name_to_map: String | ||
} | ||
""".asSmithyModel() | ||
mappingTest(config, model, "name_to_map") { memberName -> | ||
// Container was not a union, so the field keeps its old name | ||
memberName shouldBe "name_to_map" | ||
} | ||
|
||
model = """ | ||
namespace test | ||
enum Container { | ||
NameToMap = "NameToMap" | ||
} | ||
""".asSmithyModel(smithyVersion = "2.0") | ||
mappingTest(config, model, "NameToMap") { memberName -> | ||
// Container was not a union, so the field keeps its old name | ||
memberName shouldBe "NameToMap" | ||
} | ||
} | ||
|
||
@Test | ||
fun `member names are escaped`() { | ||
val model = """ | ||
|
@@ -42,7 +132,7 @@ internal class RustReservedWordSymbolProviderTest { | |
async: String | ||
} | ||
""".asSmithyModel() | ||
val provider = RustReservedWordSymbolProvider(TestSymbolProvider(model)) | ||
val provider = RustReservedWordSymbolProvider(TestSymbolProvider(model), emptyConfig) | ||
provider.toMemberName( | ||
MemberShape.builder().id("namespace#container\$async").target("namespace#Integer").build(), | ||
) shouldBe "r##async" | ||
|
@@ -58,7 +148,15 @@ internal class RustReservedWordSymbolProviderTest { | |
namespace foo | ||
@enum([{ name: "dontcare", value: "dontcare" }]) string Container | ||
""".asSmithyModel() | ||
val provider = RustReservedWordSymbolProvider(TestSymbolProvider(model)) | ||
val provider = RustReservedWordSymbolProvider( | ||
TestSymbolProvider(model), | ||
reservedWordConfig = emptyConfig.copy( | ||
enumMemberMap = mapOf( | ||
"Unknown" to "UnknownValue", | ||
"UnknownValue" to "UnknownValue_", | ||
), | ||
), | ||
) | ||
|
||
fun expectEnumRename(original: String, expected: MaybeRenamed) { | ||
val symbol = provider.toSymbol( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.