Skip to content

Commit

Permalink
Updated to Swift 07-25 (#37)
Browse files Browse the repository at this point in the history
* Updated to Swift 07-25

* Debug
  • Loading branch information
czechboy0 authored Jul 26, 2016
1 parent 30f9aec commit ded5c7e
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DEVELOPMENT-SNAPSHOT-2016-06-06-a
DEVELOPMENT-SNAPSHOT-2016-07-25-a
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 9 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -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"])
]
)
name: "Redbird",
targets: [
Target(name: "Redbird"),
Target(name: "RedbirdExample", dependencies: ["Redbird"])
],
dependencies: [
.Package(url: "https://github.com/czechboy0/Socks.git", majorVersion: 0, minor: 10)
]
)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/Redbird/ClientSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Socks
import SocksCore

typealias SocketError = SocksCore.Error
typealias SocketError = SocksError

protocol Socket: class, SocketReader {
func write(string: String) throws
Expand Down
2 changes: 1 addition & 1 deletion Sources/Redbird/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 18 additions & 18 deletions Sources/Redbird/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 7 additions & 7 deletions Sources/Redbird/ExternalTypeConversions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ 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")
}
}

public func toMaybeArray() throws -> [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")
}
}

public func toString() throws -> String {
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")
}
}

Expand All @@ -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")
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Redbird/Formatters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Redbird/Integer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions Sources/Redbird/Parsers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Sources/Redbird/Redbird.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Redbird/RedbirdExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ extension Redbird {
case .SimpleString: if try ret.toString() == "OK" { return }
default: break
}
throw RedbirdError.UnexpectedReturnedObject(ret)
throw RedbirdError.unexpectedReturnedObject(ret)
}
}
2 changes: 1 addition & 1 deletion Sources/Redbird/SimpleString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ XCTMain([
testCase(ConversionTests.allTests),
testCase(FormattingTests.allTests),
testCase(ParsingTests.allTests),
testCase(PerformanceTests.allTests),
testCase(RedbirdTests.allTests),
testCase(StringTests.allTests)
])
4 changes: 2 additions & 2 deletions Tests/Redbird/ConversionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ConversionTests: XCTestCase {

func testArray_NullThrows() {
let orig = RespNullArray()
assertThrow(.WrongNativeTypeUnboxing(orig, "Array")) {
assertThrow(.wrongNativeTypeUnboxing(orig, "Array")) {
_ = try orig.toArray()
}
}
Expand Down Expand Up @@ -61,7 +61,7 @@ class ConversionTests: XCTestCase {

func testString_NullThrows() {
let orig = RespNullBulkString()
assertThrow(.WrongNativeTypeUnboxing(orig, "String")) {
assertThrow(.wrongNativeTypeUnboxing(orig, "String")) {
_ = try orig.toString()
}
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/Redbird/ParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 11 additions & 20 deletions Tests/Redbird/PerformanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -57,6 +50,4 @@ class PerformanceTests: XCTestCase {
_ = try! InitialFormatter().format(input)
}
}

}
#endif

0 comments on commit ded5c7e

Please sign in to comment.