Skip to content

Commit

Permalink
Merge pull request #19 from christophhagen/fix-wrappers
Browse files Browse the repository at this point in the history
Fix wrapper encoding for other encoders
  • Loading branch information
christophhagen authored Apr 8, 2024
2 parents 0b9ceaa + 6aea3d4 commit c8314c3
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Sources/BinaryCodable/Wrappers/FixedSizeEncoded.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ extension FixedSizeEncoded: Encodable {
*/
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self)
try container.encode(wrappedValue)
}
}

Expand All @@ -261,6 +261,6 @@ extension FixedSizeEncoded: Decodable {
*/
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self = try container.decode(Self.self)
wrappedValue = try container.decode(WrappedValue.self)
}
}
4 changes: 2 additions & 2 deletions Sources/BinaryCodable/Wrappers/VariableLengthCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public typealias VariableLengthCodable = VariableLengthEncodable & VariableLengt
/**
A type that can be encoded as a variable-length integer
*/
public protocol VariableLengthEncodable: FixedWidthInteger {
public protocol VariableLengthEncodable: FixedWidthInteger, Encodable {

/// The value encoded as binary data using variable-length integer encoding
var variableLengthEncoding: Data { get }
Expand All @@ -17,7 +17,7 @@ public protocol VariableLengthEncodable: FixedWidthInteger {
/**
A type that can be decoded as a variable-length integer
*/
public protocol VariableLengthDecodable: FixedWidthInteger {
public protocol VariableLengthDecodable: FixedWidthInteger, Decodable {

/**
Decode a value as a variable-length integer.
Expand Down
4 changes: 2 additions & 2 deletions Sources/BinaryCodable/Wrappers/VariableLengthEncoded.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ extension VariableLengthEncoded: Encodable {
*/
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self)
try container.encode(wrappedValue)
}
}

Expand All @@ -269,6 +269,6 @@ extension VariableLengthEncoded: Decodable {
*/
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self = try container.decode(Self.self)
wrappedValue = try container.decode(WrappedValue.self)
}
}
4 changes: 2 additions & 2 deletions Sources/BinaryCodable/Wrappers/ZigZagCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public typealias ZigZagCodable = ZigZagEncodable & ZigZagDecodable
/**
A type that can be encoded as a zig-zag variable-length integer
*/
public protocol ZigZagEncodable {
public protocol ZigZagEncodable: Encodable {

/// The value encoded as binary data using zig-zag variable-length integer encoding
var zigZagEncoded: Data { get }
Expand All @@ -22,7 +22,7 @@ public protocol ZigZagEncodable {
/**
A type that can be decoded as a zig-zag variable-length integer
*/
public protocol ZigZagDecodable {
public protocol ZigZagDecodable: Decodable {

/**
Decode a value as a zig-zag variable-length integer.
Expand Down
4 changes: 2 additions & 2 deletions Sources/BinaryCodable/Wrappers/ZigZagEncoded.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ extension ZigZagEncoded: Encodable {
*/
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self)
try container.encode(wrappedValue)
}
}

Expand All @@ -269,6 +269,6 @@ extension ZigZagEncoded: Decodable {
*/
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self = try container.decode(Self.self)
wrappedValue = try container.decode(WrappedValue.self)
}
}
72 changes: 72 additions & 0 deletions Tests/BinaryCodableTests/WrapperEncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,76 @@ final class WrapperEncodingTests: XCTestCase {
123 // Int 123, variable-length encoded
])
}

/**
This test ensures that the fixed wrapper is transparent for other encoders
*/
func testEncodeFixedWrapperWithJSON() throws {
struct Test: Codable, Equatable {
@FixedSizeEncoded
var val: Int
}

struct Test2: Codable, Equatable {
var val: Int
}

let value = Test(val: 123)
let value2 = Test2(val: 123)
let encoded = try JSONEncoder().encode(value)
let encoded2 = try JSONEncoder().encode(value2)
XCTAssertEqual(encoded, encoded2)
let decoded = try JSONDecoder().decode(Test.self, from: encoded)
let decoded2 = try JSONDecoder().decode(Test2.self, from: encoded2)
XCTAssertEqual(decoded, value)
XCTAssertEqual(decoded2, value2)
}

/**
This test ensures that the zig-zag wrapper is transparent for other encoders
*/
func testEncodeZigZagWrapperWithJSON() throws {
struct Test: Codable, Equatable {
@ZigZagEncoded
var val: Int16
}

struct Test2: Codable, Equatable {
var val: Int16
}

let value = Test(val: 123)
let value2 = Test2(val: 123)
let encoded = try JSONEncoder().encode(value)
let encoded2 = try JSONEncoder().encode(value2)
XCTAssertEqual(encoded, encoded2)
let decoded = try JSONDecoder().decode(Test.self, from: encoded)
let decoded2 = try JSONDecoder().decode(Test2.self, from: encoded2)
XCTAssertEqual(decoded, value)
XCTAssertEqual(decoded2, value2)
}

/**
This test ensures that the variable length wrapper is transparent for other encoders
*/
func testEncodeVariableLengthWrapperWithJSON() throws {
struct Test: Codable, Equatable {
@VariableLengthEncoded
var val: Int
}

struct Test2: Codable, Equatable {
var val: Int
}

let value = Test(val: 123)
let value2 = Test2(val: 123)
let encoded = try JSONEncoder().encode(value)
let encoded2 = try JSONEncoder().encode(value2)
XCTAssertEqual(encoded, encoded2)
let decoded = try JSONDecoder().decode(Test.self, from: encoded)
let decoded2 = try JSONDecoder().decode(Test2.self, from: encoded2)
XCTAssertEqual(decoded, value)
XCTAssertEqual(decoded2, value2)
}
}

0 comments on commit c8314c3

Please sign in to comment.