Skip to content

Commit

Permalink
feat: Updated Smithy Document interface (#812)
Browse files Browse the repository at this point in the history
  • Loading branch information
dayaffe authored Sep 16, 2024
1 parent 9e614f2 commit 2ac222b
Show file tree
Hide file tree
Showing 43 changed files with 1,209 additions and 187 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ let package = Package(
),
.testTarget(
name: "SmithyJSONTests",
dependencies: ["SmithyJSON", "ClientRuntime"]
dependencies: ["SmithyJSON", "ClientRuntime", "SmithyTestUtil"]
),
.testTarget(
name: "SmithyFormURLTests",
Expand Down
14 changes: 14 additions & 0 deletions Sources/Smithy/Document/DocumentError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

public enum DocumentError: Error {
case invalidJSONData
case typeMismatch(String)
case numberOverflow(String)
case invalidBase64(String)
case invalidDateFormat(String)
}
63 changes: 63 additions & 0 deletions Sources/Smithy/Document/Implementations/BigDecimalDocument.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

@_spi(SmithyDocumentImpl)
public struct BigDecimalDocument: SmithyDocument {
public var type: ShapeType { .bigDecimal }
let value: Double

public init(value: Double) {
self.value = value
}

public func asByte() throws -> Int8 {
guard let byte = Int8(exactly: value) else {
throw DocumentError.numberOverflow("BigDecimal \(value) overflows byte")
}
return byte
}

public func asShort() throws -> Int16 {
guard let short = Int16(exactly: value) else {
throw DocumentError.numberOverflow("BigDecimal \(value) overflows short")
}
return short
}

public func asInteger() throws -> Int {
guard let int = Int(exactly: value) else {
throw DocumentError.numberOverflow("BigDecimal \(value) overflows int")
}
return int
}

public func asLong() throws -> Int64 {
guard let long = Int64(exactly: value) else {
throw DocumentError.numberOverflow("BigDecimal \(value) overflows long")
}
return long
}

public func asFloat() throws -> Float {
guard let float = Float(exactly: value) else {
throw DocumentError.numberOverflow("BigDecimal \(value) overflows float")
}
return float
}

public func asDouble() throws -> Double {
value
}

public func asBigInteger() throws -> Int64 {
Int64(value)
}

public func asBigDecimal() throws -> Double {
value
}
}
54 changes: 54 additions & 0 deletions Sources/Smithy/Document/Implementations/BigIntegerDocument.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

@_spi(SmithyDocumentImpl)
public struct BigIntegerDocument: SmithyDocument {
public var type: ShapeType { .bigInteger }
let value: Int

public init(value: Int) {
self.value = value
}

public func asByte() throws -> Int8 {
guard let byte = Int8(exactly: value) else {
throw DocumentError.numberOverflow("BigInteger \(value) overflows byte")
}
return byte
}

public func asShort() throws -> Int16 {
guard let short = Int16(exactly: value) else {
throw DocumentError.numberOverflow("BigInteger \(value) overflows short")
}
return short
}

public func asInteger() throws -> Int {
value
}

public func asLong() throws -> Int64 {
Int64(value)
}

public func asFloat() throws -> Float {
Float(value)
}

public func asDouble() throws -> Double {
Double(value)
}

public func asBigInteger() throws -> Int64 {
Int64(value)
}

public func asBigDecimal() throws -> Double {
Double(value)
}
}
22 changes: 22 additions & 0 deletions Sources/Smithy/Document/Implementations/BlobDocument.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import struct Foundation.Data

@_spi(SmithyDocumentImpl)
public struct BlobDocument: SmithyDocument {
public var type: ShapeType { .blob }
let value: Data

public init(value: Data) {
self.value = value
}

public func asBlob() throws -> Data {
value
}
}
20 changes: 20 additions & 0 deletions Sources/Smithy/Document/Implementations/BooleanDocument.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

@_spi(SmithyDocumentImpl)
public struct BooleanDocument: SmithyDocument {
public var type: ShapeType { .boolean }
let value: Bool

public init(value: Bool) {
self.value = value
}

public func asBoolean() throws -> Bool {
value
}
}
48 changes: 48 additions & 0 deletions Sources/Smithy/Document/Implementations/ByteDocument.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

@_spi(SmithyDocumentImpl)
public struct ByteDocument: SmithyDocument {
public var type: ShapeType { .byte }
let value: Int8

public init(value: Int8) {
self.value = value
}

public func asByte() throws -> Int8 {
value
}

public func asShort() throws -> Int16 {
Int16(value)
}

public func asInteger() throws -> Int {
Int(value)
}

public func asLong() throws -> Int64 {
Int64(value)
}

public func asFloat() throws -> Float {
Float(value)
}

public func asDouble() throws -> Double {
Double(value)
}

public func asBigInteger() throws -> Int64 {
Int64(value)
}

public func asBigDecimal() throws -> Double {
Double(value)
}
}
101 changes: 101 additions & 0 deletions Sources/Smithy/Document/Implementations/Document.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import struct Foundation.Data
import struct Foundation.Date

/// A type-erased container for some value conforming to the `SmithyDocument` protocol.
public struct Document: SmithyDocument {
let document: SmithyDocument

public init(_ document: SmithyDocument) {
self.document = document
}
}

// Since protocol-bound values cannot conform to Self-referencing
// protocols like `Equatable`, the `Document` container provides the
// `Equatable` conformance.
extension Document: Equatable {

public static func ==(_ lhs: Document, _ rhs: Document) -> Bool {
isEqual(lhs.document, rhs.document)
}
}

// All of these implementations of `SmithyDocument` methods simply delegate
// to the inner document.
public extension Document {

var type: Smithy.ShapeType {
document.type
}

func asBoolean() throws -> Bool {
try document.asBoolean()
}

func asString() throws -> String {
try document.asString()
}

func asList() throws -> [SmithyDocument] {
try document.asList()
}

func asStringMap() throws -> [String: SmithyDocument] {
try document.asStringMap()
}

func size() -> Int {
document.size()
}

func asByte() throws -> Int8 {
try document.asByte()
}

func asShort() throws -> Int16 {
try document.asShort()
}

func asInteger() throws -> Int {
try document.asInteger()
}

func asLong() throws -> Int64 {
try document.asLong()
}

func asFloat() throws -> Float {
try document.asFloat()
}

func asDouble() throws -> Double {
try document.asDouble()
}

func asBigInteger() throws -> Int64 {
try document.asBigInteger()
}

func asBigDecimal() throws -> Double {
try document.asBigDecimal()
}

func asBlob() throws -> Data {
try document.asBlob()
}

func asTimestamp() throws -> Date {
try document.asTimestamp()
}

func getMember(_ memberName: String) throws -> SmithyDocument? {
try document.getMember(memberName)
}
}
Loading

0 comments on commit 2ac222b

Please sign in to comment.