Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into jbe/marshal_unmarshal…
Browse files Browse the repository at this point in the history
…_to_smithy_swift
  • Loading branch information
jbelkins committed May 16, 2024
2 parents 2811202 + dbf0fb1 commit 7ecaa9f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@

package software.amazon.smithy.swift.codegen.test.utils

import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.traits.TimestampFormatTrait
import software.amazon.smithy.swift.codegen.SwiftWriter
import software.amazon.smithy.swift.codegen.integration.DefaultHTTPProtocolCustomizations
import software.amazon.smithy.swift.codegen.integration.DefaultServiceConfig
Expand All @@ -23,10 +20,7 @@ import software.amazon.smithy.swift.codegen.integration.HttpProtocolUnitTestRequ
import software.amazon.smithy.swift.codegen.integration.HttpProtocolUnitTestResponseGenerator
import software.amazon.smithy.swift.codegen.integration.ProtocolGenerator
import software.amazon.smithy.swift.codegen.integration.ServiceConfig
import software.amazon.smithy.swift.codegen.integration.serde.struct.StructDecodeGenerator
import software.amazon.smithy.swift.codegen.integration.serde.struct.StructEncodeGenerator
import software.amazon.smithy.swift.codegen.middleware.OperationMiddleware
import software.amazon.smithy.swift.codegen.model.ShapeMetadata

