Releases: christophhagen/BinaryCodable
Compile on 32-bit Systems
This release fixes compilation on 32-bit systems.
The upper and lower bounds for valid integer coding keys where given as string literals, and required 64-bit wide integers. The bounds now adapt to the system the library compiles on.
Linux compatibility
This bugfix release fixes compiling on Linux systems, where certain Swift functions, like CFSwapInt64HostToLittle
are not available.
Additional changes:
- Adds documentation using Jazzy
- Cleaner definitions for the property wrappers
SignedValue
andFixedSize
.
Protobuf OneOf Support
This version adds support for the protobuf Oneof feature.
A new protocol ProtobufOneOf
signals that enums with associated values should be treated as a Oneof
on the wire.
public protocol ProtobufOneOf { }
A protobuf definition of a oneof
:
syntax = "proto3";
message ExampleOneOf {
int32 field1 = 1;
oneof alternatives {
int64 id = 2;
string name = 3;
}
}
The corresponding Swift definition would be:
struct ExampleOneOf: Codable {
let field1: Int32
// The oneof field
let alternatives: Alternatives
// The OneOf definition
enum Alternatives: Codable, ProtobufOneOf {
case id(Int64)
case name(String)
// Field values, must not overlap with `ExampleOneOf.CodingKeys`
enum CodingKeys: Int, CodingKey {
case id = 2
case name = 3
}
}
enum CodingKeys: Int, CodingKey {
case field1 = 1
// The field id of the Oneof field is not used
case alternatives = 123456
}
}
There are additional error cases added to signal errors during encoding and decoding:
For ProtobufEncodingError
:
/**
An unavailable encoding feature was accessed.
The associated value contains a textual description of the unsupported access.
*/
case invalidAccess(String)
For ProtobufDecodingError
:
/**
A decoding feature was accessed which is not supported for protobuf encoding.
The associated value contains a textual description of the invalid access.
*/
case invalidAccess(String)
Error handling and function calls
Error cases
This release ensures that all errors thrown for encoding and decoding are actually of type BinaryEncodingError
and BinaryDecodingError
, by introducing additional error cases for unknown errors, as well as equivalents to the Swift EncodingError
and DecodingError
types.
BinaryEncodingError
gets the new cases:
case invalidValue(Any, EncodingError.Context)
case unknownError(Error)
while `BinaryDecodingError gets the new cases:
case dataCorrupted(DecodingError.Context)
case typeMismatch(Any.Type, DecodingError.Context)
case valueNotFound(Any.Type, DecodingError.Context)
case unknownError(Error)
Encoding function calls
This release also relaxes the encoding function definitions, switching from:
public func encode<T>(_ value: T) throws -> Data where T: Encodable
to:
public func encode(_ value: Encodable) throws -> Data
which allows more calls, such as:
let values: [Encodable] = ["Some", 123, true]
// Previously not possible
let data = try values.map { try BinaryEncoder.encode($0) }
Protobuf support
The first major release of the library provides increased compatibility with Protocol Buffers, and fixes a few errors.
Major changes:
- Extracted Protocol Buffer functionality to new types
ProtobufEncoder
andProtobufDecoder
- Introduces new Errors
ProtobufEncodingError
andProtobufDecodingError
- Allow setting of custom
userInfo
for encoding and decoding. - Treat
Int
andUint
as 64-bit types during encoding. - Use
Varint
encoding for proto types by default - Introduce
SignedInteger
wrapper to enforceZigZag
encoding for protobuf types. - Add static one-shot encoding and decoding functions
- More tests
0.9.0
This is the first official release of BinaryCodable
.
It provides support for many (tested) Swift types, and also features limited compatibility with the Google Protobuf format.