-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Support RPCv2 CBOR wire protocol #887
Open
dayaffe
wants to merge
22
commits into
main
Choose a base branch
from
day/rpcv2-cbor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+880
−51
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b236c1a
add CBOR support to ServiceUtils (WireProtocol, AWSProtocol)
dayaffe c6a872c
add configurable protocol resolution with a default priority list
dayaffe 37fdc0e
make function makeQueryCompatibleError generic to work across multipl…
dayaffe cdbaadb
Merge branch 'main' into day/rpcv2-cbor
dayaffe 10dccdd
current version only has 3/69 failing protocol tests
dayaffe 50ceb88
ALL TESTS PASSING WOOOO
dayaffe 4eec99b
Merge branch 'main' into day/rpcv2-cbor
dayaffe ebde137
all tests passing now
dayaffe 6b1e91c
add getter for private context
dayaffe e587f61
Merge branch 'main' into day/rpcv2-cbor
dayaffe 7a321a0
add addUserAgentMiddleware fun to mocks
dayaffe 3e1f884
expose only service name instead of whole context
dayaffe 079a6c3
implement addUserAgentMiddleware in CBOR mock
dayaffe b8475ce
fix some lint issues
dayaffe 36628b4
fix some lint issues
dayaffe 9b2880b
fix imports
dayaffe f14c31b
more import fixing
dayaffe 2601702
more lint fixes
dayaffe 2713fbc
more lint
dayaffe 2de2171
try reorder
dayaffe 2b3120e
ran ktlintFormat
dayaffe 5bc3d23
Merge branch 'main' into day/rpcv2-cbor
dayaffe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,237 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import AwsCommonRuntimeKit | ||
import Foundation | ||
|
||
@_spi(SmithyReadWrite) import protocol SmithyReadWrite.SmithyReader | ||
@_spi(SmithyReadWrite) import enum SmithyReadWrite.ReaderError | ||
@_spi(SmithyReadWrite) import protocol SmithyReadWrite.SmithyWriter | ||
@_spi(Smithy) import struct Smithy.Document | ||
@_spi(Smithy) import protocol Smithy.SmithyDocument | ||
@_spi(SmithyTimestamps) import enum SmithyTimestamps.TimestampFormat | ||
@_spi(SmithyTimestamps) import struct SmithyTimestamps.TimestampFormatter | ||
|
||
@_spi(SmithyReadWrite) | ||
public final class Reader: SmithyReader { | ||
public typealias NodeInfo = String | ||
|
||
public let cborValue: CBORType? | ||
public let nodeInfo: NodeInfo | ||
public internal(set) var children: [Reader] = [] | ||
public internal(set) weak var parent: Reader? | ||
public var hasContent: Bool { cborValue != nil && cborValue != .null } | ||
|
||
public init(nodeInfo: NodeInfo, cborValue: CBORType?, parent: Reader? = nil) { | ||
self.nodeInfo = nodeInfo | ||
self.cborValue = cborValue | ||
self.parent = parent | ||
self.children = Self.children(from: cborValue, parent: self) | ||
} | ||
|
||
public static func from(data: Data) throws -> Reader { | ||
let decoder = try CBORDecoder(data: [UInt8](data)) | ||
let rootValue: CBORType | ||
if decoder.hasNext() { | ||
rootValue = try decoder.popNext() | ||
} else { | ||
rootValue = .null | ||
} | ||
return Reader(nodeInfo: "", cborValue: rootValue, parent: nil) | ||
} | ||
|
||
private static func children(from cborValue: CBORType?, parent: Reader) -> [Reader] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function helps form a graph for nested and complex types |
||
var children = [Reader]() | ||
switch cborValue { | ||
case .map(let map): | ||
for (key, value) in map { | ||
let child = Reader(nodeInfo: key, cborValue: value, parent: parent) | ||
children.append(child) | ||
} | ||
case .array(let array): | ||
for (index, value) in array.enumerated() { | ||
let child = Reader(nodeInfo: "\(index)", cborValue: value, parent: parent) | ||
children.append(child) | ||
} | ||
default: | ||
break | ||
} | ||
return children | ||
} | ||
|
||
public subscript(nodeInfo: NodeInfo) -> Reader { | ||
if let match = children.first(where: { $0.nodeInfo == nodeInfo }) { | ||
return match | ||
} else { | ||
return Reader(nodeInfo: nodeInfo, cborValue: nil, parent: self) | ||
} | ||
} | ||
|
||
public func readIfPresent() throws -> String? { | ||
switch cborValue { | ||
case .text(let string): | ||
return string | ||
case .indef_text_start: | ||
// Handle concatenation of indefinite-length text | ||
var combinedText = "" | ||
for child in children { | ||
if let chunk = try child.readIfPresent() as String? { | ||
combinedText += chunk | ||
} | ||
} | ||
return combinedText | ||
case .bytes(let data): | ||
return String(data: data, encoding: .utf8) | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
public func readIfPresent() throws -> Int8? { | ||
switch cborValue { | ||
case .int(let intValue): return Int8(intValue) | ||
case .uint(let uintValue): return Int8(uintValue) | ||
default: return nil | ||
} | ||
} | ||
|
||
public func readIfPresent() throws -> Int16? { | ||
switch cborValue { | ||
case .int(let intValue): return Int16(intValue) | ||
case .uint(let uintValue): return Int16(uintValue) | ||
default: return nil | ||
} | ||
} | ||
|
||
public func readIfPresent() throws -> Int? { | ||
switch cborValue { | ||
case .int(let intValue): return Int(intValue) | ||
case .uint(let uintValue): return Int(uintValue) | ||
default: return nil | ||
} | ||
} | ||
|
||
public func readIfPresent() throws -> Float? { | ||
switch cborValue { | ||
case .double(let doubleValue): return Float(doubleValue) | ||
case .int(let intValue): return Float(intValue) | ||
case .uint(let uintValue): return Float(uintValue) | ||
default: return nil | ||
} | ||
} | ||
|
||
public func readIfPresent() throws -> Double? { | ||
switch cborValue { | ||
case .double(let doubleValue): return doubleValue | ||
case .int(let intValue): return Double(intValue) | ||
case .uint(let uintValue): return Double(uintValue) | ||
default: return nil | ||
} | ||
} | ||
|
||
public func readIfPresent() throws -> Bool? { | ||
switch cborValue { | ||
case .bool(let boolValue): return boolValue | ||
default: return nil | ||
} | ||
} | ||
|
||
public func readIfPresent() throws -> Data? { | ||
switch cborValue { | ||
case .bytes(let data): return data | ||
case .text(let string): return Data(base64Encoded: string) | ||
default: return nil | ||
} | ||
} | ||
|
||
public func readIfPresent() throws -> Document? { | ||
guard let cborValue else { return nil } | ||
return Document(cborValue as! SmithyDocument) | ||
} | ||
|
||
public func readIfPresent<T>() throws -> T? where T: RawRepresentable, T.RawValue == Int { | ||
guard let rawValue: Int = try readIfPresent() else { return nil } | ||
return T(rawValue: rawValue) | ||
} | ||
|
||
public func readIfPresent<T>() throws -> T? where T: RawRepresentable, T.RawValue == String { | ||
guard let rawValue: String = try readIfPresent() else { return nil } | ||
return T(rawValue: rawValue) | ||
} | ||
|
||
public func readTimestampIfPresent(format: TimestampFormat) throws -> Date? { | ||
switch cborValue { | ||
case .double(let doubleValue): | ||
return Date(timeIntervalSince1970: doubleValue) | ||
case .int(let intValue): | ||
return Date(timeIntervalSince1970: Double(intValue)) | ||
case .uint(let uintValue): | ||
return Date(timeIntervalSince1970: Double(uintValue)) | ||
case .text(let string): | ||
return TimestampFormatter(format: format).date(from: string) | ||
case .date(let dateValue): | ||
return dateValue // Directly return the date value | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
public func readMapIfPresent<Value>( | ||
valueReadingClosure: (Reader) throws -> Value, | ||
keyNodeInfo: NodeInfo, | ||
valueNodeInfo: NodeInfo, | ||
isFlattened: Bool | ||
) throws -> [String: Value]? { | ||
guard let cborValue else { return nil } | ||
guard case .map(let map) = cborValue else { return nil } | ||
var dict = [String: Value]() | ||
for (key, _) in map { | ||
let reader = self[key] | ||
do { | ||
let value = try valueReadingClosure(reader) | ||
dict[key] = value | ||
} catch ReaderError.requiredValueNotPresent { | ||
if !(try reader.readNullIfPresent() ?? false) { throw ReaderError.requiredValueNotPresent } | ||
} | ||
} | ||
return dict | ||
} | ||
|
||
public func readListIfPresent<Member>( | ||
memberReadingClosure: (Reader) throws -> Member, | ||
memberNodeInfo: NodeInfo, | ||
isFlattened: Bool | ||
) throws -> [Member]? { | ||
guard let cborValue else { return nil } | ||
guard case .array = cborValue else { return nil } | ||
|
||
return try children.map { child in | ||
// Check if the child is an indefinite-length text | ||
if case .indef_text_start = child.cborValue { | ||
var combinedText = "" | ||
for grandChild in child.children { | ||
if let chunk = try grandChild.readIfPresent() as String? { | ||
combinedText += chunk | ||
} | ||
} | ||
// Return the combined text | ||
return combinedText as! Member | ||
} else { | ||
// Handle regular values | ||
return try memberReadingClosure(child) | ||
} | ||
} | ||
} | ||
|
||
public func readNullIfPresent() throws -> Bool? { | ||
if cborValue == .null { | ||
return true | ||
} else { | ||
return nil | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this function calls CBORDecoder which is defined in CRT for actually decoding individual types