Skip to content

Commit

Permalink
feat!: Conceal serde types with Swift SPI (#1699)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbelkins authored Sep 3, 2024
1 parent 9369785 commit d4bf68b
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import enum Smithy.ByteStream
import class Smithy.Context
import ClientRuntime
import SmithyHTTPAPI
import SmithyXML
@_spi(SmithyReadWrite) import SmithyXML
import struct Foundation.Data
import SmithyStreams

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
import protocol ClientRuntime.BaseError
import enum ClientRuntime.BaseErrorDecodeError
import class SmithyHTTPAPI.HTTPResponse
import class SmithyJSON.Reader
@_spi(SmithyReadWrite) import class SmithyJSON.Reader

public struct AWSJSONError: BaseError {
public let code: String
public let message: String?
public let requestID: String?
public var errorBodyReader: Reader { responseReader }
@_spi(SmithyReadWrite) public var errorBodyReader: Reader { responseReader }

public let httpResponse: HTTPResponse
private let responseReader: Reader

@_spi(SmithyReadWrite)
public init(httpResponse: HTTPResponse, responseReader: Reader, noErrorWrapping: Bool) throws {
let code: String? = try httpResponse.headers.value(for: "X-Amzn-Errortype")
?? responseReader["code"].readIfPresent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
import protocol ClientRuntime.BaseError
import enum ClientRuntime.BaseErrorDecodeError
import class SmithyHTTPAPI.HTTPResponse
import class SmithyXML.Reader
@_spi(SmithyReadWrite) import class SmithyXML.Reader

public struct AWSQueryError: BaseError {
public let code: String
public let message: String?
public let requestID: String?
public let httpResponse: HTTPResponse
public let responseReader: Reader
public let errorBodyReader: Reader
@_spi(SmithyReadWrite) public let responseReader: Reader
@_spi(SmithyReadWrite) public let errorBodyReader: Reader

@_spi(SmithyReadWrite)
public init(httpResponse: HTTPResponse, responseReader: Reader, noErrorWrapping: Bool) throws {
self.errorBodyReader = noErrorWrapping ? responseReader : responseReader["Error"]
let code: String? = try errorBodyReader["Code"].readIfPresent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
import protocol ClientRuntime.BaseError
import enum ClientRuntime.BaseErrorDecodeError
import class SmithyHTTPAPI.HTTPResponse
import class SmithyXML.Reader
@_spi(SmithyReadWrite) import class SmithyXML.Reader

public struct EC2QueryError: BaseError {
public let code: String
public let message: String?
public let requestID: String?
public let errorBodyReader: Reader
@_spi(SmithyReadWrite) public let errorBodyReader: Reader

public let httpResponse: HTTPResponse
public let responseReader: Reader
@_spi(SmithyReadWrite) public let responseReader: Reader

@_spi(SmithyReadWrite)
public init(httpResponse: HTTPResponse, responseReader: Reader, noErrorWrapping: Bool) throws {
self.httpResponse = httpResponse
self.responseReader = responseReader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import protocol ClientRuntime.BaseError
import enum ClientRuntime.BaseErrorDecodeError
import class SmithyHTTPAPI.HTTPResponse
import class SmithyJSON.Reader
@_spi(SmithyReadWrite) import class SmithyJSON.Reader

public struct RestJSONError: BaseError {
public let code: String
Expand All @@ -17,7 +17,7 @@ public struct RestJSONError: BaseError {

public let httpResponse: HTTPResponse
private let responseReader: Reader
public var errorBodyReader: Reader { responseReader }
@_spi(SmithyReadWrite) public var errorBodyReader: Reader { responseReader }

// header identifying the error code
let X_AMZN_ERROR_TYPE_HEADER_NAME = "X-Amzn-Errortype"
Expand All @@ -31,6 +31,7 @@ public struct RestJSONError: BaseError {
// error message header returned by event stream errors
let X_AMZN_EVENT_ERROR_MESSAGE_HEADER_NAME = ":error-message"

@_spi(SmithyReadWrite)
public init(httpResponse: HTTPResponse, responseReader: SmithyJSON.Reader, noErrorWrapping: Bool) throws {
let type = try httpResponse.headers.value(for: X_AMZN_ERROR_TYPE_HEADER_NAME)
?? responseReader["code"].readIfPresent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import protocol ClientRuntime.BaseError
import enum ClientRuntime.BaseErrorDecodeError
import class SmithyHTTPAPI.HTTPResponse
import class SmithyXML.Reader
@_spi(SmithyReadWrite) import class SmithyXML.Reader

public struct RestXMLError: BaseError {
public let code: String
Expand All @@ -18,8 +18,9 @@ public struct RestXMLError: BaseError {

public let httpResponse: HTTPResponse
private let responseReader: Reader
public let errorBodyReader: Reader
@_spi(SmithyReadWrite) public let errorBodyReader: Reader

@_spi(SmithyReadWrite)
public init(httpResponse: HTTPResponse, responseReader: Reader, noErrorWrapping: Bool) throws {
self.errorBodyReader = Self.errorBodyReader(responseReader: responseReader, noErrorWrapping: noErrorWrapping)
let code: String? = try errorBodyReader["Code"].readIfPresent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SmithyChecksumsAPI
import SmithyHTTPAPI
import XCTest
import AwsCommonRuntimeKit
import SmithyTestUtil
@_spi(SmithyReadWrite) import SmithyTestUtil
@testable import ClientRuntime
import class SmithyStreams.BufferedStream
import class SmithyChecksums.ChunkedStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import SmithyHTTPAPI
import ClientRuntime
import SmithyTestUtil
import XCTest
import SmithyXML
@testable import AWSClientRuntime
@_spi(SmithyReadWrite) import SmithyXML
@_spi(SmithyReadWrite) @testable import AWSClientRuntime

class Ec2ErrorRequestIdTests: XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

import Smithy
import SmithyHTTPAPI
import SmithyReadWrite
import SmithyJSON
@_spi(SmithyReadWrite) import SmithyReadWrite
@_spi(SmithyReadWrite) import SmithyJSON
import ClientRuntime
import SmithyTestUtil
import XCTest
@testable import AWSClientRuntime
@_spi(SmithyReadWrite) @testable import AWSClientRuntime
@_spi(UnknownAWSHTTPServiceError) import struct AWSClientRuntime.UnknownAWSHTTPServiceError

class RestJSONErrorTests: HttpResponseTestBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import SmithyHTTPAPI
import SmithyIdentity
import SmithyTestUtil
import AwsCommonRuntimeKit
import SmithyTimestamps
@_spi(SmithyTimestamps) import SmithyTimestamps
import SmithyEventStreams
import SmithyEventStreamsAPI
import AWSSDKIdentity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,20 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "StringArrayEndpointParam"
BuildableName = "StringArrayEndpointParam"
BlueprintName = "StringArrayEndpointParam"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down Expand Up @@ -409,6 +423,16 @@
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "StringArrayEndpointParamTests"
BuildableName = "StringArrayEndpointParamTests"
BlueprintName = "StringArrayEndpointParamTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import software.amazon.smithy.swift.codegen.swiftmodules.SwiftSymbol
object AWSClientRuntimeTypes {

object AWSQuery {
val AWSQueryError = runtimeSymbol("AWSQueryError", SwiftDeclaration.STRUCT)
val AWSQueryError = runtimeSymbol("AWSQueryError", SwiftDeclaration.STRUCT, listOf("SmithyReadWrite"))
}
object EC2Query {
val EC2QueryError = runtimeSymbol("EC2QueryError", SwiftDeclaration.STRUCT)
val EC2QueryError = runtimeSymbol("EC2QueryError", SwiftDeclaration.STRUCT, listOf("SmithyReadWrite"))
}
object AWSJSON {
val AWSJSONError = runtimeSymbol("AWSJSONError", SwiftDeclaration.STRUCT)
val AWSJSONError = runtimeSymbol("AWSJSONError", SwiftDeclaration.STRUCT, listOf("SmithyReadWrite"))
val XAmzTargetMiddleware = runtimeSymbol("XAmzTargetMiddleware", SwiftDeclaration.STRUCT)
}
object RestJSON {
val RestJSONError = runtimeSymbol("RestJSONError", SwiftDeclaration.STRUCT)
val RestJSONError = runtimeSymbol("RestJSONError", SwiftDeclaration.STRUCT, listOf("SmithyReadWrite"))
}

object RestXML {
val RestXMLError = runtimeSymbol("RestXMLError", SwiftDeclaration.STRUCT)
val RestXMLError = runtimeSymbol("RestXMLError", SwiftDeclaration.STRUCT, listOf("SmithyReadWrite"))
object S3 {
val AWSS3ServiceError = runtimeSymbol("AWSS3ServiceError", SwiftDeclaration.PROTOCOL)
val AWSS3ServiceError = runtimeSymbol("AWSS3ServiceError", SwiftDeclaration.PROTOCOL, listOf("SmithyReadWrite"))
val AWSS3ErrorWith200StatusXMLMiddleware = runtimeSymbol("AWSS3ErrorWith200StatusXMLMiddleware", SwiftDeclaration.STRUCT)
}
}
Expand All @@ -33,7 +33,7 @@ object AWSClientRuntimeTypes {
val AWSUserAgentMetadata = runtimeSymbol("AWSUserAgentMetadata", SwiftDeclaration.STRUCT)
val UserAgentMiddleware = runtimeSymbol("UserAgentMiddleware", SwiftDeclaration.STRUCT)
val EndpointResolverMiddleware = runtimeSymbol("EndpointResolverMiddleware", SwiftDeclaration.STRUCT)
val UnknownAWSHTTPServiceError = runtimeSymbol("UnknownAWSHTTPServiceError", SwiftDeclaration.STRUCT, "UnknownAWSHTTPServiceError")
val UnknownAWSHTTPServiceError = runtimeSymbol("UnknownAWSHTTPServiceError", SwiftDeclaration.STRUCT, listOf("UnknownAWSHTTPServiceError"))
val AWSServiceError = runtimeSymbol("AWSServiceError", SwiftDeclaration.PROTOCOL)
val Sha256TreeHashMiddleware = runtimeSymbol("Sha256TreeHashMiddleware", SwiftDeclaration.STRUCT)
val AWSRetryErrorInfoProvider = runtimeSymbol("AWSRetryErrorInfoProvider", SwiftDeclaration.ENUM)
Expand All @@ -53,9 +53,14 @@ object AWSClientRuntimeTypes {
}
}

private fun runtimeSymbol(name: String, declaration: SwiftDeclaration? = null, spiName: String? = null): Symbol = SwiftSymbol.make(
private fun runtimeSymbol(
name: String,
declaration: SwiftDeclaration,
spiNames: List<String> = emptyList(),
): Symbol = SwiftSymbol.make(
name,
declaration,
AWSSwiftDependency.AWS_CLIENT_RUNTIME,
spiName,
emptyList(),
spiNames,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ private fun runtimeSymbol(name: String, declaration: SwiftDeclaration? = null):
name,
declaration,
AWSSwiftDependency.AWS_SDK_EVENT_STREAMS_AUTH,
null,
emptyList(),
emptyList(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ private fun runtimeSymbol(name: String, declaration: SwiftDeclaration? = null):
name,
declaration,
AWSSwiftDependency.AWS_SDK_HTTP_AUTH,
null,
emptyList(),
emptyList(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ private fun runtimeSymbol(name: String, declaration: SwiftDeclaration? = null):
name,
declaration,
AWSSwiftDependency.AWS_SDK_IDENTITY,
null,
emptyList(),
emptyList(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ extension QueryListsInput {
try writer["Hi"].writeList(value.flattenedListArgWithXmlName, memberWritingClosure: SmithyReadWrite.WritingClosures.writeString(value:to:), memberNodeInfo: "item", isFlattened: true)
try writer["ListArg"].writeList(value.listArg, memberWritingClosure: SmithyReadWrite.WritingClosures.writeString(value:to:), memberNodeInfo: "member", isFlattened: false)
try writer["ListArgWithXmlNameMember"].writeList(value.listArgWithXmlNameMember, memberWritingClosure: SmithyReadWrite.WritingClosures.writeString(value:to:), memberNodeInfo: "item", isFlattened: false)
try writer["flatTsList"].writeList(value.flatTsList, memberWritingClosure: SmithyReadWrite.timestampWritingClosure(format: .epochSeconds), memberNodeInfo: "member", isFlattened: true)
try writer["tsList"].writeList(value.tsList, memberWritingClosure: SmithyReadWrite.timestampWritingClosure(format: .epochSeconds), memberNodeInfo: "member", isFlattened: false)
try writer["flatTsList"].writeList(value.flatTsList, memberWritingClosure: SmithyReadWrite.timestampWritingClosure(format: SmithyTimestamps.TimestampFormat.epochSeconds), memberNodeInfo: "member", isFlattened: true)
try writer["tsList"].writeList(value.tsList, memberWritingClosure: SmithyReadWrite.timestampWritingClosure(format: SmithyTimestamps.TimestampFormat.epochSeconds), memberNodeInfo: "member", isFlattened: false)
try writer["Action"].write("QueryLists")
try writer["Version"].write("2020-01-08")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ extension QueryTimestampsInput {
static func write(value: QueryTimestampsInput?, to writer: SmithyFormURL.Writer) throws {
guard let value else { return }
try writer["epochMember"].writeTimestamp(value.epochMember, format: .epochSeconds)
try writer["epochTarget"].writeTimestamp(value.epochTarget, format: .epochSeconds)
try writer["normalFormat"].writeTimestamp(value.normalFormat, format: .dateTime)
try writer["epochMember"].writeTimestamp(value.epochMember, format: SmithyTimestamps.TimestampFormat.epochSeconds)
try writer["epochTarget"].writeTimestamp(value.epochTarget, format: SmithyTimestamps.TimestampFormat.epochSeconds)
try writer["normalFormat"].writeTimestamp(value.normalFormat, format: SmithyTimestamps.TimestampFormat.dateTime)
try writer["Action"].write("QueryTimestamps")
try writer["Version"].write("2020-01-08")
}
Expand Down
34 changes: 17 additions & 17 deletions scripts/protogen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ rm -rf codegen/protocol-test-codegen-local/build
./gradlew -p codegen/protocol-test-codegen-local build

# Delete the generated Package.swift for protocol test packages so they may be seen in Xcode
rm codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/aws-restjson/swift-codegen/Package.swift
rm codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/aws-restjson-validation/swift-codegen/Package.swift
rm codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/aws-json-10/swift-codegen/Package.swift
rm codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/aws-json-11/swift-codegen/Package.swift
rm codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/rest-xml/swift-codegen/Package.swift
rm codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/rest-xml-xmlns/swift-codegen/Package.swift
rm codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/ec2-query/swift-codegen/Package.swift
rm codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/aws-query/swift-codegen/Package.swift
rm codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/apigateway/swift-codegen/Package.swift
rm codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/glacier/swift-codegen/Package.swift
rm codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/machinelearning/swift-codegen/Package.swift
rm codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/s3/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/aws-restjson/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/aws-restjson-validation/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/aws-json-10/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/aws-json-11/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/rest-xml/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/rest-xml-xmlns/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/ec2-query/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/aws-query/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/apigateway/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/glacier/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/machinelearning/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen/build/smithyprojections/protocol-test-codegen/s3/swift-codegen/Package.swift

# Now do the same for local protocol tests
rm codegen/protocol-test-codegen-local/build/smithyprojections/protocol-test-codegen-local/rest_json_extras/swift-codegen/Package.swift
rm codegen/protocol-test-codegen-local/build/smithyprojections/protocol-test-codegen-local/AwsQueryExtras/swift-codegen/Package.swift
rm codegen/protocol-test-codegen-local/build/smithyprojections/protocol-test-codegen-local/EventStream/swift-codegen/Package.swift
rm codegen/protocol-test-codegen-local/build/smithyprojections/protocol-test-codegen-local/RPCEventStream/swift-codegen/Package.swift
rm codegen/protocol-test-codegen-local/build/smithyprojections/protocol-test-codegen-local/Waiters/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen-local/build/smithyprojections/protocol-test-codegen-local/rest_json_extras/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen-local/build/smithyprojections/protocol-test-codegen-local/AwsQueryExtras/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen-local/build/smithyprojections/protocol-test-codegen-local/EventStream/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen-local/build/smithyprojections/protocol-test-codegen-local/RPCEventStream/swift-codegen/Package.swift
rm -f codegen/protocol-test-codegen-local/build/smithyprojections/protocol-test-codegen-local/Waiters/swift-codegen/Package.swift

# If on Mac, reopen Xcode to the refreshed tests
if [ -x "$(command -v osascript)" ]; then
Expand Down

0 comments on commit d4bf68b

Please sign in to comment.