Skip to content

Commit

Permalink
try init
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 committed Nov 21, 2024
1 parent ff6035a commit 544094b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
35 changes: 18 additions & 17 deletions Source/AwsCommonRuntimeKit/crt/CBOR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ public enum CBORType: Equatable {
public class CBOREncoder {
var rawValue: OpaquePointer

public init() {
// TODO: Try init?
rawValue = aws_cbor_encoder_new(allocator.rawValue)!
public init() throws {
let rawValue = aws_cbor_encoder_new(allocator.rawValue)
guard let rawValue else {
throw CommonRunTimeError.crtError(.makeFromLastError())
}
self.rawValue = rawValue
}

public func encode(_ value: CBORType) {
Expand Down Expand Up @@ -58,7 +61,6 @@ public class CBOREncoder {
aws_cbor_encoder_write_bytes(self.rawValue, cursor.pointee)
}
}
//case .cbor_break: aws_cbor_encoder_write_break(self.rawValue)
case .map(let values):
do {
aws_cbor_encoder_write_map_start(self.rawValue, values.count)
Expand All @@ -76,7 +78,6 @@ public class CBOREncoder {
}
case .date(let value):
do {
// tag 1 means epoch based timestamp
aws_cbor_encoder_write_tag(self.rawValue, UInt64(AWS_CBOR_TAG_EPOCH_TIME))
aws_cbor_encoder_write_float(self.rawValue, value.timeIntervalSince1970)
}
Expand All @@ -95,10 +96,10 @@ public class CBOREncoder {
}
}

public func getEncoded() -> Data {
public func getEncoded() -> [UInt8] {
let encoded = aws_cbor_encoder_get_encoded_data(self.rawValue)
print(encoded)
let data = encoded.toData()
let data = encoded.toArray()
print(data)
return data
}
Expand All @@ -111,20 +112,20 @@ public class CBOREncoder {
public class CBORDecoder {
var rawValue: OpaquePointer
// Keep a reference to data to make it outlive the decoder
var c_array: [UInt8]
var data: [UInt8]

public init(data: Data) {
public init(data: [UInt8]) throws {
// TODO: Try init?
// TODO: Can we get rid of the copy here?
c_array = [UInt8](repeating: 0, count: data.count)
data.copyBytes(to: &c_array, count: data.count)

let count = c_array.count
self.rawValue = self.c_array.withUnsafeBytes {
self.data = data
let count = self.data.count
let rawValue = self.data.withUnsafeBytes {
let cursor = aws_byte_cursor_from_array($0.baseAddress, count)
return aws_cbor_decoder_new(allocator.rawValue, cursor)!
return aws_cbor_decoder_new(allocator.rawValue, cursor)
}

guard let rawValue else {
throw CommonRunTimeError.crtError(.makeFromLastError())
}
self.rawValue = rawValue
}

public func decodeNext() throws -> CBORType {

Check failure on line 131 in Source/AwsCommonRuntimeKit/crt/CBOR.swift

View workflow job for this annotation

GitHub Actions / lint

Function Body Length Violation: Function body should span 150 lines or less excluding comments and whitespace: currently spans 203 lines (function_body_length)
Expand Down
5 changes: 4 additions & 1 deletion Source/AwsCommonRuntimeKit/crt/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,14 @@ extension TimeInterval {
}

extension aws_byte_cursor {

func toData() -> Data {
Data(bytes: self.ptr, count: self.len)
}

func toArray() -> [UInt8] {
Array(UnsafeBufferPointer(start: self.ptr, count: self.len))
}

func toString() -> String {
if self.len == 0 {
return ""
Expand Down
4 changes: 2 additions & 2 deletions Test/AwsCommonRuntimeKitTests/crt/CBOR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ class CBORTests: XCBaseTestCase {
.indef_break,
]
// encode the values
let encoder = CBOREncoder()
let encoder = try! CBOREncoder()
for value in values {
encoder.encode(value)
}
let encoded = encoder.getEncoded();

print(encoded)

let decoder = CBORDecoder(data: encoded)
let decoder = try! CBORDecoder(data: encoded)
for value in values {
XCTAssertTrue(decoder.hasNext())
XCTAssertEqual(try! decoder.decodeNext(), value)
Expand Down

0 comments on commit 544094b

Please sign in to comment.