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

For enum values starting with a digit, prefix the generated symbol with an underscore. #3961

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class RustReservedWordSymbolProvider(
return base.toSymbol(shape)
}
val previousName = base.toMemberName(shape)
val escapedName = this.toMemberName(shape)
// Prefix leading digit with an underscore to avoid invalid identifiers; allow extra leading underscores.
val escapedName = this.toMemberName(shape).replace(Regex("^(_*\\d)"), "_$1")
// if the names don't match and it isn't a simple escaping with `r#`, record a rename
renamedSymbol.toBuilder().name(escapedName)
.letIf(escapedName != previousName && !escapedName.contains("r#")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class EnumMemberModel(
parentShape: Shape,
definition: EnumDefinition,
): MaybeRenamed? {
val name = definition.name.orNull()?.toPascalCase() ?: return null
// Prefix leading digit with an underscore to avoid invalid identifiers; allow extra leading underscores.
val name = definition.name.orNull()?.toPascalCase()?.replace(Regex("^(_*\\d)"), "_$1") ?: return null
// Create a fake member shape for symbol look up until we refactor to use EnumShape
val fakeMemberShape =
MemberShape.builder().id(parentShape.id.withMember(name)).target("smithy.api#String").build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,7 @@ internal class RustReservedWordSymbolProviderTest {
expectEnumRename("SelfValue", MaybeRenamed("SelfValue_", "SelfValue"))
expectEnumRename("SelfOther", MaybeRenamed("SelfOther", null))
expectEnumRename("SELF", MaybeRenamed("SelfValue", "Self"))

expectEnumRename("_2DBarcode", MaybeRenamed("_2DBarcode", "2DBarcode"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class EnumGeneratorTest {
{ value: "some-value-2",
name: "someName2",
documentation: "More documentation #escape" },
{ value: "2D_BARCODE",
name: "_2D_BARCODE",
documentation: "Example with leading digit." },
{ value: "unknown",
name: "unknown",
documentation: "It has some docs that #need to be escaped" }
Expand Down Expand Up @@ -80,6 +83,14 @@ class EnumGeneratorTest {
}
}

@Test
fun `it prefixes enum names with underscore to avoid generating invalid identifiers starting with a digit`() {
model("_2D_BARCODE").also { member ->
member.derivedName() shouldBe "_2DBarcode"
member.name()!!.renamedFrom shouldBe "2DBarcode"
}
}

@Test
fun `it should render documentation`() {
val rendered = RustWriter.forModule("model").also { model("some_name_1").render(it) }.toString()
Expand Down