Skip to content

Commit

Permalink
working basic types
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 committed Nov 20, 2024
1 parent b74e2a3 commit fc48c40
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 14 deletions.
102 changes: 97 additions & 5 deletions Source/AwsCommonRuntimeKit/crt/CBOR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public class CBOREncoder {
}

public func getEncoded() -> Data {
aws_cbor_encoder_get_encoded_data(self.rawValue).toData()
let encoded = aws_cbor_encoder_get_encoded_data(self.rawValue)
print(encoded)
let data = encoded.toData()
print(data)
return data
}

deinit {
Expand All @@ -90,14 +94,20 @@ public enum CBORType: Equatable {
public class CBORDecoder {
var rawValue: OpaquePointer
// Keep a reference to data to make it outlive the decoder
let data: Data
var c_array: [UInt8]

public init(data: Data) {
// TODO: Try init?
self.data = data
rawValue = self.data.withAWSByteCursorPointer {
aws_cbor_decoder_new(allocator.rawValue, $0.pointee)!
// 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;

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

View workflow job for this annotation

GitHub Actions / lint

Trailing Semicolon Violation: Lines should not have trailing semicolons (trailing_semicolon)
self.rawValue = self.c_array.withUnsafeBytes {
let cursor = aws_byte_cursor_from_array($0.baseAddress, count)
return aws_cbor_decoder_new(allocator.rawValue, cursor)!
}

}

public func decodeNext() throws -> CBORType {
Expand All @@ -115,8 +125,90 @@ public class CBORDecoder {
else {
throw CommonRunTimeError.crtError(.makeFromLastError())
}
print(out_value)
return .uint64(out_value)
}

case AWS_CBOR_TYPE_NEGINT:
do {
var out_value: UInt64 = 0
guard
aws_cbor_decoder_pop_next_negative_int_val(self.rawValue, &out_value)
== AWS_OP_SUCCESS
else {
throw CommonRunTimeError.crtError(.makeFromLastError())
}
print(out_value)
return .int(-(Int64(out_value+1)))
}
case AWS_CBOR_TYPE_FLOAT:
do {
var out_value: Double = 0
guard
aws_cbor_decoder_pop_next_float_val(self.rawValue, &out_value)
== AWS_OP_SUCCESS
else {
throw CommonRunTimeError.crtError(.makeFromLastError())
}
print(out_value)
return .double(out_value)
}
case AWS_CBOR_TYPE_BYTES:
do {
var out_value: aws_byte_cursor = aws_byte_cursor()
guard
aws_cbor_decoder_pop_next_bytes_val(self.rawValue, &out_value)
== AWS_OP_SUCCESS
else {
throw CommonRunTimeError.crtError(.makeFromLastError())
}
print(out_value)
return .bytes(out_value.toData())
}
case AWS_CBOR_TYPE_TEXT:
do {
var out_value: aws_byte_cursor = aws_byte_cursor()
guard
aws_cbor_decoder_pop_next_text_val(self.rawValue, &out_value)
== AWS_OP_SUCCESS
else {
throw CommonRunTimeError.crtError(.makeFromLastError())
}
print(out_value)
return .text(out_value.toString())
}
case AWS_CBOR_TYPE_BOOL:
do {
var out_value: Bool = false;

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

View workflow job for this annotation

GitHub Actions / lint

Trailing Semicolon Violation: Lines should not have trailing semicolons (trailing_semicolon)
guard
aws_cbor_decoder_pop_next_boolean_val(self.rawValue, &out_value)
== AWS_OP_SUCCESS
else {
throw CommonRunTimeError.crtError(.makeFromLastError())
}
return .bool(out_value)
}
case AWS_CBOR_TYPE_NULL:
do {
guard
aws_cbor_decoder_consume_next_single_element(self.rawValue)
== AWS_OP_SUCCESS
else {
throw CommonRunTimeError.crtError(.makeFromLastError())
}
return .null
}
case AWS_CBOR_TYPE_UNDEFINED:
do {
guard
aws_cbor_decoder_consume_next_single_element(self.rawValue)
== AWS_OP_SUCCESS
else {
throw CommonRunTimeError.crtError(.makeFromLastError())
}
return .undefined
}

default:
fatalError("invalid cbor decode type")
}
Expand Down
2 changes: 1 addition & 1 deletion Source/AwsCommonRuntimeKit/crt/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ extension TimeInterval {
extension aws_byte_cursor {

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

func toString() -> String {
Expand Down
25 changes: 17 additions & 8 deletions Test/AwsCommonRuntimeKitTests/crt/CBOR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,31 @@ import AwsCCommon
class CBORTests: XCBaseTestCase {

func testCBOREncode() async throws {
let uintValue: CBORType = .uint64(100)

let values: [CBORType] = [
.uint64(100),
.int(-100),
.double(10.59),
.bytes("hello".data(using: .utf8)!),
.text("hello"),
.bool(true),
.null,
.undefined,
]
// encode the values
let encoder = CBOREncoder()
encoder.encode(uintValue)
for value in values {
encoder.encode(value)
}
let encoded = encoder.getEncoded();

print(encoded)

let decoder = CBORDecoder(data: encoded)
while decoder.hasNext() {
let value = try! decoder.decodeNext()
XCTAssertEqual(value, .uint64(100))

for value in values {
XCTAssertTrue(decoder.hasNext())
XCTAssertEqual(try! decoder.decodeNext(), value)
}

XCTAssertFalse(decoder.hasNext())
}

}

0 comments on commit fc48c40

Please sign in to comment.