class TestCustomizations : DefaultHTTPProtocolCustomizations()
/**
Expand All @@ -38,29 +32,6 @@ class TestProtocolGenerator : HTTPBindingProtocolGenerator(TestCustomizations())
override val httpProtocolClientGeneratorFactory = HttpProtocolClientGeneratorFactory()
override val shouldRenderEncodableConformance = false

override fun renderStructEncode(
ctx: ProtocolGenerator.GenerationContext,
shapeContainingMembers: Shape,
shapeMetadata: Map<ShapeMetadata, Any>,
members: List<MemberShape>,
writer: SwiftWriter,
defaultTimestampFormat: TimestampFormatTrait.Format,
path: String?
) {
StructEncodeGenerator(ctx, shapeContainingMembers, members, shapeMetadata, writer).render()
}
override fun renderStructDecode(
ctx: ProtocolGenerator.GenerationContext,
shapeContainingMembers: Shape,
shapeMetadata: Map<ShapeMetadata, Any>,
members: List<MemberShape>,
writer: SwiftWriter,
defaultTimestampFormat: TimestampFormatTrait.Format,
path: String
) {
StructDecodeGenerator(ctx, shapeContainingMembers, members, shapeMetadata, writer).render()
}

override fun addProtocolSpecificMiddleware(ctx: ProtocolGenerator.GenerationContext, operation: OperationShape) {
// Intentionally empty
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import software.amazon.smithy.swift.codegen.integration.middlewares.SignerMiddle
import software.amazon.smithy.swift.codegen.integration.middlewares.providers.HttpHeaderProvider
import software.amazon.smithy.swift.codegen.integration.middlewares.providers.HttpQueryItemProvider
import software.amazon.smithy.swift.codegen.integration.middlewares.providers.HttpUrlPathProvider
import software.amazon.smithy.swift.codegen.integration.serde.struct.StructDecodeGenerator
import software.amazon.smithy.swift.codegen.integration.serde.struct.StructEncodeGenerator
import software.amazon.smithy.swift.codegen.integration.serde.union.UnionDecodeGenerator
import software.amazon.smithy.swift.codegen.integration.serde.union.UnionEncodeGenerator
import software.amazon.smithy.swift.codegen.middleware.OperationMiddlewareGenerator
Expand Down Expand Up @@ -184,8 +186,7 @@ abstract class HTTPBindingProtocolGenerator(
) {
writer.addImport(SwiftDependency.CLIENT_RUNTIME.target)
writer.write("")
val path = "properties.".takeIf { shape.hasTrait<ErrorTrait>() } ?: null
renderStructEncode(ctx, shape, shapeMetadata, httpBodyMembers, writer, customizations.defaultTimestampFormat, path)
renderStructEncode(ctx, shape, shapeMetadata, httpBodyMembers, writer)
}
}
}
Expand Down Expand Up @@ -229,27 +230,11 @@ abstract class HTTPBindingProtocolGenerator(
val path = "properties.".takeIf { shape.hasTrait<ErrorTrait>() } ?: ""
if (shape.hasTrait<NeedsWriterTrait>()) {
writer.write("")
renderStructEncode(
ctx,
shape,
mapOf(),
httpBodyMembers,
writer,
customizations.defaultTimestampFormat,
path
)
renderStructEncode(ctx, shape, mapOf(), httpBodyMembers, writer)
}
if (shape.hasTrait<NeedsReaderTrait>()) {
writer.write("")
renderStructDecode(
ctx,
shape,
mapOf(),
httpBodyMembers,
writer,
customizations.defaultTimestampFormat,
path
)
renderStructDecode(ctx, shape, mapOf(), httpBodyMembers, writer)
}
}
is UnionShape -> {
Expand Down Expand Up @@ -443,26 +428,31 @@ abstract class HTTPBindingProtocolGenerator(
override val operationMiddleware = OperationMiddlewareGenerator()

protected abstract val httpProtocolClientGeneratorFactory: HttpProtocolClientGeneratorFactory

protected val httpResponseGenerator = HTTPResponseGenerator(customizations)

protected abstract val shouldRenderEncodableConformance: Boolean
protected abstract fun renderStructEncode(

private fun renderStructEncode(
ctx: ProtocolGenerator.GenerationContext,
shapeContainingMembers: Shape,
shapeMetadata: Map<ShapeMetadata, Any>,
members: List<MemberShape>,
writer: SwiftWriter,
defaultTimestampFormat: TimestampFormatTrait.Format,
path: String? = null,
)
protected abstract fun renderStructDecode(
) {
StructEncodeGenerator(ctx, shapeContainingMembers, members, shapeMetadata, writer).render()
}

private fun renderStructDecode(
ctx: ProtocolGenerator.GenerationContext,
shapeContainingMembers: Shape,
shapeMetadata: Map<ShapeMetadata, Any>,
members: List<MemberShape>,
writer: SwiftWriter,
defaultTimestampFormat: TimestampFormatTrait.Format,
path: String,
)
) {
StructDecodeGenerator(ctx, shapeContainingMembers, members, shapeMetadata, writer).render()
}

protected abstract fun addProtocolSpecificMiddleware(ctx: ProtocolGenerator.GenerationContext, operation: OperationShape)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ package mocks
import TestHttpProtocolClientGeneratorFactory
import software.amazon.smithy.aws.traits.protocols.AwsJson1_1Trait
import software.amazon.smithy.model.pattern.UriPattern
import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.traits.HttpTrait
import software.amazon.smithy.model.traits.TimestampFormatTrait
import software.amazon.smithy.swift.codegen.SwiftWriter
import software.amazon.smithy.swift.codegen.integration.DefaultHTTPProtocolCustomizations
import software.amazon.smithy.swift.codegen.integration.HTTPBindingProtocolGenerator
Expand All @@ -23,9 +20,6 @@ import software.amazon.smithy.swift.codegen.integration.HttpProtocolUnitTestRequ
import software.amazon.smithy.swift.codegen.integration.HttpProtocolUnitTestResponseGenerator
import software.amazon.smithy.swift.codegen.integration.ProtocolGenerator
import software.amazon.smithy.swift.codegen.integration.protocols.core.StaticHttpBindingResolver
import software.amazon.smithy.swift.codegen.integration.serde.struct.StructDecodeGenerator
import software.amazon.smithy.swift.codegen.integration.serde.struct.StructEncodeGenerator
import software.amazon.smithy.swift.codegen.model.ShapeMetadata

class MockJsonHttpBindingResolver(
private val context: ProtocolGenerator.GenerationContext,
Expand Down Expand Up @@ -73,30 +67,6 @@ class MockHTTPAWSJson11ProtocolGenerator() : HTTPBindingProtocolGenerator(MockAW
override val httpProtocolClientGeneratorFactory = TestHttpProtocolClientGeneratorFactory()
override val shouldRenderEncodableConformance = false

override fun renderStructEncode(
ctx: ProtocolGenerator.GenerationContext,
shapeContainingMembers: Shape,
shapeMetadata: Map<ShapeMetadata, Any>,
members: List<MemberShape>,
writer: SwiftWriter,
defaultTimestampFormat: TimestampFormatTrait.Format,
path: String?
) {
val encodeGenerator = StructEncodeGenerator(ctx, shapeContainingMembers, members, shapeMetadata, writer)
encodeGenerator.render()
}
override fun renderStructDecode(
ctx: ProtocolGenerator.GenerationContext,
shapeContainingMembers: Shape,
shapeMetadata: Map<ShapeMetadata, Any>,
members: List<MemberShape>,
writer: SwiftWriter,
defaultTimestampFormat: TimestampFormatTrait.Format,
path: String
) {
StructDecodeGenerator(ctx, shapeContainingMembers, members, shapeMetadata, writer).render()
}

override fun addProtocolSpecificMiddleware(ctx: ProtocolGenerator.GenerationContext, operation: OperationShape) {
// Intentionally empty
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ package mocks
import TestHttpProtocolClientGeneratorFactory
import software.amazon.smithy.aws.traits.protocols.Ec2QueryTrait
import software.amazon.smithy.model.pattern.UriPattern
import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.traits.HttpTrait
import software.amazon.smithy.model.traits.TimestampFormatTrait
import software.amazon.smithy.swift.codegen.SwiftWriter
import software.amazon.smithy.swift.codegen.integration.DefaultHTTPProtocolCustomizations
import software.amazon.smithy.swift.codegen.integration.HTTPBindingProtocolGenerator
import software.amazon.smithy.swift.codegen.integration.HttpBindingResolver
Expand All @@ -24,9 +20,6 @@ import software.amazon.smithy.swift.codegen.integration.HttpProtocolUnitTestRequ
import software.amazon.smithy.swift.codegen.integration.HttpProtocolUnitTestResponseGenerator
import software.amazon.smithy.swift.codegen.integration.ProtocolGenerator
import software.amazon.smithy.swift.codegen.integration.protocols.core.StaticHttpBindingResolver
import software.amazon.smithy.swift.codegen.integration.serde.struct.StructDecodeGenerator
import software.amazon.smithy.swift.codegen.integration.serde.struct.StructEncodeGenerator
import software.amazon.smithy.swift.codegen.model.ShapeMetadata

class MockEC2QueryHTTPProtocolCustomizations() : DefaultHTTPProtocolCustomizations()

Expand All @@ -50,29 +43,6 @@ class MockHTTPEC2QueryProtocolGenerator : HTTPBindingProtocolGenerator(MockEC2Qu
override val protocol: ShapeId = Ec2QueryTrait.ID
override val httpProtocolClientGeneratorFactory = TestHttpProtocolClientGeneratorFactory()
override val shouldRenderEncodableConformance = true
override fun renderStructEncode(
ctx: ProtocolGenerator.GenerationContext,
shapeContainingMembers: Shape,
shapeMetadata: Map<ShapeMetadata, Any>,
members: List<MemberShape>,
writer: SwiftWriter,
defaultTimestampFormat: TimestampFormatTrait.Format,
path: String?
) {
StructEncodeGenerator(ctx, shapeContainingMembers, members, shapeMetadata, writer).render()
}
override fun renderStructDecode(
ctx: ProtocolGenerator.GenerationContext,
shapeContainingMembers: Shape,
shapeMetadata: Map<ShapeMetadata, Any>,
members: List<MemberShape>,
writer: SwiftWriter,
defaultTimestampFormat: TimestampFormatTrait.Format,
path: String
) {
val decodeGenerator = StructDecodeGenerator(ctx, shapeContainingMembers, members, mapOf(), writer)
decodeGenerator.render()
}

override fun addProtocolSpecificMiddleware(ctx: ProtocolGenerator.GenerationContext, operation: OperationShape) {
// Intentionally empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,15 @@
*/

