Skip to content
Merged
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 @@ -4,6 +4,7 @@
*/

import Foundation
public typealias URL = Foundation.URL
extension URL {
func toQueryItems() -> [URLQueryItem]? { return URLComponents(url: self,
resolvingAgainstBaseURL: false)?.queryItems }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ object ClientRuntimeTypes {
val Data = runtimeSymbol("Data")
val Document = runtimeSymbol("Document")
val URLQueryItem = runtimeSymbol("URLQueryItem")
val URL = runtimeSymbol("URL")
val ClientError = runtimeSymbol("ClientError")
val SdkError = runtimeSymbol("SdkError")
val ServiceError = runtimeSymbol("ServiceError")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ import software.amazon.smithy.swift.codegen.model.camelCaseName
import software.amazon.smithy.swift.codegen.model.capitalizedName
import software.amazon.smithy.swift.codegen.swiftFunctionParameterIndent

typealias HttpMethodCallback = () -> String
class MiddlewareExecutionGenerator(
private val ctx: ProtocolGenerator.GenerationContext,
private val writer: SwiftWriter,
private val httpBindingResolver: HttpBindingResolver,
private val httpProtocolCustomizable: HttpProtocolCustomizable,
private val operationMiddleware: OperationMiddleware,
private val operationStackName: String,
private val executionContext: MiddlewareRenderableExecutionContext
private val executionContext: MiddlewareRenderableExecutionContext,
private val httpMethodCallback: HttpMethodCallback? = null
) {
private val model: Model = ctx.model
private val symbolProvider = ctx.symbolProvider
Expand All @@ -40,8 +42,8 @@ class MiddlewareExecutionGenerator(
}

private fun renderContextAttributes(op: OperationShape) {
val httpTrait = httpBindingResolver.httpTrait(op)
val httpMethod = httpTrait.method.toLowerCase()
val httpMethod = resolveHttpMethod(op)

// FIXME it over indents if i add another indent, come up with better way to properly indent or format for swift
writer.write(" .withEncoder(value: encoder)")
writer.write(" .withDecoder(value: decoder)")
Expand All @@ -60,6 +62,15 @@ class MiddlewareExecutionGenerator(
httpProtocolCustomizable.renderContextAttributes(ctx, writer, serviceShape, op)
}

private fun resolveHttpMethod(op: OperationShape): String {
return httpMethodCallback?.let {
it()
} ?: run {
val httpTrait = httpBindingResolver.httpTrait(op)
httpTrait.method.toLowerCase()
}
}

private fun renderMiddlewares(op: OperationShape, operationStackName: String) {
operationMiddleware.renderMiddleware(ctx.model, ctx.symbolProvider, writer, op, operationStackName, MiddlewareStep.INITIALIZESTEP, executionContext)
operationMiddleware.renderMiddleware(ctx.model, ctx.symbolProvider, writer, op, operationStackName, MiddlewareStep.BUILDSTEP, executionContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface OperationMiddleware {
fun prependMiddleware(operation: OperationShape, renderableMiddleware: MiddlewareRenderable)
fun removeMiddleware(operation: OperationShape, step: MiddlewareStep, middlewareName: String)

fun clone(): OperationMiddleware

fun renderMiddleware(
model: Model,
symbolProvider: SymbolProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.swift.codegen.SwiftWriter

open class OperationMiddlewareGenerator : OperationMiddleware {
open class OperationMiddlewareGenerator(val mutableHashMap: MutableMap<OperationShape, MiddlewareStack> = mutableMapOf()) : OperationMiddleware {

private var middlewareMap: MutableMap<OperationShape, MiddlewareStack> = mutableMapOf()
private var middlewareMap: MutableMap<OperationShape, MiddlewareStack> = mutableHashMap

override fun appendMiddleware(operation: OperationShape, renderableMiddleware: MiddlewareRenderable) {
val step = renderableMiddleware.middlewareStep
Expand All @@ -28,6 +28,20 @@ open class OperationMiddlewareGenerator : OperationMiddleware {
}
}

override fun clone(): OperationMiddleware {
val copy: MutableMap<OperationShape, MiddlewareStack> = mutableMapOf()
middlewareMap.forEach { (shape, stack) ->
copy[shape] = MiddlewareStack(
stack.initializeMiddlewares.toMutableList(),
stack.serializeMiddlewares.toMutableList(),
stack.buildMiddlewares.toMutableList(),
stack.finalizeMiddlewares.toMutableList(),
stack.deserializeMiddlewares.toMutableList()
)
}
return OperationMiddlewareGenerator(copy)
}

override fun renderMiddleware(
model: Model,
symbolProvider: SymbolProvider,
Expand Down