diff --git a/MsgPack/Encoder.swift b/MsgPack/Encoder.swift index 29df41f..5c76007 100644 --- a/MsgPack/Encoder.swift +++ b/MsgPack/Encoder.swift @@ -8,7 +8,20 @@ import Foundation -class Encoder: Swift.Encoder { +public class Encoder { + let serialiser = Serialiser() + + public init() {} + + public func encode(_ 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(_ 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 } diff --git a/MsgPackTests/MsgPackTests.swift b/MsgPackTests/MsgPackTests.swift index a1fbf16..0716113 100644 --- a/MsgPackTests/MsgPackTests.swift +++ b/MsgPackTests/MsgPackTests.swift @@ -10,23 +10,23 @@ import XCTest @testable import MsgPack class MsgPackTests: XCTestCase { - + var encoder: MsgPack.Encoder! - override func setUp() { - super.setUp() - + override func setUp() { + super.setUp() + encoder = Encoder() - } - -// override func tearDown() { -// // Put teardown code here. This method is called after the invocation of each test method in the class. -// super.tearDown() -// } + } + + // override func tearDown() { + // // Put teardown code here. This method is called after the invocation of each test method in the class. + // super.tearDown() + // } 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) } } } } + diff --git a/Playground.playground/Contents.swift b/Playground.playground/Contents.swift new file mode 100644 index 0000000..cbd6840 --- /dev/null +++ b/Playground.playground/Contents.swift @@ -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] diff --git a/Playground.playground/contents.xcplayground b/Playground.playground/contents.xcplayground new file mode 100644 index 0000000..63b6dd8 --- /dev/null +++ b/Playground.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f5b9b15 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Usage + +Take a look at the [playground](Playground.playground/Contents.swift) diff --git a/Workspace.xcworkspace/contents.xcworkspacedata b/Workspace.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..95b0188 --- /dev/null +++ b/Workspace.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,13 @@ + + + + + + + + +