import software.amazon.smithy.aws.traits.protocols.RestJson1Trait
import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.traits.TimestampFormatTrait
import software.amazon.smithy.swift.codegen.SwiftWriter
import software.amazon.smithy.swift.codegen.integration.DefaultHTTPProtocolCustomizations
import software.amazon.smithy.swift.codegen.integration.HTTPBindingProtocolGenerator
import software.amazon.smithy.swift.codegen.integration.HttpProtocolTestGenerator
import software.amazon.smithy.swift.codegen.integration.HttpProtocolUnitTestErrorGenerator
import software.amazon.smithy.swift.codegen.integration.HttpProtocolUnitTestRequestGenerator
import software.amazon.smithy.swift.codegen.integration.HttpProtocolUnitTestResponseGenerator
import software.amazon.smithy.swift.codegen.integration.ProtocolGenerator
import software.amazon.smithy.swift.codegen.integration.serde.struct.StructDecodeGenerator
import software.amazon.smithy.swift.codegen.integration.serde.struct.StructEncodeGenerator
import software.amazon.smithy.swift.codegen.model.ShapeMetadata

class MockRestJsonHTTPProtocolCustomizations() : DefaultHTTPProtocolCustomizations()
class MockHTTPRestJsonProtocolGenerator : HTTPBindingProtocolGenerator(MockRestJsonHTTPProtocolCustomizations()) {
Expand All @@ -28,29 +21,6 @@ class MockHTTPRestJsonProtocolGenerator : HTTPBindingProtocolGenerator(MockRestJ
override val httpProtocolClientGeneratorFactory = TestHttpProtocolClientGeneratorFactory()
override val shouldRenderEncodableConformance = false

override fun renderStructEncode(
ctx: ProtocolGenerator.GenerationContext,
shapeContainingMembers: Shape,
shapeMetadata: Map<ShapeMetadata, Any>,
members: List<MemberShape>,
writer: SwiftWriter,
defaultTimestampFormat: TimestampFormatTrait.Format,
path: String?
) {
StructEncodeGenerator(ctx, shapeContainingMembers, members, shapeMetadata, writer).render()
}
override fun renderStructDecode(
ctx: ProtocolGenerator.GenerationContext,
shapeContainingMembers: Shape,
shapeMetadata: Map<ShapeMetadata, Any>,
members: List<MemberShape>,
writer: SwiftWriter,
defaultTimestampFormat: TimestampFormatTrait.Format,
path: String
) {
StructDecodeGenerator(ctx, shapeContainingMembers, members, shapeMetadata, writer).render()
}

override fun addProtocolSpecificMiddleware(ctx: ProtocolGenerator.GenerationContext, operation: OperationShape) {
// Intentionally empty
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,15 @@
*/

import software.amazon.smithy.aws.traits.protocols.RestXmlTrait
import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.traits.TimestampFormatTrait
import software.amazon.smithy.swift.codegen.SwiftWriter
import software.amazon.smithy.swift.codegen.integration.DefaultHTTPProtocolCustomizations
import software.amazon.smithy.swift.codegen.integration.HTTPBindingProtocolGenerator
import software.amazon.smithy.swift.codegen.integration.HttpProtocolTestGenerator
import software.amazon.smithy.swift.codegen.integration.HttpProtocolUnitTestErrorGenerator
import software.amazon.smithy.swift.codegen.integration.HttpProtocolUnitTestRequestGenerator
import software.amazon.smithy.swift.codegen.integration.HttpProtocolUnitTestResponseGenerator
import software.amazon.smithy.swift.codegen.integration.ProtocolGenerator
import software.amazon.smithy.swift.codegen.integration.serde.struct.StructDecodeGenerator
import software.amazon.smithy.swift.codegen.integration.serde.struct.StructEncodeGenerator
import software.amazon.smithy.swift.codegen.model.ShapeMetadata

class MockRestXMLHTTPProtocolCustomizations() : DefaultHTTPProtocolCustomizations()

Expand All @@ -29,31 +22,6 @@ class MockHTTPRestXMLProtocolGenerator : HTTPBindingProtocolGenerator(MockRestXM
override val httpProtocolClientGeneratorFactory = TestHttpProtocolClientGeneratorFactory()
override val shouldRenderEncodableConformance = false

override fun renderStructEncode(
ctx: ProtocolGenerator.GenerationContext,
shapeContainingMembers: Shape,
shapeMetadata: Map<ShapeMetadata, Any>,
members: List<MemberShape>,
writer: SwiftWriter,
defaultTimestampFormat: TimestampFormatTrait.Format,
path: String?
) {
val encoder = StructEncodeGenerator(ctx, shapeContainingMembers, members, shapeMetadata, writer)
encoder.render()
}
override fun renderStructDecode(
ctx: ProtocolGenerator.GenerationContext,
shapeContainingMembers: Shape,
shapeMetadata: Map<ShapeMetadata, Any>,
members: List<MemberShape>,
writer: SwiftWriter,
defaultTimestampFormat: TimestampFormatTrait.Format,
path: String
) {
val decodeGenerator = StructDecodeGenerator(ctx, shapeContainingMembers, members, shapeMetadata, writer)
decodeGenerator.render()
}

override fun addProtocolSpecificMiddleware(ctx: ProtocolGenerator.GenerationContext, operation: OperationShape) {
// Intentionally empty
}
Expand Down

0 comments on commit 7ecaa9f

Please sign in to comment.