Skip to content

Commit

Permalink
feat!: Conceal serde types with Swift SPI (#805)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbelkins committed Sep 3, 2024
1 parent e7b994f commit 2936b2f
Show file tree
Hide file tree
Showing 84 changed files with 295 additions and 205 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ let package = Package(
),
.target(
name: "SmithyReadWrite",
dependencies: ["SmithyTimestamps"]
dependencies: ["Smithy", "SmithyTimestamps"]
),
.target(
name: "SmithyXML",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import class SmithyHTTPAPI.HTTPResponse
import enum Smithy.ByteStream
import protocol SmithyReadWrite.WireDataProviding
@_spi(SmithyReadWrite) import protocol SmithyReadWrite.WireDataProviding
import struct Foundation.Data

@_spi(SmithyReadWrite)
extension HTTPResponse: WireDataProviding {

public func data() async throws -> Data {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import SmithyHTTPAPI
import SmithyReadWrite
@_spi(SmithyReadWrite) import SmithyReadWrite
import class Foundation.DateFormatter
import struct Foundation.Locale
import struct Foundation.TimeInterval
Expand All @@ -15,6 +15,7 @@ import struct Foundation.UUID
import class Smithy.Context
import protocol Smithy.ResponseMessageDeserializer

@_spi(SmithyReadWrite)
public struct DeserializeMiddleware<OperationStackOutput> {
public var id: String = "Deserialize"
let wireResponseClosure: WireResponseOutputClosure<HTTPResponse, OperationStackOutput>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import Smithy
import SmithyHTTPAPI
import struct Foundation.Data
import protocol SmithyReadWrite.SmithyWriter
import typealias SmithyReadWrite.WritingClosure
@_spi(SmithyReadWrite) import protocol SmithyReadWrite.SmithyWriter
@_spi(SmithyReadWrite) import typealias SmithyReadWrite.WritingClosure

@_spi(SmithyReadWrite)
public struct BodyMiddleware<OperationStackInput,
OperationStackOutput,
Writer: SmithyWriter> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SmithyEventStreams
import SmithyEventStreamsAPI
import SmithyEventStreamsAuthAPI
import struct Foundation.Data
import typealias SmithyReadWrite.WritingClosure
@_spi(SmithyReadWrite) import typealias SmithyReadWrite.WritingClosure
import SmithyHTTPAPI

public struct EventStreamBodyMiddleware<OperationStackInput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import enum Smithy.ClientError
import protocol Smithy.RequestMessageSerializer
import class Smithy.Context
import struct Foundation.Data
import protocol SmithyReadWrite.SmithyWriter
import typealias SmithyReadWrite.WritingClosure
@_spi(SmithyReadWrite) import protocol SmithyReadWrite.SmithyWriter
@_spi(SmithyReadWrite) import typealias SmithyReadWrite.WritingClosure
import SmithyHTTPAPI

@_spi(SmithyReadWrite)
public struct PayloadBodyMiddleware<OperationStackInput,
OperationStackOutput,
OperationStackInputPayload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
// SPDX-License-Identifier: Apache-2.0
//

import struct SmithyTimestamps.TimestampFormatter
import enum SmithyTimestamps.TimestampFormat
@_spi(SmithyTimestamps) import struct SmithyTimestamps.TimestampFormatter
@_spi(SmithyTimestamps) import enum SmithyTimestamps.TimestampFormat

@_spi(SmithyTimestamps)
public typealias TimestampFormatter = SmithyTimestamps.TimestampFormatter

@_spi(SmithyTimestamps)
public typealias TimestampFormat = SmithyTimestamps.TimestampFormat

This file was deleted.

7 changes: 4 additions & 3 deletions Sources/SmithyFormURL/Writer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
// SPDX-License-Identifier: Apache-2.0
//

import protocol SmithyReadWrite.SmithyWriter
@_spi(SmithyReadWrite) import protocol SmithyReadWrite.SmithyWriter
import enum SmithyReadWrite.Document
import enum SmithyTimestamps.TimestampFormat
import struct SmithyTimestamps.TimestampFormatter
@_spi(SmithyTimestamps) import enum SmithyTimestamps.TimestampFormat
@_spi(SmithyTimestamps) import struct SmithyTimestamps.TimestampFormatter
import struct Foundation.Data
import struct Foundation.Date
import struct Foundation.CharacterSet

@_spi(SmithyReadWrite)
public final class Writer: SmithyWriter {
public typealias NodeInfo = SmithyFormURL.NodeInfo

Expand Down
7 changes: 4 additions & 3 deletions Sources/SmithyJSON/Reader/Reader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
// SPDX-License-Identifier: Apache-2.0
//

import protocol SmithyReadWrite.SmithyReader
@_spi(SmithyReadWrite) import protocol SmithyReadWrite.SmithyReader
import enum SmithyReadWrite.Document
import typealias SmithyReadWrite.ReadingClosure
import enum SmithyReadWrite.ReaderError
import enum SmithyTimestamps.TimestampFormat
import struct SmithyTimestamps.TimestampFormatter
@_spi(SmithyTimestamps) import enum SmithyTimestamps.TimestampFormat
@_spi(SmithyTimestamps) import struct SmithyTimestamps.TimestampFormatter
import struct Foundation.Data
import struct Foundation.Date
import class Foundation.NSNull
import class Foundation.NSNumber
import func CoreFoundation.CFGetTypeID
import func CoreFoundation.CFBooleanGetTypeID

@_spi(SmithyReadWrite)
public final class Reader: SmithyReader {
public typealias NodeInfo = SmithyJSON.NodeInfo

Expand Down
7 changes: 4 additions & 3 deletions Sources/SmithyJSON/Writer/Writer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
// SPDX-License-Identifier: Apache-2.0
//

import protocol SmithyReadWrite.SmithyWriter
@_spi(SmithyReadWrite) import protocol SmithyReadWrite.SmithyWriter
import enum SmithyReadWrite.Document
import enum SmithyTimestamps.TimestampFormat
import struct SmithyTimestamps.TimestampFormatter
@_spi(SmithyTimestamps) import enum SmithyTimestamps.TimestampFormat
@_spi(SmithyTimestamps) import struct SmithyTimestamps.TimestampFormatter
import struct Foundation.Data
import struct Foundation.Date
import class Foundation.NSNumber

@_spi(SmithyReadWrite)
public final class Writer: SmithyWriter {
public typealias NodeInfo = SmithyJSON.NodeInfo

Expand Down
1 change: 1 addition & 0 deletions Sources/SmithyReadWrite/DataProviding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import struct Foundation.Data

@_spi(SmithyReadWrite)
public protocol WireDataProviding: AnyObject {

func data() async throws -> Data
Expand Down
12 changes: 11 additions & 1 deletion Sources/SmithyReadWrite/ReadingClosure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

import struct Foundation.Data
import struct Foundation.Date
import enum SmithyTimestamps.TimestampFormat
@_spi(SmithyTimestamps) import enum SmithyTimestamps.TimestampFormat

@_spi(SmithyReadWrite)
public typealias ReadingClosure<T, Reader> = (Reader) throws -> T

@_spi(SmithyReadWrite)
public func mapReadingClosure<T, Reader: SmithyReader>(
valueReadingClosure: @escaping ReadingClosure<T, Reader>,
keyNodeInfo: Reader.NodeInfo,
Expand All @@ -27,6 +29,7 @@ public func mapReadingClosure<T, Reader: SmithyReader>(
}
}

@_spi(SmithyReadWrite)
public func mapReadingClosure<T, Reader: SmithyReader>(
valueReadingClosure: @escaping ReadingClosure<T, Reader>,
keyNodeInfo: Reader.NodeInfo,
Expand All @@ -43,6 +46,7 @@ public func mapReadingClosure<T, Reader: SmithyReader>(
}
}

@_spi(SmithyReadWrite)
public func listReadingClosure<T, Reader: SmithyReader>(
memberReadingClosure: @escaping ReadingClosure<T, Reader>,
memberNodeInfo: Reader.NodeInfo,
Expand All @@ -57,6 +61,7 @@ public func listReadingClosure<T, Reader: SmithyReader>(
}
}

@_spi(SmithyReadWrite)
public func listReadingClosure<T, Reader: SmithyReader>(
memberReadingClosure: @escaping ReadingClosure<T, Reader>,
memberNodeInfo: Reader.NodeInfo,
Expand All @@ -71,18 +76,21 @@ public func listReadingClosure<T, Reader: SmithyReader>(
}
}

@_spi(SmithyReadWrite)
public func timestampReadingClosure<Reader: SmithyReader>(format: TimestampFormat) -> ReadingClosure<Date, Reader> {
return { reader in
try reader.readTimestamp(format: format)
}
}

@_spi(SmithyReadWrite)
public func timestampReadingClosure<Reader: SmithyReader>(format: TimestampFormat) -> ReadingClosure<Date?, Reader> {
return { reader in
try reader.readTimestampIfPresent(format: format)
}
}

@_spi(SmithyReadWrite)
public struct ReadingClosureBox<Value: RawRepresentable> {

public init() {}
Expand All @@ -104,6 +112,7 @@ public struct ReadingClosureBox<Value: RawRepresentable> {
}
}

@_spi(SmithyReadWrite)
public enum ReadingClosures {

public static func readString<Reader: SmithyReader>(from reader: Reader) throws -> String {
Expand Down Expand Up @@ -163,6 +172,7 @@ public enum ReadingClosures {
}
}

@_spi(SmithyReadWrite)
public func optionalFormOf<T, Reader: SmithyReader>(
readingClosure: @escaping ReadingClosure<T, Reader>
) -> ReadingClosure<T?, Reader> {
Expand Down
3 changes: 2 additions & 1 deletion Sources/SmithyReadWrite/SmithyReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import struct Foundation.Data
import struct Foundation.Date
import enum SmithyTimestamps.TimestampFormat
@_spi(SmithyTimestamps) import enum SmithyTimestamps.TimestampFormat

@_spi(SmithyReadWrite)
public protocol SmithyReader: AnyObject {
associatedtype NodeInfo

Expand Down
15 changes: 14 additions & 1 deletion Sources/SmithyReadWrite/SmithyWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import struct Foundation.Data
import struct Foundation.Date
import enum SmithyTimestamps.TimestampFormat
@_spi(SmithyTimestamps) import enum SmithyTimestamps.TimestampFormat
import enum Smithy.ByteStream

@_spi(SmithyReadWrite)
public protocol SmithyWriter: AnyObject {
associatedtype NodeInfo

Expand Down Expand Up @@ -47,6 +49,17 @@ public protocol SmithyWriter: AnyObject {

public extension SmithyWriter {

func write(_ value: ByteStream?) throws {
// This serialization will never be performed in practice, since
// a ByteStream will never be a part of
// a XML body - if there is a streaming member in a restXml
// input shape, the rest of the input members must all be bound
// to HTML components outside the body.
//
// This empty implementation is only provided to quiet the
// compiler when a structure with a ByteSteam is code-generated.
}

static func write<T>(
_ value: T,
rootNodeInfo: NodeInfo,
Expand Down
1 change: 1 addition & 0 deletions Sources/SmithyReadWrite/WireResponseErrorClosure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
//

/// Defines a closure that can be used to convert a wire response to a Swift `Error`.
@_spi(SmithyReadWrite)
public typealias WireResponseErrorClosure<WireResponse> = (WireResponse) async throws -> Error
1 change: 1 addition & 0 deletions Sources/SmithyReadWrite/WireResponseOutputClosure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
//

/// Defines a closure that can be used to convert a wire response to an output value.
@_spi(SmithyReadWrite)
public typealias WireResponseOutputClosure<WireResponse, OperationStackOutput> =
(WireResponse) async throws -> OperationStackOutput
9 changes: 8 additions & 1 deletion Sources/SmithyReadWrite/WritingClosure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

import struct Foundation.Data
import struct Foundation.Date
import enum SmithyTimestamps.TimestampFormat
@_spi(SmithyTimestamps) import enum SmithyTimestamps.TimestampFormat

@_spi(SmithyReadWrite)
public typealias WritingClosure<T, Writer> = (T, Writer) throws -> Void

@_spi(SmithyReadWrite)
public func mapWritingClosure<T, Writer: SmithyWriter>(
valueWritingClosure: @escaping WritingClosure<T, Writer>,
keyNodeInfo: Writer.NodeInfo,
Expand All @@ -28,6 +30,7 @@ public func mapWritingClosure<T, Writer: SmithyWriter>(
}
}

@_spi(SmithyReadWrite)
public func listWritingClosure<T, Writer: SmithyWriter>(
memberWritingClosure: @escaping WritingClosure<T, Writer>,
memberNodeInfo: Writer.NodeInfo,
Expand All @@ -43,12 +46,14 @@ public func listWritingClosure<T, Writer: SmithyWriter>(
}
}

@_spi(SmithyReadWrite)
public func timestampWritingClosure<Writer: SmithyWriter>(format: TimestampFormat) -> WritingClosure<Date, Writer> {
return { date, writer in
try writer.writeTimestamp(date, format: format)
}
}

@_spi(SmithyReadWrite)
public struct WritingClosureBox<Value: RawRepresentable> {

public init() {}
Expand All @@ -62,6 +67,7 @@ public struct WritingClosureBox<Value: RawRepresentable> {
}
}

@_spi(SmithyReadWrite)
public enum WritingClosures {

public static func writeString<Writer: SmithyWriter>(value: String?, to writer: Writer) throws {
Expand Down Expand Up @@ -101,6 +107,7 @@ public enum WritingClosures {
}
}

@_spi(SmithyReadWrite)
public func sparseFormOf<T, Writer: SmithyWriter>(
writingClosure: @escaping WritingClosure<T?, Writer>
) -> WritingClosure<T?, Writer> {
Expand Down
3 changes: 2 additions & 1 deletion Sources/SmithyTestUtil/Errors/TestBaseError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import protocol ClientRuntime.BaseError
import enum ClientRuntime.BaseErrorDecodeError
import class SmithyHTTPAPI.HTTPResponse
import class SmithyJSON.Reader
@_spi(SmithyReadWrite) import class SmithyJSON.Reader

@_spi(SmithyReadWrite)
public struct TestBaseError: BaseError {
public let code: String
public let message: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import Smithy
import SmithyHTTPAPI
import SmithyReadWrite
@_spi(SmithyReadWrite) import SmithyReadWrite
import ClientRuntime

@_spi(SmithyReadWrite)
public struct MockDeserializeMiddleware<OperationStackOutput> {
public var id: String
let responseClosure: WireResponseOutputClosure<HTTPResponse, OperationStackOutput>
Expand Down
Loading

0 comments on commit 2936b2f

Please sign in to comment.