Skip to content

Commit

Permalink
feat!: Conceal unknown error type (#740)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
sichanyoo and Sichan Yoo authored May 29, 2024
1 parent 500da59 commit 0599776
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public extension DefaultRetryStrategy {

/// Errors that may be thrown when an operation is retried unsuccessfully.
enum Error: Swift.Error {

/// The number and frequency of retries being attempted has exceeded the
/// current limits.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import software.amazon.smithy.swift.codegen.model.defaultValue
import software.amazon.smithy.swift.codegen.model.isBoxed
import software.amazon.smithy.swift.codegen.model.isBuiltIn
import software.amazon.smithy.swift.codegen.model.isGeneric
import software.amazon.smithy.swift.codegen.model.isInternalSPI
import software.amazon.smithy.swift.codegen.model.isOptional
import software.amazon.smithy.swift.codegen.model.isServiceNestedNamespace
import java.util.function.BiFunction
Expand Down Expand Up @@ -97,7 +98,16 @@ class SwiftWriter(private val fullPackageName: String, swiftImportContainer: Swi

fun addImport(symbol: Symbol) {
if (symbol.isBuiltIn || symbol.isServiceNestedNamespace || symbol.namespace.isEmpty()) return
addImport(symbol.namespace)
if (symbol.isInternalSPI()) {
val kind = symbol.getProperty("kind").orElseThrow()
val spiName = symbol.getProperty("spiName").orElseThrow()
addImport(
"$kind ${symbol.namespace}.${symbol.name}",
internalSPIName = "$spiName"
)
} else {
addImport(symbol.namespace)
}
}

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

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

fun addImportReferences(symbol: Symbol, vararg options: SymbolReference.ContextOption) {
Expand Down Expand Up @@ -278,3 +288,14 @@ class SwiftWriter(private val fullPackageName: String, swiftImportContainer: Swi
putFormatter('N', SwiftSymbolFormatter(shouldSetDefault = false, shouldRenderOptional = false))
}
}

enum class SwiftDeclaration(val kind: String) {
STRUCT("struct"),
CLASS("class"),
ENUM("enum"),
PROTOCOL("protocol"),
TYPEALIAS("typealias"),
FUNC("func"),
LET("let"),
VAR("var")
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MessageUnmarshallableGenerator(
ctx.delegator.useShapeWriter(streamMember) { writer ->

writer.addImport(SwiftDependency.CLIENT_RUNTIME.target)
writer.addImport(customizations.unknownServiceErrorSymbol)
writer.addImport(customizations.unknownServiceErrorSymbol.namespace)
writer.openBlock("extension \$L {", "}", streamSymbol.fullName) {
writer.openBlock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class HTTPResponseBindingErrorGenerator(

ctx.delegator.useShapeWriter(httpBindingSymbol) { writer ->
writer.addImport(SwiftDependency.CLIENT_RUNTIME.target)
writer.addImport(unknownServiceErrorSymbol)
writer.addImport(customizations.baseErrorSymbol.namespace)
writer.addImports(ctx.service.responseWireProtocol)
writer.openBlock("enum \$L {", "}", operationErrorName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package software.amazon.smithy.swift.codegen.model
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.codegen.core.SymbolDependencyContainer
import software.amazon.smithy.codegen.core.SymbolReference
import software.amazon.smithy.swift.codegen.SwiftDeclaration
import software.amazon.smithy.swift.codegen.SwiftDependency

@DslMarker
Expand Down Expand Up @@ -108,6 +109,18 @@ fun Symbol.isOptional(): Boolean {
return this.getProperty("isOptional").orElse(false) as Boolean
}

fun Symbol.isInternalSPI(): Boolean {
return this.getProperty("isInternalSPI").orElse(false) as Boolean
}

fun Symbol.toInternalSPI(kind: SwiftDeclaration, spiName: String): Symbol {
return this.toBuilder()
.putProperty("isInternalSPI", true)
.putProperty("spiName", spiName)
.putProperty("kind", kind.kind)
.build()
}

fun Symbol.toOptional(): Symbol {
return this.toBuilder().putProperty("isOptional", true).name(name).build()
}
Expand Down

0 comments on commit 0599776

Please sign in to comment.