-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add WaiterTypedError type, conform operation errors to it (#491)
- Loading branch information
Showing
8 changed files
with
197 additions
and
2 deletions.
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
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
16 changes: 16 additions & 0 deletions
16
Packages/ClientRuntime/Sources/Waiters/WaiterTypedError.swift
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,16 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// An error that may be identified by a string error type, for the purpose of matching the error to a Smithy `errorType` acceptor. | ||
/// This protocol will only be extended onto errors that have a Smithy waiter defined for them, and is only intended for use in the | ||
/// operation of a waiter. | ||
public protocol WaiterTypedError: Error { | ||
|
||
/// The Smithy identifier, without namespace, for the type of this error, or `nil` if the | ||
/// error has no known type. | ||
var waiterErrorType: String? { get } | ||
} |
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
74 changes: 74 additions & 0 deletions
74
...oftware/amazon/smithy/swift/codegen/integration/httpResponse/WaiterTypedErrorGenerator.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,74 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
package software.amazon.smithy.swift.codegen.integration.httpResponse | ||
|
||
import software.amazon.smithy.aws.traits.protocols.AwsQueryErrorTrait | ||
import software.amazon.smithy.codegen.core.Symbol | ||
import software.amazon.smithy.model.shapes.OperationShape | ||
import software.amazon.smithy.model.shapes.StructureShape | ||
import software.amazon.smithy.swift.codegen.SwiftDependency | ||
import software.amazon.smithy.swift.codegen.declareSection | ||
import software.amazon.smithy.swift.codegen.integration.ProtocolGenerator | ||
import software.amazon.smithy.swift.codegen.integration.SectionId | ||
import software.amazon.smithy.swift.codegen.integration.middlewares.handlers.MiddlewareShapeUtils | ||
import software.amazon.smithy.swift.codegen.model.getTrait | ||
|
||
class WaiterTypedErrorGenerator( | ||
val ctx: ProtocolGenerator.GenerationContext, | ||
val op: OperationShape, | ||
val unknownServiceErrorSymbol: Symbol | ||
) { | ||
object WaiterTypedErrorGeneratorSectionId : SectionId | ||
|
||
fun render() { | ||
val errorShapes = op.errors.map { ctx.model.expectShape(it) as StructureShape }.toSet().sorted() | ||
val operationErrorName = MiddlewareShapeUtils.outputErrorSymbolName(op) | ||
val rootNamespace = ctx.settings.moduleName | ||
val httpBindingSymbol = Symbol.builder() | ||
.definitionFile("./$rootNamespace/models/$operationErrorName+WaiterTypedError.swift") | ||
.name(operationErrorName) | ||
.build() | ||
|
||
ctx.delegator.useShapeWriter(httpBindingSymbol) { writer -> | ||
writer.addImport(SwiftDependency.CLIENT_RUNTIME.target) | ||
writer.addImport(unknownServiceErrorSymbol) | ||
val unknownServiceErrorType = unknownServiceErrorSymbol.name | ||
|
||
val context = mapOf( | ||
"ctx" to ctx, | ||
"unknownServiceErrorType" to unknownServiceErrorType, | ||
"operationErrorName" to operationErrorName, | ||
"errorShapes" to errorShapes | ||
) | ||
writer.declareSection(WaiterTypedErrorGeneratorSectionId, context) { | ||
writer.openBlock("extension \$L: WaiterTypedError {", "}", operationErrorName) { | ||
writer.write("") | ||
writer.write("/// The Smithy identifier, without namespace, for the type of this error, or `nil` if the") | ||
writer.write("/// error has no known type.") | ||
writer.openBlock("public var waiterErrorType: String? {", "}") { | ||
writer.write("switch self {") | ||
for (errorShape in errorShapes) { | ||
var errorShapeName = resolveErrorShapeName(errorShape) | ||
var errorShapeType = ctx.symbolProvider.toSymbol(errorShape).name | ||
var errorShapeEnumCase = errorShapeType.decapitalize() | ||
writer.write("case .\$L: return \$S", errorShapeEnumCase, errorShapeName) | ||
} | ||
writer.write("case .unknown(let error): return error.waiterErrorType") | ||
writer.write("}") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun resolveErrorShapeName(errorShape: StructureShape): String { | ||
errorShape.getTrait<AwsQueryErrorTrait>()?.let { | ||
return it.code | ||
} ?: run { | ||
return ctx.symbolProvider.toSymbol(errorShape).name | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
smithy-swift-codegen/src/test/kotlin/WaiterTypedErrorGeneratorTests.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,39 @@ | ||
import io.kotest.matchers.string.shouldContainOnlyOnce | ||
import org.junit.jupiter.api.Test | ||
import software.amazon.smithy.swift.codegen.ClientRuntimeTypes | ||
import software.amazon.smithy.swift.codegen.integration.httpResponse.WaiterTypedErrorGenerator | ||
|
||
class WaiterTypedErrorGeneratorTests { | ||
|
||
@Test | ||
fun `renders correct WaiterTypedError extension for operation error`() { | ||
val context = setupTests("waiter-typed-error.smithy", "com.test#WaiterTypedErrorTest") | ||
val contents = getFileContents(context.manifest, "/WaiterTypedErrorTest/models/GetWidgetOutputError+WaiterTypedError.swift") | ||
val expected = """ | ||
extension GetWidgetOutputError: WaiterTypedError { | ||
/// The Smithy identifier, without namespace, for the type of this error, or `nil` if the | ||
/// error has no known type. | ||
public var waiterErrorType: String? { | ||
switch self { | ||
case .invalidWidgetError: return "InvalidWidgetError" | ||
case .widgetNotFoundError: return "WidgetNotFoundError" | ||
case .unknown(let error): return error.waiterErrorType | ||
} | ||
} | ||
} | ||
""".trimIndent() | ||
contents.shouldContainOnlyOnce(expected) | ||
} | ||
|
||
private fun setupTests(smithyFile: String, serviceShapeId: String): TestContext { | ||
val context = TestContext.initContextFrom(smithyFile, serviceShapeId, MockHttpRestJsonProtocolGenerator()) { model -> | ||
model.defaultSettings(serviceShapeId, "WaiterTypedErrorTest", "2019-12-16", "WaiterTypedErrorTest") | ||
} | ||
context.generator.generateProtocolClient(context.generationCtx) | ||
val operationShape = context.generationCtx.model.operationShapes.first() | ||
WaiterTypedErrorGenerator(context.generationCtx, operationShape, ClientRuntimeTypes.Http.UnknownHttpServiceError).render() | ||
context.generationCtx.delegator.flushWriters() | ||
return context | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
smithy-swift-codegen/src/test/resources/waiter-typed-error.smithy
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,32 @@ | ||
$version: "1.0" | ||
|
||
namespace com.test | ||
|
||
use aws.api#service | ||
use aws.protocols#restJson1 | ||
|
||
@service(sdkId: "WaiterTypedErrorTest") | ||
service WaiterTypedErrorTest { | ||
version: "2019-12-16", | ||
operations: [GetWidget] | ||
} | ||
|
||
@http(uri: "/GetWidget", method: "GET") | ||
operation GetWidget { | ||
output: GetWidgetOutput | ||
errors: [WidgetNotFoundError, InvalidWidgetError] | ||
} | ||
|
||
structure GetWidgetOutput { | ||
name: String | ||
} | ||
|
||
@error("client") | ||
structure WidgetNotFoundError { | ||
name: String | ||
} | ||
|
||
@error("client") | ||
structure InvalidWidgetError { | ||
name: String | ||
} |