Skip to content

Commit 77e2e3c

Browse files
Merge pull request #5 from SomeRandomiOSDev/1.0.3
Changed visibility of CBOREncoded
2 parents f412cc4 + 9828ae8 commit 77e2e3c

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
CBORCoding.xcodeproj/project.xcworkspace
22
CBORCoding.xcodeproj/xcuserdata
33
.build
4+
.swiftpm

CBORCoding/CBOR+Codable.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,13 @@ extension CBOR.IndefiniteLengthString: Codable {
334334

335335
extension CBOR.CBOREncoded: Encodable {
336336

337-
func encode(to encoder: Encoder) throws {
338-
var container = encoder.singleValueContainer()
339-
try container.encode(self)
337+
public func encode(to encoder: Encoder) throws {
338+
if let encoder = encoder as? __CBOREncoder {
339+
var container = encoder.singleValueContainer()
340+
try container.encode(self)
341+
} else {
342+
var container = encoder.singleValueContainer()
343+
try container.encode(encodedData)
344+
}
340345
}
341346
}

CBORCoding/CBOR.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ public struct CBOR {
193193
self.chunks = chunks
194194
}
195195
}
196+
197+
/// A type that asserts its data is already in CBOR encoded format. No additional
198+
/// encoding is done on the contained byte data
199+
public struct CBOREncoded {
200+
201+
// MARK: - Fields
202+
203+
let encodedData: Data
204+
}
196205
}
197206

198207
// MARK: - CBOR Extension
@@ -397,15 +406,6 @@ extension CBOR {
397406
// swiftlint:enable force_unwrapping
398407
}
399408

400-
/// A type that asserts its data is already in CBOR encoded format. No additional
401-
/// encoding is done on the contained byte data
402-
internal struct CBOREncoded {
403-
404-
// MARK: - Fields
405-
406-
let encodedData: Data
407-
}
408-
409409
// MARK: Internal Methods
410410

411411
internal static func majorType(for byte: UInt8) -> MajorType {

CBORCodingTests/CBORTests.swift

+10
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,16 @@ class CBORTests: XCTestCase {
490490
XCTAssertEqual(data, encodedData)
491491
}
492492

493+
func testEncodeCBOREncodedWithOtherEncoder() {
494+
let encoder = JSONEncoder()
495+
let dataToEncode = Data("CBOR".utf8)
496+
var encoded1 = Data(), encoded2 = Data()
497+
498+
XCTAssertNoThrow(encoded1 = try encoder.encode([dataToEncode]))
499+
XCTAssertNoThrow(encoded2 = try encoder.encode([CBOR.CBOREncoded(encodedData: dataToEncode)]))
500+
XCTAssertEqual(encoded1, encoded2)
501+
}
502+
493503
func testDirectlyEncodeCBOREncoded() {
494504
struct Test: Encodable {
495505

0 commit comments

Comments
 (0)