Skip to content

Commit

Permalink
pop next
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 committed Nov 21, 2024
1 parent 4ccb796 commit d5ceda8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
13 changes: 6 additions & 7 deletions Source/AwsCommonRuntimeKit/crt/CBOR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public class CBOREncoder {
case .int(let value):
do {
if value >= 0 {
// TODO: This is unexpected because decoding doesn't return the same value. We should just
// throw error in this case.
aws_cbor_encoder_write_uint(self.rawValue, UInt64(value))
} else {
aws_cbor_encoder_write_negint(self.rawValue, UInt64(-1 - value))
Expand Down Expand Up @@ -121,7 +119,7 @@ public class CBORDecoder {
self.rawValue = rawValue
}

public func decodeNext() throws -> CBORType {
public func popNext() throws -> CBORType {

Check failure on line 122 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)
var cbor_type: aws_cbor_type = AWS_CBOR_TYPE_UNKNOWN
guard aws_cbor_decoder_peek_type(self.rawValue, &cbor_type) == AWS_OP_SUCCESS else {
throw CommonRunTimeError.crtError(.makeFromLastError())
Expand Down Expand Up @@ -223,7 +221,7 @@ public class CBORDecoder {
// TODO: fix error
throw CommonRunTimeError.crtError(.makeFromLastError())
}
let timestamp = try decodeNext()
let timestamp = try popNext()

if case .double(let value) = timestamp {
return .date(Date.init(timeIntervalSince1970: value))
Expand All @@ -245,7 +243,7 @@ public class CBORDecoder {
}
var array: [CBORType] = []
for _ in 0..<out_value {
array.append(try decodeNext())
array.append(try popNext())
}
return .array(array)
case AWS_CBOR_TYPE_MAP_START:
Expand All @@ -258,9 +256,9 @@ public class CBORDecoder {
}
var map: [String: CBORType] = [:]
for _ in 0..<out_value {
let key = try decodeNext()
let key = try popNext()
if case .text(let key) = key {
map[key] = try decodeNext()
map[key] = try popNext()
} else {
// TODO: fix error
throw CommonRunTimeError.crtError(.makeFromLastError())
Expand Down Expand Up @@ -333,6 +331,7 @@ public class CBORDecoder {
}
}

// todo: docs
public func hasNext() -> Bool {
aws_cbor_decoder_get_remaining_length(self.rawValue) != 0
}
Expand Down
2 changes: 1 addition & 1 deletion Test/AwsCommonRuntimeKitTests/crt/CBOR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class CBORTests: XCBaseTestCase {
let decoder = try! CBORDecoder(data: encoded)
for value in values {
XCTAssertTrue(decoder.hasNext())
XCTAssertEqual(try! decoder.decodeNext(), value)
XCTAssertEqual(try! decoder.popNext(), value)
}
XCTAssertFalse(decoder.hasNext())
}
Expand Down

0 comments on commit d5ceda8

Please sign in to comment.