mirror of
https://github.com/Dev1an/MsgPack.git
synced 2026-03-25 08:45:55 +00:00
Add readme
This commit is contained in:
parent
37b1d55cea
commit
e9907a8c50
6 changed files with 62 additions and 26 deletions
|
|
@ -8,7 +8,20 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
class Encoder: Swift.Encoder {
|
||||
public class Encoder {
|
||||
let serialiser = Serialiser()
|
||||
|
||||
public init() {}
|
||||
|
||||
public func encode<T : Encodable>(_ value: T) throws -> Data {
|
||||
try value.encode(to: serialiser)
|
||||
var data = Data()
|
||||
serialiser.storage?.appendTo(data: &data)
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
class Serialiser: Swift.Encoder {
|
||||
var codingPath = [CodingKey]()
|
||||
var userInfo = [CodingUserInfoKey : Any]()
|
||||
|
||||
|
|
@ -25,16 +38,9 @@ class Encoder: Swift.Encoder {
|
|||
func singleValueContainer() -> SingleValueEncodingContainer {
|
||||
return self
|
||||
}
|
||||
|
||||
public func dataFor<T : Encodable>(_ value: T) throws -> Data {
|
||||
try value.encode(to: self)
|
||||
var data = Data()
|
||||
storage?.appendTo(data: &data)
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
extension MsgPack.Encoder: SingleValueEncodingContainer {
|
||||
extension MsgPack.Serialiser: SingleValueEncodingContainer {
|
||||
enum Error: Swift.Error {
|
||||
case notImplemented
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ class MsgPackTests: XCTestCase {
|
|||
|
||||
func testEncodeTrue() {
|
||||
do {
|
||||
let data = try encoder.dataFor(true)
|
||||
let data = try encoder.encode(true)
|
||||
XCTAssertEqual(data[0], 0xc3)
|
||||
XCTAssertEqual(data.count, 1, "Encoded data should contain only one byte")
|
||||
} catch {
|
||||
|
|
@ -36,7 +36,7 @@ class MsgPackTests: XCTestCase {
|
|||
|
||||
func testEncodeFalse() {
|
||||
do {
|
||||
let data = try encoder.dataFor(false)
|
||||
let data = try encoder.encode(false)
|
||||
XCTAssertEqual(data.count, 1, "Encoded data should contain only one byte, but contains \(data.count)")
|
||||
XCTAssertEqual(data[0], 0xc2)
|
||||
} catch {
|
||||
|
|
@ -47,7 +47,7 @@ class MsgPackTests: XCTestCase {
|
|||
func testEncodeUInt8() {
|
||||
do {
|
||||
let number: UInt8 = 5
|
||||
let data = try encoder.dataFor(number)
|
||||
let data = try encoder.encode(number)
|
||||
XCTAssertEqual(data.count, 2, "Encoded data should contain exactly two bytes, but contains \(data.count)")
|
||||
XCTAssertEqual(data[0], 0xCC)
|
||||
XCTAssertEqual(data[1], number)
|
||||
|
|
@ -58,7 +58,7 @@ class MsgPackTests: XCTestCase {
|
|||
|
||||
func testEncodeUInt16() {
|
||||
do {
|
||||
let data = try encoder.dataFor(UInt16(0x0506))
|
||||
let data = try encoder.encode(UInt16(0x0506))
|
||||
XCTAssertEqual(data.count, 3, "Encoded data should contain exactly three bytes, but contains \(data.count)")
|
||||
XCTAssertEqual(data[0], 0xCD)
|
||||
XCTAssertEqual(data[1], 0x05)
|
||||
|
|
@ -70,7 +70,7 @@ class MsgPackTests: XCTestCase {
|
|||
|
||||
func testEncodeUInt32() {
|
||||
do {
|
||||
let data = try encoder.dataFor(UInt32(0x05060708))
|
||||
let data = try encoder.encode(UInt32(0x05060708))
|
||||
XCTAssertEqual(data.count, 5, "Encoded data should contain exactly five bytes, but contains \(data.count)")
|
||||
XCTAssertEqual(data[0], 0xCE)
|
||||
XCTAssertEqual(data[1], 0x05)
|
||||
|
|
@ -84,7 +84,7 @@ class MsgPackTests: XCTestCase {
|
|||
|
||||
func testEncodeUInt64() {
|
||||
do {
|
||||
let data = try encoder.dataFor(UInt64(0x0506070809101112))
|
||||
let data = try encoder.encode(UInt64(0x0506070809101112))
|
||||
XCTAssertEqual(data.count, 9, "Encoded data should contain exactly nine bytes, but contains \(data.count)")
|
||||
XCTAssertEqual(data[0], 0xCF)
|
||||
XCTAssertEqual(data[1], 0x05)
|
||||
|
|
@ -103,9 +103,10 @@ class MsgPackTests: XCTestCase {
|
|||
func testPerformanceOf1MilionUInt32Encodings() {
|
||||
var d = Data()
|
||||
self.measure {
|
||||
for _ in 0 ... 1000000 {
|
||||
for _ in 0 ..< 1000000 {
|
||||
Format.uInt32(136315908).appendTo(data: &d)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
9
Playground.playground/Contents.swift
Normal file
9
Playground.playground/Contents.swift
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
//: Playground - noun: a place where people can play
|
||||
|
||||
import MsgPack
|
||||
|
||||
var number: UInt16 = 0x0906
|
||||
|
||||
let data = try Encoder().encode(number)
|
||||
data[1]
|
||||
data[2]
|
||||
4
Playground.playground/contents.xcplayground
Normal file
4
Playground.playground/contents.xcplayground
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='macos'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
||||
3
README.md
Normal file
3
README.md
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# Usage
|
||||
|
||||
Take a look at the [playground](Playground.playground/Contents.swift)
|
||||
13
Workspace.xcworkspace/contents.xcworkspacedata
generated
Normal file
13
Workspace.xcworkspace/contents.xcworkspacedata
generated
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Workspace
|
||||
version = "1.0">
|
||||
<FileRef
|
||||
location = "group:README.md">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:Playground.playground">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "container:MsgPack.xcodeproj">
|
||||
</FileRef>
|
||||
</Workspace>
|
||||
Loading…
Reference in a new issue