Skip to content

Commit ba9ec3f

Browse files
authored
Remove EncodedData replacing with Array<UInt8> (#20)
* Replace `EncodedData` with `Array<UInt8>`. * Update documentation after replacing `EncodedData` with `Array<UInt8>`. * Change signature of StorageContainerReaderWriter.write from `write(_ storageContainer: StorageContainer, to buffer: UnsafeMutableRawBufferPointer) -> Int` to `convert(_ storageContainer: StorageContainer) -> [UInt8]`. * Renamed EncodedDataNegativeTests to BinaryDecoderByteArrayTests. * Adjust binary format alignment decreasing padding.
1 parent c4da3fb commit ba9ec3f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+631
-3383
lines changed

.jazzy.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ custom_categories:
4040
children:
4141
- BinaryEncoder
4242
- BinaryDecoder
43-
- EncodedData
44-
- name: EncodedData Transformation
45-
children:
46-
- Array
47-
- Data
4843
- name: Supplemental Information
4944
children:
5045
- Binary Format

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ All significant changes to this project will be documented in this file.
33

44
## [1.0.0-beta.5](https://github.com/stickytools/sticky-encoding/tree/1.0.0-beta.5)
55

6+
#### Removed
7+
- Simplified the interface removing the `EncodedData` type replacing it with straight `Array<UInt8>`.
8+
69
#### Added
10+
- Changed return type of `BinaryEncoder.encode` from `EncodedData` to `Array<UInt8>`.
11+
- Changed parameter type of `BinaryDecoder.decode` from `EncodedData` to `Array<UInt8>`.
712
- Moving binary conversion into decode/encode stage to improve error recovery on invalid binary input.
813

914
## [1.0.0-beta.4](https://github.com/stickytools/sticky-encoding/tree/1.0.0-beta.4)
@@ -17,7 +22,7 @@ All significant changes to this project will be documented in this file.
1722
- Added support to/from `Swift.Data` to `EncodedData`.
1823

1924
#### Changed
20-
- Changed EncodedData `var bytes: [UInt8]` to `Array<UInt8>(_ encodedData: EncodedDate)` constructor.
25+
- Changed EncodedData `var bytes: [UInt8]` to `Array<UInt8>(_ bytes: EncodedDate)` constructor.
2126

2227
## [1.0.0-beta.2](https://github.com/stickytools/sticky-encoding/tree/1.0.0-beta.2)
2328

README.md

+8-22
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ of the BinaryEncoder and call `encode`.
5454
```Swift
5555
let string = "You can encode single values of any type."
5656

57-
let encoded = try encoder.encode(string)
57+
let bytes = try encoder.encode(string)
5858
```
5959
Basic structs and classes can also be encoded.
6060
```Swift
@@ -66,7 +66,7 @@ Basic structs and classes can also be encoded.
6666

6767
let employee = Employee(first: "John", last: "Doe", employeeNumber: 2345643)
6868

69-
let encodedData = try encoder.encode(employee)
69+
let bytes = try encoder.encode(employee)
7070
```
7171
As well as Complex types with sub classes.
7272

@@ -81,32 +81,19 @@ To create an instance of a BinaryDecoder:
8181
8282
To decode, you pass the Type of object to create, and an instance of encoded data representing that type.
8383
```Swift
84-
let employee = try decoder.decode(Employee.self, from: encodedData)
84+
let employee = try decoder.decode(Employee.self, from: bytes)
8585
```
8686

87-
### EncodedData
87+
### Encoded Data
8888

89-
The `BinaryEncoder.encode` method returns a type called `EncodedData` (likewise `BinaryDecoder.decode` accepts an `EncodedData` instance). This type is the direct connection between raw memory and a type that can be converted to and from a `Codable` object.
89+
The `BinaryEncoder.encode` method returns type `Array<UInt8>` (likewise `BinaryDecoder.decode` accepts an `Array<UInt8>` instance).
9090

91-
StickyEncoding uses this intermediate representation so that it can support many use cases from direct byte conversion to writing/reading directly to/from raw memory.
92-
93-
When encoding of an object, the intermediate representation has already been
94-
encoded down to a form that can be rapidly written to memory.
91+
`[Array<UInt8>` is easily converted to other types such as `Swift.Data` for passing to Foundation methods to store and load data from file.
9592
```Swift
96-
let encodedData = try encoder.encode(employee)
93+
let bytes = try encoder.encode(employee)
9794

9895
// Write the bytes directly to a file.
99-
FileManager.default.createFile(atPath: "employee.bin", contents: Data(encodedData))
100-
```
101-
There are use cases that require writing to a buffer or socket in which case StickyEncoding offers a direct write method so that an intermediate structure (byte array) does not have to be created first.
102-
```Swift
103-
let encodedData = try encoder.encode(employee)
104-
105-
// Allocate byte aligned raw memory to hold the binary encoded data.
106-
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: encodedData.byteCount, alignment: MemoryLayout<UInt8>.alignment)
107-
108-
// Write the encoded data directly to the raw memory.
109-
encodedData.write(to: buffer)
96+
FileManager.default.createFile(atPath: "employee.bin", contents: Data(bytes))
11097
```
11198

11299
## Sources and Binaries
@@ -126,7 +113,6 @@ You can find the latest sources and binaries on [github](https://github.com/stic
126113

127114
> Note: for a instructions on how to build and test StickyEncoding for contributing please see the [Contributing Guide](CONTRIBUTING.md).
128115
129-
130116
## Installation
131117

132118
### Swift Package Manager

Sources/Documentation/Abstracts/EncodedData Transformation.md

-5
This file was deleted.

Sources/Documentation/Abstracts/Encoding & Decoding.md

-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,3 @@ Encoding is done using a `BinaryEncoder` instance and will encode any `Encodable
66
Decoding is done using a `BinaryDecoder` instance and can decode any `Decodable` type that was previously encoded using the `BinaryEncoder`. Of course you can declare `Encodable` or `Decodable` conformance by using `Codable` as well.
77

88
StickyEncoding creates a compact binary format that represents the encoded object or data type. You can read more about the format in the document [Binary Format](Sources/Documentation/Sections/Binary&#32;Format.md).
9-
10-
To facilitate many use cases, StickyEncoding encodes the data to an instance of `EncodedData`. EncodedData contains a binary format suitable
11-
for writing directly to memory, disk, or into a byte array. Or in the case of decoding, the format facilitates rapid decoding to Swift instances.
Binary file not shown.

Sources/Documentation/Sections/Binary Format.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# Binary Format
22

3-
The internal binary representation of the encoded data consists of a series of **Elements** each containing a **Header** section followed by a **Container**. The Element is essentially a wrapper around a Container defining it's type and size in bytes. The format was designed to be as compact and efficient as possible at storing the simplest to the most complex structures that can be encoded.
3+
The internal binary representation of the encoded data consists of a series of **Elements** each containing a **Header** section followed by a **Container**. The Element is essentially a wrapper around a Container defining it's type. The format was designed to be as compact and efficient as possible at storing the simplest to the most complex structures that can be encoded.
44

55
![Binary Format Container Header](Binary-Format-Container-Header.png)
66

7-
The Header of each Element contains a 32 bit integer representing the container type followed by a 32 integer containing the total number of bytes stored in the Container section of this Element.
8-
97
## Containers
108
Each Container formats it's data based on the requirements of that container. There are 3 container types that can be stored corresponding to the `Encoder` and `Decoder` protocol return types for the functions `singleValueContainer()`, `unkeyedContainer()`, and `container(keyedBy:)`. An Element is created in response to each call to any one of these functions.
119

Loading

Sources/StickyEncoding/BinaryDecoder.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import Foundation
3131
///
3232
/// To decode, you pass the Type of object to create, and an instance of encoded data representing that type.
3333
/// ```
34-
/// let employee = try decoder.decode(Employee.self, from: encodedData)
34+
/// let employee = try decoder.decode(Employee.self, from: bytes)
3535
/// ```
3636
///
3737
open class BinaryDecoder {
@@ -60,9 +60,9 @@ open class BinaryDecoder {
6060
/// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not valid.
6161
/// - throws: An error if any value throws an error during decoding.
6262
///
63-
open func decode<T : Decodable>(_ type: T.Type, from encodedData: EncodedData) throws -> T {
63+
open func decode<T : Decodable>(_ type: T.Type, from bytes: [UInt8]) throws -> T {
6464

65-
let storage = try StorageContainerReader.read(from: encodedData.buffer)
65+
let storage = try StorageContainerReader.convert(bytes)
6666

6767
return try T.init(from: _BinaryDecoder(codingPath: [], rootStorage: storage, userInfo: self.userInfo))
6868
}

Sources/StickyEncoding/BinaryEncoder.swift

+6-11
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import Foundation
4545
/// ```
4646
/// let string = "You can encode single values of any type."
4747
///
48-
/// let encoded = try encoder.encode(string)
48+
/// let bytes = try encoder.encode(string)
4949
/// ```
5050
/// Basic structs and classes can also be encoded.
5151
/// ```
@@ -57,7 +57,7 @@ import Foundation
5757
///
5858
/// let employee = Employee(first: "John", last: "Doe", employeeNumber: 2345643)
5959
///
60-
/// let encodedData = try encoder.encode(employee)
60+
/// let bytes = try encoder.encode(employee)
6161
/// ```
6262
/// As well as Complex types with sub classes.
6363
///
@@ -84,20 +84,15 @@ open class BinaryEncoder {
8484
///
8585
/// - throws: An error if any value throws an error during encoding.
8686
///
87-
open func encode<T: Encodable>(_ value: T) throws -> EncodedData {
87+
open func encode<T: Encodable>(_ value: T) throws -> [UInt8] {
8888
let encoder = _BinaryEncoder(codingPath: [], userInfo: self.userInfo)
8989

9090
try value.encode(to: encoder)
9191

92-
if let storage = encoder.rootStorage.value {
93-
var bytes = Array<UInt8>(repeating: 0, count: StorageContainerWriter.byteCount(storage))
92+
guard let storage = encoder.rootStorage.value
93+
else { return [] }
9494

95-
bytes.withUnsafeMutableBytes { (buffer) -> Void in
96-
StorageContainerWriter.write(storage, to: buffer)
97-
}
98-
return EncodedData(bytes)
99-
}
100-
return EncodedData()
95+
return StorageContainerWriter.convert(storage)
10196
}
10297

10398
// MARK: Getting contextual information

0 commit comments

Comments
 (0)