Skip to content

Commit cb47865

Browse files
authored
fix!: consistent naming for types, protocols and everything else (#461)
1 parent 84801cb commit cb47865

File tree

8 files changed

+72
-15
lines changed

8 files changed

+72
-15
lines changed

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/PaginatorGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import software.amazon.smithy.swift.codegen.model.camelCaseName
2020
import software.amazon.smithy.swift.codegen.model.expectShape
2121
import software.amazon.smithy.swift.codegen.model.hasTrait
2222
import software.amazon.smithy.swift.codegen.model.isBoxed
23-
import software.amazon.smithy.swift.codegen.utils.toCamelCase
23+
import software.amazon.smithy.swift.codegen.utils.toLowerCamelCase
2424

2525
/**
2626
* Generate paginators for supporting operations. See
@@ -155,7 +155,7 @@ class PaginatorGenerator : SwiftIntegration {
155155
.call {
156156
val sortedMembers = inputShape.members().sortedBy { it.camelCaseName() }
157157
for ((index, member) in sortedMembers.withIndex()) {
158-
if (member.memberName.toCamelCase() != markerLiteral) {
158+
if (member.memberName.toLowerCamelCase() != markerLiteral) {
159159
writer.writeInline("\n\$L: \$L", member.camelCaseName(), "self.${member.camelCaseName()}")
160160
} else {
161161
writer.writeInline("\n\$L: \$L", member.camelCaseName(), "token")

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/StructureGenerator.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import software.amazon.smithy.swift.codegen.model.hasTrait
2626
import software.amazon.smithy.swift.codegen.model.isError
2727
import software.amazon.smithy.swift.codegen.model.nestedNamespaceType
2828
import software.amazon.smithy.swift.codegen.model.recursiveSymbol
29+
import software.amazon.smithy.swift.codegen.utils.toUpperCamelCase
2930

3031
class StructureGenerator(
3132
private val model: Model,
@@ -52,7 +53,7 @@ class StructureGenerator(
5253
}
5354

5455
fun render() {
55-
writer.putContext("struct.name", structSymbol.name)
56+
writer.putContext("struct.name", structSymbol.name.toUpperCamelCase())
5657
if (shape.isError) {
5758
renderErrorStructure()
5859
} else {

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SymbolVisitor.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import software.amazon.smithy.swift.codegen.model.defaultName
5151
import software.amazon.smithy.swift.codegen.model.hasTrait
5252
import software.amazon.smithy.swift.codegen.model.nestedNamespaceType
5353
import software.amazon.smithy.swift.codegen.utils.clientName
54+
import software.amazon.smithy.swift.codegen.utils.toLowerCamelCase
5455
import software.amazon.smithy.utils.StringUtils.lowerCase
5556
import java.util.logging.Logger
5657

@@ -94,7 +95,7 @@ class SymbolVisitor(private val model: Model, swiftSettings: SwiftSettings) :
9495
val name = escaper.escapeMemberName(shape.memberName)
9596
return if (!name.equals("sdkUnknown")) lowerCase(name) else name
9697
}
97-
return escaper.escapeMemberName(shape.memberName.decapitalize())
98+
return escaper.escapeMemberName(shape.memberName.toLowerCamelCase())
9899
}
99100

100101
override fun integerShape(shape: IntegerShape): Symbol = numberShape(shape, "Int", "0")

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/EndpointTraitConstructor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package software.amazon.smithy.swift.codegen.integration
77

88
import software.amazon.smithy.model.shapes.Shape
99
import software.amazon.smithy.model.traits.EndpointTrait
10-
import software.amazon.smithy.swift.codegen.utils.toCamelCase
10+
import software.amazon.smithy.swift.codegen.utils.toLowerCamelCase
1111

1212
class EndpointTraitConstructor(private val endpointTrait: EndpointTrait, private val inputShape: Shape) {
1313
fun construct(): String {
@@ -16,7 +16,7 @@ class EndpointTraitConstructor(private val endpointTrait: EndpointTrait, private
1616
// hostLabel can only target string shapes
1717
// see: https://awslabs.github.io/smithy/1.0/spec/core/endpoint-traits.html#hostlabel-trait
1818
val member = inputShape.members().first { it.memberName == segment.content }
19-
"\\(input.${member.memberName.toCamelCase()}!)"
19+
"\\(input.${member.memberName.toLowerCamelCase()}!)"
2020
} else {
2121
segment.content
2222
}

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/ServiceConfig.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package software.amazon.smithy.swift.codegen.integration
88
import software.amazon.smithy.codegen.core.Symbol
99
import software.amazon.smithy.swift.codegen.ClientRuntimeTypes
1010
import software.amazon.smithy.swift.codegen.SwiftWriter
11-
import software.amazon.smithy.swift.codegen.utils.toPascalCase
11+
import software.amazon.smithy.swift.codegen.utils.toUpperCamelCase
1212

1313
/**
1414
* Represents a config field on a client config struct.
@@ -20,7 +20,7 @@ data class ConfigField(val memberName: String?, val type: Symbol, val propFormat
2020
*/
2121
abstract class ServiceConfig(val writer: SwiftWriter, val serviceName: String) {
2222

23-
open val typeName: String = "${serviceName.toPascalCase()}Configuration"
23+
open val typeName: String = "${serviceName.toUpperCamelCase()}Configuration"
2424
open val typeProtocol: Symbol = Symbol.builder().name("${typeName}Protocol").build()
2525

2626
open val typesToConformConfigTo: List<Symbol> = mutableListOf(ClientRuntimeTypes.Core.SDKRuntimeConfiguration)

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/utils/CaseUtils.kt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,25 @@ fun String.splitOnWordBoundaries(): List<String> {
3232
return result.split(" ")
3333
}
3434

35-
fun String.toPascalCase(): String = splitOnWordBoundaries().joinToString(separator = "") { it.lowercase().replaceFirstChar { c -> c.uppercaseChar() } }
35+
// See https://awslabs.github.io/smithy/1.0/spec/aws/aws-core.html#using-sdk-service-id-for-client-naming
36+
fun String.clientName(): String = toUpperCamelCase()
3637

37-
fun String.toCamelCase(): String = toPascalCase().replaceFirstChar { c -> c.lowercaseChar() }
38+
fun String.toLowerCamelCase(): String {
39+
val words = this.splitOnWordBoundaries()
3840

39-
// See https://awslabs.github.io/smithy/1.0/spec/aws/aws-core.html#using-sdk-service-id-for-client-naming
40-
fun String.clientName(): String = toPascalCase()
41+
// make first part lowercase
42+
val firstWord = words.first().lowercase()
43+
44+
// join
45+
return firstWord + words.drop(1).joinToString(separator = "")
46+
}
47+
48+
fun String.toUpperCamelCase(): String {
49+
val words = this.splitOnWordBoundaries()
50+
51+
// make first part uppercase
52+
val firstWord = words.first().replaceFirstChar { c -> c.uppercaseChar() }
53+
54+
// join
55+
return firstWord + words.drop(1).joinToString(separator = "")
56+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import org.junit.jupiter.api.Assertions.assertEquals
2+
import org.junit.jupiter.api.Test
3+
import software.amazon.smithy.swift.codegen.utils.toLowerCamelCase
4+
import software.amazon.smithy.swift.codegen.utils.toUpperCamelCase
5+
6+
class CaseUtilsTests {
7+
@Test
8+
fun `it should convert to camel case when only first letter upper case`() {
9+
val input = "FooBar"
10+
val expected = "fooBar"
11+
val actual = input.toLowerCamelCase()
12+
assertEquals(expected, actual)
13+
}
14+
15+
@Test
16+
fun `it should convert to camel case when contiguous letters upper case`() {
17+
val input = "FOOBar"
18+
val expected = "fooBar"
19+
val actual = input.toLowerCamelCase()
20+
assertEquals(expected, actual)
21+
}
22+
23+
@Test
24+
fun `it should convert to upper case when only first is lower case`() {
25+
val input = "fooBar"
26+
val expected = "FooBar"
27+
val actual = input.toUpperCamelCase()
28+
assertEquals(expected, actual)
29+
}
30+
31+
@Test
32+
fun `it should convert to upper case when contiguous letters lower case`() {
33+
val input = "fOOBar"
34+
val expected = "FOOBar"
35+
val actual = input.toUpperCamelCase()
36+
assertEquals(expected, actual)
37+
}
38+
}

smithy-swift-codegen/src/test/kotlin/SymbolVisitorTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ class SymbolVisitorTest {
1313
// See https://awslabs.github.io/smithy/1.0/spec/aws/aws-core.html#using-sdk-service-id-for-client-naming
1414
@Test
1515
fun `it produces the correct string transformation for client names`() {
16-
assertEquals("ApiGateway", "API Gateway".clientName())
16+
assertEquals("APIGateway", "API Gateway".clientName())
1717
assertEquals("Lambda", "Lambda".clientName())
1818
assertEquals("ElastiCache", "ElastiCache".clientName())
1919
assertEquals("ApiGatewayManagementApi", "ApiGatewayManagementApi".clientName())
2020
assertEquals("MigrationHubConfig", "MigrationHub Config".clientName())
2121
assertEquals("IoTFleetHub", "IoTFleetHub".clientName())
22-
assertEquals("Iot1ClickProjects", "IoT 1Click Projects".clientName())
23-
assertEquals("DynamoDb", "DynamoDB".clientName())
22+
assertEquals("IoT1ClickProjects", "IoT 1Click Projects".clientName())
23+
assertEquals("DynamoDB", "DynamoDB".clientName())
2424
assertEquals("ExampleClient", "Example Client".clientName())
2525
assertEquals("ExampleClient", " Example Client ".clientName())
26+
assertEquals("EMRServerless", "EMR Serverless".clientName())
2627
}
2728
}

0 commit comments

Comments
 (0)