-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Updated Smithy Document interface (#812)
- Loading branch information
Showing
43 changed files
with
1,209 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
Sources/Smithy/Document/Implementations/BigDecimalDocument.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
Sources/Smithy/Document/Implementations/BigIntegerDocument.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
Sources/Smithy/Document/Implementations/BlobDocument.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
Sources/Smithy/Document/Implementations/BooleanDocument.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
Sources/Smithy/Document/Implementations/ByteDocument.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.