diff --git a/.swift-version b/.swift-version index bba74a1..fbe576f 100644 --- a/.swift-version +++ b/.swift-version @@ -1 +1 @@ -DEVELOPMENT-SNAPSHOT-2016-06-06-a +DEVELOPMENT-SNAPSHOT-2016-07-25-a diff --git a/.travis.yml b/.travis.yml index 49c61fc..4443e27 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ os: language: generic sudo: required dist: trusty -osx_image: xcode7.3 +osx_image: xcode8 install: - bash TestRedis/install_redis_local.sh - eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/02090c7ede5a637b76e6df1710e83cd0bbe7dcdf/swiftenv-install.sh)" diff --git a/Makefile b/Makefile index c5ec881..1cccdb9 100644 --- a/Makefile +++ b/Makefile @@ -13,9 +13,9 @@ build-release: test: redis @swift test -example: redis build-release +example: redis build @echo "Running example client" - .build/release/RedbirdExample + .build/debug/RedbirdExample validate_spec: @echo "Validating podspec" diff --git a/Package.swift b/Package.swift index b66dd9a..cfca4c5 100644 --- a/Package.swift +++ b/Package.swift @@ -1,12 +1,12 @@ import PackageDescription let package = Package( - name: "Redbird", - dependencies: [ - .Package(url: "https://github.com/czechboy0/Socks.git", majorVersion: 0, minor: 8) - ], - targets: [ - Target(name: "Redbird"), - Target(name: "RedbirdExample", dependencies: ["Redbird"]) - ] -) \ No newline at end of file + name: "Redbird", + targets: [ + Target(name: "Redbird"), + Target(name: "RedbirdExample", dependencies: ["Redbird"]) + ], + dependencies: [ + .Package(url: "https://github.com/czechboy0/Socks.git", majorVersion: 0, minor: 10) + ] +) diff --git a/README.md b/README.md index df21538..a729c5d 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ That means I'm writing it up all the way from bare TCP sockets. Just using `Glib ## Swift Package Manager ```swift -.Package(url: "https://github.com/czechboy0/Redbird.git", majorVersion: 0, minor: 7) +.Package(url: "https://github.com/czechboy0/Redbird.git", majorVersion: 0, minor: 9) ``` # Usage diff --git a/Sources/Redbird/ClientSocket.swift b/Sources/Redbird/ClientSocket.swift index 6b351d8..8c99154 100644 --- a/Sources/Redbird/ClientSocket.swift +++ b/Sources/Redbird/ClientSocket.swift @@ -9,7 +9,7 @@ import Socks import SocksCore -typealias SocketError = SocksCore.Error +typealias SocketError = SocksError protocol Socket: class, SocketReader { func write(string: String) throws diff --git a/Sources/Redbird/Error.swift b/Sources/Redbird/Error.swift index 04da2bc..411e27b 100644 --- a/Sources/Redbird/Error.swift +++ b/Sources/Redbird/Error.swift @@ -6,7 +6,7 @@ // Copyright © 2016 Honza Dvorsky. All rights reserved. // -public struct RespError: RespObject, ErrorProtocol { +public struct RespError: RespObject, Error { static var signature: String = "-" public let respType: RespType = .Error diff --git a/Sources/Redbird/Errors.swift b/Sources/Redbird/Errors.swift index 31bc3f5..1dd6c70 100644 --- a/Sources/Redbird/Errors.swift +++ b/Sources/Redbird/Errors.swift @@ -6,22 +6,22 @@ // Copyright © 2016 Honza Dvorsky. All rights reserved. // -public enum RedbirdError: ErrorProtocol { - case ParsingGeneric(String) - case ParsingStringNotThisType(String, RespType?) - case SimpleStringInvalidInput(String) - case IntegerInvalidInput(String) - case FormatterNotForThisType(RespObject, RespType?) - case ReceivedStringNotTerminatedByRespTerminator(String) - case StringNotConvertibleToByte(String) - case NoDataFromSocket - case NotEnoughCharactersToReadFromSocket(Int, [Byte]) - case BulkStringProvidedUnparseableByteCount(String) - case ArrayProvidedUnparseableCount(String) - case NoFormatterFoundForObject(RespObject) - case MoreThanOneWordSpecifiedAsCommand(String) - case WrongNativeTypeUnboxing(RespObject, String) - case UnexpectedReturnedObject(RespObject) - case PipelineNoCommandProvided - case FailedToCreateSocket(ErrorProtocol) +public enum RedbirdError: Error { + case parsingGeneric(String) + case parsingStringNotThisType(String, RespType?) + case simpleStringInvalidInput(String) + case integerInvalidInput(String) + case formatterNotForThisType(RespObject, RespType?) + case receivedStringNotTerminatedByRespTerminator(String) + case stringNotConvertibleToByte(String) + case noDataFromSocket + case notEnoughCharactersToReadFromSocket(Int, [Byte]) + case bulkStringProvidedUnparseableByteCount(String) + case arrayProvidedUnparseableCount(String) + case noFormatterFoundForObject(RespObject) + case moreThanOneWordSpecifiedAsCommand(String) + case wrongNativeTypeUnboxing(RespObject, String) + case unexpectedReturnedObject(RespObject) + case pipelineNoCommandProvided + case failedToCreateSocket(Error) } diff --git a/Sources/Redbird/ExternalTypeConversions.swift b/Sources/Redbird/ExternalTypeConversions.swift index 134a845..90ac26f 100644 --- a/Sources/Redbird/ExternalTypeConversions.swift +++ b/Sources/Redbird/ExternalTypeConversions.swift @@ -11,7 +11,7 @@ extension RespObject { public func toArray() throws -> [RespObject] { switch self.respType { case .Array: return (self as! RespArray).content - default: throw RedbirdError.WrongNativeTypeUnboxing(self, "Array") + default: throw RedbirdError.wrongNativeTypeUnboxing(self, "Array") } } @@ -19,7 +19,7 @@ extension RespObject { switch self.respType { case .Array: return (self as! RespArray).content case .NullArray: return nil - default: throw RedbirdError.WrongNativeTypeUnboxing(self, "MaybeArray") + default: throw RedbirdError.wrongNativeTypeUnboxing(self, "MaybeArray") } } @@ -27,7 +27,7 @@ extension RespObject { switch self.respType { case .SimpleString: return (self as! RespSimpleString).content case .BulkString: return (self as! RespBulkString).content - default: throw RedbirdError.WrongNativeTypeUnboxing(self, "String") + default: throw RedbirdError.wrongNativeTypeUnboxing(self, "String") } } @@ -36,28 +36,28 @@ extension RespObject { case .SimpleString: return (self as! RespSimpleString).content case .BulkString: return (self as! RespBulkString).content case .NullBulkString: return nil - default: throw RedbirdError.WrongNativeTypeUnboxing(self, "MaybeString") + default: throw RedbirdError.wrongNativeTypeUnboxing(self, "MaybeString") } } public func toInt() throws -> Int { switch self.respType { case .Integer: return Int((self as! RespInteger).intContent) - default: throw RedbirdError.WrongNativeTypeUnboxing(self, "Int") + default: throw RedbirdError.wrongNativeTypeUnboxing(self, "Int") } } public func toBool() throws -> Bool { switch self.respType { case .Integer: return (self as! RespInteger).boolContent - default: throw RedbirdError.WrongNativeTypeUnboxing(self, "Bool") + default: throw RedbirdError.wrongNativeTypeUnboxing(self, "Bool") } } public func toError() throws -> RespError { switch self.respType { case .Error: return (self as! RespError) - default: throw RedbirdError.WrongNativeTypeUnboxing(self, "Error") + default: throw RedbirdError.wrongNativeTypeUnboxing(self, "Error") } } diff --git a/Sources/Redbird/Formatters.swift b/Sources/Redbird/Formatters.swift index 1dc4eb8..c7fd8d7 100644 --- a/Sources/Redbird/Formatters.swift +++ b/Sources/Redbird/Formatters.swift @@ -26,7 +26,7 @@ struct InitialFormatter: Formatter { //find the appropriate formatter for this type guard let formatter = formatters[object.respType] else { - throw RedbirdError.NoFormatterFoundForObject(object) + throw RedbirdError.noFormatterFoundForObject(object) } let formatted = try formatter.format(object) @@ -114,7 +114,7 @@ struct ArrayFormatter: Formatter { .wrappedInitialSignatureAndTrailingTerminator(RespArray.signature) let suffix = try content .map { try InitialFormatter().format($0) } - .reduce("", combine: +) + .reduce("", +) let str = prefix + suffix return str } diff --git a/Sources/Redbird/Integer.swift b/Sources/Redbird/Integer.swift index ed3a3bc..0aa2fee 100644 --- a/Sources/Redbird/Integer.swift +++ b/Sources/Redbird/Integer.swift @@ -16,7 +16,7 @@ public struct RespInteger: RespObject { init(content: String) throws { guard let intContent = Int64(content) else { - throw RedbirdError.SimpleStringInvalidInput(content) + throw RedbirdError.simpleStringInvalidInput(content) } self.intContent = intContent } diff --git a/Sources/Redbird/Parsers.swift b/Sources/Redbird/Parsers.swift index 85f889a..0c068ba 100644 --- a/Sources/Redbird/Parsers.swift +++ b/Sources/Redbird/Parsers.swift @@ -26,10 +26,10 @@ extension Parser { let leftToRead = min - alreadyRead.count let readChars = try reader.read(bytes: leftToRead) guard readChars.count > 0 else { - throw RedbirdError.NoDataFromSocket + throw RedbirdError.noDataFromSocket } guard readChars.count == leftToRead else { - throw RedbirdError.NotEnoughCharactersToReadFromSocket(leftToRead, alreadyRead) + throw RedbirdError.notEnoughCharactersToReadFromSocket(leftToRead, alreadyRead) } return alreadyRead + readChars } @@ -54,7 +54,7 @@ struct InitialParser: Parser { case RespBulkString.signature: parser = BulkStringParser() case RespArray.signature: parser = ArrayParser() default: - throw RedbirdError.ParsingStringNotThisType(try alreadyRead.stringView(), nil) + throw RedbirdError.parsingStringNotThisType(try alreadyRead.stringView(), nil) } return try parser.parse(read, reader: reader) @@ -109,12 +109,12 @@ struct BulkStringParser: Parser { let (head, maybeTail) = try reader.readUntilDelimiter(alreadyRead: alreadyRead, delimiter: RespTerminator) guard let tail = maybeTail else { let readSoFar = try head.stringView() - throw RedbirdError.ReceivedStringNotTerminatedByRespTerminator(readSoFar) + throw RedbirdError.receivedStringNotTerminatedByRespTerminator(readSoFar) } let rawByteCountString = try head.stringView() let byteCountString = rawByteCountString.strippedInitialSignatureAndTrailingTerminator() guard let byteCount = Int(byteCountString) else { - throw RedbirdError.BulkStringProvidedUnparseableByteCount(byteCountString) + throw RedbirdError.bulkStringProvidedUnparseableByteCount(byteCountString) } //if byte count is -1, then return a null string @@ -161,12 +161,12 @@ struct ArrayParser: Parser { let (head, maybeTail) = try reader.readUntilDelimiter(alreadyRead: alreadyRead, delimiter: RespTerminator) guard let tail = maybeTail else { let readSoFar = try head.stringView() - throw RedbirdError.ReceivedStringNotTerminatedByRespTerminator(readSoFar) + throw RedbirdError.receivedStringNotTerminatedByRespTerminator(readSoFar) } let rawCountString = try head.stringView() let countString = rawCountString.strippedInitialSignatureAndTrailingTerminator() guard let count = Int(countString) else { - throw RedbirdError.ArrayProvidedUnparseableCount(countString) + throw RedbirdError.arrayProvidedUnparseableCount(countString) } //if byte count is -1, then return a null array diff --git a/Sources/Redbird/Redbird.swift b/Sources/Redbird/Redbird.swift index 6a55c5a..867aa8e 100644 --- a/Sources/Redbird/Redbird.swift +++ b/Sources/Redbird/Redbird.swift @@ -57,7 +57,7 @@ public class Redbird { //make sure nobody passed params in the command name //TODO: will become obsolete once we change name to an enum value guard name.subwords().count == 1 else { - throw RedbirdError.MoreThanOneWordSpecifiedAsCommand(name) + throw RedbirdError.moreThanOneWordSpecifiedAsCommand(name) } //format the outgoing command into a Resp string @@ -72,7 +72,7 @@ public class Redbird { } catch { var retry = false - if case RedbirdError.NoDataFromSocket = error { + if case RedbirdError.noDataFromSocket = error { retry = true } if let e = error as? SocketError { @@ -147,9 +147,9 @@ public class Pipeline: Redbird { @discardableResult public func execute() throws -> [RespObject] { guard self.commands.count > 0 else { - throw RedbirdError.PipelineNoCommandProvided + throw RedbirdError.pipelineNoCommandProvided } - let formatted = self.commands.reduce("", combine: +) + let formatted = self.commands.reduce("", +) var ret: [RespObject]? try self.handleComms { diff --git a/Sources/Redbird/RedbirdExtensions.swift b/Sources/Redbird/RedbirdExtensions.swift index 4122e79..6c4e26d 100644 --- a/Sources/Redbird/RedbirdExtensions.swift +++ b/Sources/Redbird/RedbirdExtensions.swift @@ -17,6 +17,6 @@ extension Redbird { case .SimpleString: if try ret.toString() == "OK" { return } default: break } - throw RedbirdError.UnexpectedReturnedObject(ret) + throw RedbirdError.unexpectedReturnedObject(ret) } } diff --git a/Sources/Redbird/SimpleString.swift b/Sources/Redbird/SimpleString.swift index f0f28a8..56b15af 100644 --- a/Sources/Redbird/SimpleString.swift +++ b/Sources/Redbird/SimpleString.swift @@ -14,7 +14,7 @@ public struct RespSimpleString: RespObject { init(content: String) throws { if content.contains(character: "\r") || content.contains(character: "\n") { - throw RedbirdError.SimpleStringInvalidInput(content) + throw RedbirdError.simpleStringInvalidInput(content) } self.content = content } diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 0a4953f..d9f77db 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -5,6 +5,7 @@ XCTMain([ testCase(ConversionTests.allTests), testCase(FormattingTests.allTests), testCase(ParsingTests.allTests), + testCase(PerformanceTests.allTests), testCase(RedbirdTests.allTests), testCase(StringTests.allTests) ]) diff --git a/Tests/Redbird/ConversionTests.swift b/Tests/Redbird/ConversionTests.swift index af44a35..fb871d6 100644 --- a/Tests/Redbird/ConversionTests.swift +++ b/Tests/Redbird/ConversionTests.swift @@ -21,7 +21,7 @@ class ConversionTests: XCTestCase { func testArray_NullThrows() { let orig = RespNullArray() - assertThrow(.WrongNativeTypeUnboxing(orig, "Array")) { + assertThrow(.wrongNativeTypeUnboxing(orig, "Array")) { _ = try orig.toArray() } } @@ -61,7 +61,7 @@ class ConversionTests: XCTestCase { func testString_NullThrows() { let orig = RespNullBulkString() - assertThrow(.WrongNativeTypeUnboxing(orig, "String")) { + assertThrow(.wrongNativeTypeUnboxing(orig, "String")) { _ = try orig.toString() } } diff --git a/Tests/Redbird/ParsingTests.swift b/Tests/Redbird/ParsingTests.swift index 1b28b03..b689004 100644 --- a/Tests/Redbird/ParsingTests.swift +++ b/Tests/Redbird/ParsingTests.swift @@ -17,6 +17,10 @@ class TestReader: SocketReader { self.content = content.byteArrayView() } + init(bytes: [Byte]) { + self.content = bytes + } + func read(bytes: Int) throws -> [Byte] { precondition(bytes > 0) diff --git a/Tests/Redbird/PerformanceTests.swift b/Tests/Redbird/PerformanceTests.swift index c67c0cf..08ef342 100644 --- a/Tests/Redbird/PerformanceTests.swift +++ b/Tests/Redbird/PerformanceTests.swift @@ -7,37 +7,30 @@ // import XCTest +import Foundation @testable import Redbird -//#if os(Linux) -// extension PerformanceTests: XCTestCaseProvider { -// var allTests : [(String, () throws -> Void)] { -// return [ -// ("testPerf_ParsingArray_Normal", testPerf_ParsingArray_Normal), -// ("testPerf_LargeArray", testPerf_LargeArray) -// ] -// } -// } -//#endif +extension PerformanceTests { + static var allTests = [ + ("testPerf_ParsingArray_Normal", testPerf_ParsingArray_Normal), + ("testPerf_LargeArray", testPerf_LargeArray) + ] +} -#if os(Linux) -#else class PerformanceTests: XCTestCase { - func urlForFixture(name: String) -> NSURL { - + func urlForFixture(name: String) -> URL { let parent = (#file).components(separatedBy: "/").dropLast().joined(separator: "/") - let url = NSURL(string: "file://\(parent)/\(name).txt")! + let url = URL(string: "file://\(parent)/\(name).txt")! print("Loading fixture from url \(url)") return url } func testPerf_ParsingArray_Normal() { - let strUrl = urlForFixture(name: "teststring") - let str = try! String(contentsOf: strUrl, encoding: NSUTF8StringEncoding) + let bytes = Array(try! Data(contentsOf: strUrl)) measure { - let reader = TestReader(content: str) + let reader = TestReader(bytes: bytes) let (_, _) = try! InitialParser().parse([], reader: reader) } } @@ -57,6 +50,4 @@ class PerformanceTests: XCTestCase { _ = try! InitialFormatter().format(input) } } - } -#endif