Skip to content

Commit 0599776

Browse files
sichanyooSichan Yoo
andauthored
feat!: Conceal unknown error type (#740)
* Add helper functions to put internal SPI property to Symbols. Modify import logic to check for the property and call SPI import if applicable. * Make codegen write SPI imports by specific type for granularity & avoid duplicate imports. Add SwiftKind. * Add SPI import for MessageUnmarshallable as well. * Rename SwiftKind to SwiftDeclaration. * Allow varying spi name. * Swiftlint & ktlint --------- Co-authored-by: Sichan Yoo <[email protected]>
1 parent 500da59 commit 0599776

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

Sources/SmithyRetries/DefaultRetryStrategy/DefaultRetryStrategy+Error.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public extension DefaultRetryStrategy {
99

1010
/// Errors that may be thrown when an operation is retried unsuccessfully.
1111
enum Error: Swift.Error {
12-
12+
1313
/// The number and frequency of retries being attempted has exceeded the
1414
/// current limits.
1515
///

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import software.amazon.smithy.swift.codegen.model.defaultValue
2525
import software.amazon.smithy.swift.codegen.model.isBoxed
2626
import software.amazon.smithy.swift.codegen.model.isBuiltIn
2727
import software.amazon.smithy.swift.codegen.model.isGeneric
28+
import software.amazon.smithy.swift.codegen.model.isInternalSPI
2829
import software.amazon.smithy.swift.codegen.model.isOptional
2930
import software.amazon.smithy.swift.codegen.model.isServiceNestedNamespace
3031
import java.util.function.BiFunction
@@ -97,7 +98,16 @@ class SwiftWriter(private val fullPackageName: String, swiftImportContainer: Swi
9798

9899
fun addImport(symbol: Symbol) {
99100
if (symbol.isBuiltIn || symbol.isServiceNestedNamespace || symbol.namespace.isEmpty()) return
100-
addImport(symbol.namespace)
101+
if (symbol.isInternalSPI()) {
102+
val kind = symbol.getProperty("kind").orElseThrow()
103+
val spiName = symbol.getProperty("spiName").orElseThrow()
104+
addImport(
105+
"$kind ${symbol.namespace}.${symbol.name}",
106+
internalSPIName = "$spiName"
107+
)
108+
} else {
109+
addImport(symbol.namespace)
110+
}
101111
}
102112

103113
fun addImport(
@@ -111,8 +121,8 @@ class SwiftWriter(private val fullPackageName: String, swiftImportContainer: Swi
111121

112122
// Adds an import statement that imports the individual type from the specified module
113123
// Example: addIndividualTypeImport("struct", "Foundation", "Date") -> "import struct Foundation.Date"
114-
fun addIndividualTypeImport(kind: String, module: String, type: String) {
115-
importContainer.addImport("$kind $module.$type", false)
124+
fun addIndividualTypeImport(kind: SwiftDeclaration, module: String, type: String) {
125+
importContainer.addImport("${kind.kind} $module.$type", false)
116126
}
117127

118128
fun addImportReferences(symbol: Symbol, vararg options: SymbolReference.ContextOption) {
@@ -278,3 +288,14 @@ class SwiftWriter(private val fullPackageName: String, swiftImportContainer: Swi
278288
putFormatter('N', SwiftSymbolFormatter(shouldSetDefault = false, shouldRenderOptional = false))
279289
}
280290
}
291+
292+
enum class SwiftDeclaration(val kind: String) {
293+
STRUCT("struct"),
294+
CLASS("class"),
295+
ENUM("enum"),
296+
PROTOCOL("protocol"),
297+
TYPEALIAS("typealias"),
298+
FUNC("func"),
299+
LET("let"),
300+
VAR("var")
301+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class MessageUnmarshallableGenerator(
4040
ctx.delegator.useShapeWriter(streamMember) { writer ->
4141

4242
writer.addImport(SwiftDependency.CLIENT_RUNTIME.target)
43+
writer.addImport(customizations.unknownServiceErrorSymbol)
4344
writer.addImport(customizations.unknownServiceErrorSymbol.namespace)
4445
writer.openBlock("extension \$L {", "}", streamSymbol.fullName) {
4546
writer.openBlock(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class HTTPResponseBindingErrorGenerator(
7878

7979
ctx.delegator.useShapeWriter(httpBindingSymbol) { writer ->
8080
writer.addImport(SwiftDependency.CLIENT_RUNTIME.target)
81+
writer.addImport(unknownServiceErrorSymbol)
8182
writer.addImport(customizations.baseErrorSymbol.namespace)
8283
writer.addImports(ctx.service.responseWireProtocol)
8384
writer.openBlock("enum \$L {", "}", operationErrorName) {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package software.amazon.smithy.swift.codegen.model
88
import software.amazon.smithy.codegen.core.Symbol
99
import software.amazon.smithy.codegen.core.SymbolDependencyContainer
1010
import software.amazon.smithy.codegen.core.SymbolReference
11+
import software.amazon.smithy.swift.codegen.SwiftDeclaration
1112
import software.amazon.smithy.swift.codegen.SwiftDependency
1213

1314
@DslMarker
@@ -108,6 +109,18 @@ fun Symbol.isOptional(): Boolean {
108109
return this.getProperty("isOptional").orElse(false) as Boolean
109110
}
110111

112+
fun Symbol.isInternalSPI(): Boolean {
113+
return this.getProperty("isInternalSPI").orElse(false) as Boolean
114+
}
115+
116+
fun Symbol.toInternalSPI(kind: SwiftDeclaration, spiName: String): Symbol {
117+
return this.toBuilder()
118+
.putProperty("isInternalSPI", true)
119+
.putProperty("spiName", spiName)
120+
.putProperty("kind", kind.kind)
121+
.build()
122+
}
123+
111124
fun Symbol.toOptional(): Symbol {
112125
return this.toBuilder().putProperty("isOptional", true).name(name).build()
113126
}

0 commit comments

Comments
 (0)