-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more complete documentation. (#7)
* Add Binary Format diagrams and md file. * Update header docs with examples. * Add 'Overview' section describing usage. * Adding tests for documentation examples.
- Loading branch information
Showing
12 changed files
with
294 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,48 @@ | ||
### Binary Format | ||
# Binary Format | ||
|
||
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. | ||
|
||
![Binary Format Container Header](../Binary-Format-Container-Header.png) | ||
|
||
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. | ||
|
||
## Containers | ||
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. | ||
|
||
### Single Value Container | ||
|
||
Each SingleValueContainer is defined by the Container structure below. | ||
SingleValueContainers consist of the encoded type of the value it stores, the size in bytes of the value, and the value itself. | ||
|
||
![Binary Format Single Value Container](../Binary-Format-Single-Value-Container.png) | ||
|
||
### Unkeyed Container | ||
|
||
An unkeyed container consists of the count of the number of elements it stores and a series of the elements | ||
containing other containers. | ||
|
||
![Binary Format](../Binary-Format-Unkeyed-Container.png) | ||
|
||
|
||
### Keyed Container | ||
|
||
A keyed container consists of the count of the number of key/Element pairs contained in the container followed by a key and Element for each. | ||
|
||
![Binary Format](../Binary-Format-Keyed-Container.png) | ||
|
||
Each key consists of the size in bytes of the string for the key and the value of the string itself. | ||
|
||
> Note: Keys are not null terminated. | ||
## Memory Alignment | ||
|
||
Currently StickyEncoding aligns the data in the file and in memory aligned to the machine it is running on. | ||
|
||
## Byte ordering | ||
|
||
StickyEncoding uses the same byte order as the machine that the binary data is created on uses. | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
Tests/StickyEncodingTests/DocumentationExampleTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/// | ||
/// DocumentationExampleTests.swift | ||
/// | ||
/// Copyright 2019 Tony Stone | ||
/// | ||
/// Licensed under the Apache License, Version 2.0 (the "License"); | ||
/// you may not use this file except in compliance with the License. | ||
/// You may obtain a copy of the License at | ||
/// | ||
/// http://www.apache.org/licenses/LICENSE-2.0 | ||
/// | ||
/// Unless required by applicable law or agreed to in writing, software | ||
/// distributed under the License is distributed on an "AS IS" BASIS, | ||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
/// See the License for the specific language governing permissions and | ||
/// limitations under the License. | ||
/// | ||
/// Created by Tony Stone on 2/13/19. | ||
/// | ||
import XCTest | ||
|
||
import StickyEncoding | ||
|
||
class DocumentationExampleTests: XCTestCase { | ||
|
||
let encoder = BinaryEncoder() | ||
let decoder = BinaryDecoder() | ||
|
||
/// Documentation Examples | ||
/// | ||
/// These test should represent the documentations examples exactly | ||
/// with no XCTAsserts added. These tests are to make sure that the | ||
/// examples compile and run. | ||
/// | ||
|
||
func testBinaryEncoderExample1() throws { | ||
|
||
let string = "You can encode single values of any type." | ||
|
||
let encoded = try encoder.encode(string) | ||
} | ||
|
||
func testBinaryEncoderExample2() throws { | ||
|
||
/// Actual code in example. | ||
/// | ||
struct Employee: Codable { | ||
let first: String | ||
let last: String | ||
let employeeNumber: Int | ||
} | ||
|
||
let employee = Employee(first: "John", last: "Doe", employeeNumber: 2345643) | ||
|
||
let encodedData = try encoder.encode(employee) | ||
} | ||
|
||
func testBinaryDecoderExample1() throws { | ||
|
||
struct Employee: Codable { | ||
let first: String | ||
let last: String | ||
let employeeNumber: Int | ||
} | ||
|
||
let encodedData = try encoder.encode(Employee(first: "John", last: "Doe", employeeNumber: 2345643)) | ||
|
||
/// Actual code in example. | ||
/// | ||
let employee = try decoder.decode(Employee.self, from: encodedData) | ||
} | ||
|
||
func testEncodedDataExample1() throws { | ||
|
||
/// Code from previous example used as boiler plate for this example. | ||
/// | ||
struct Employee: Codable { | ||
let first: String | ||
let last: String | ||
let employeeNumber: Int | ||
} | ||
|
||
let employee = Employee(first: "John", last: "Doe", employeeNumber: 2345643) | ||
|
||
/// Actual code in example. | ||
/// | ||
let encodedData = try encoder.encode(employee) | ||
|
||
FileManager.default.createFile(atPath: "employee.bin", contents: Data(bytes: encodedData.bytes)) | ||
} | ||
|
||
func testEncodedDataExample2() throws { | ||
|
||
/// Code from previous example used as boiler plate for this example. | ||
/// | ||
struct Employee: Codable { | ||
let first: String | ||
let last: String | ||
let employeeNumber: Int | ||
} | ||
|
||
let employee = Employee(first: "John", last: "Doe", employeeNumber: 2345643) | ||
|
||
/// Actual code in example. | ||
/// | ||
let encodedData = try encoder.encode(employee) | ||
|
||
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: encodedData.byteCount, alignment: MemoryLayout<UInt8>.alignment) | ||
|
||
encodedData.write(to: buffer) | ||
} | ||
} |