Skip to content

Commit 4cbb84b

Browse files
authored
Merge pull request #8 from Mordil/proposal-update
Update code to match current proposal
2 parents c80b8e7 + 90c3acb commit 4cbb84b

File tree

7 files changed

+70
-102
lines changed

7 files changed

+70
-102
lines changed

Sources/DispatchRedis/RedisPipeline.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public final class RedisPipeline {
3232
/// - arguments: The arguments, if any, to send with the command.
3333
/// - Returns: A self-reference to this `RedisPipeline` instance for chaining commands.
3434
@discardableResult
35-
public func enqueue(command: String, arguments: [RESPConvertible] = []) throws -> RedisPipeline {
35+
public func enqueue(command: String, arguments: [RESPValueConvertible] = []) throws -> RedisPipeline {
3636
try _driverPipeline.enqueue(command: command, arguments: arguments)
3737
return self
3838
}

Sources/NIORedis/ChannelHandlers/RedisCommandHandler.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ public struct RedisCommandContext {
66
/// A full command keyword and arguments stored as a single `RESPValue`.
77
public let command: RESPValue
88
/// A promise expected to be fulfilled with the `RESPValue` response to the command from Redis.
9-
public let promise: EventLoopPromise<RESPValue>
9+
public let responsePromise: EventLoopPromise<RESPValue>
1010

1111
public init(command: RESPValue, promise: EventLoopPromise<RESPValue>) {
1212
self.command = command
13-
self.promise = promise
13+
self.responsePromise = promise
1414
}
1515
}
1616

@@ -77,7 +77,7 @@ extension RedisCommandHandler: ChannelOutboundHandler {
7777
/// See `ChannelOutboundHandler.write(ctx:data:promise:)`
7878
public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
7979
let context = unwrapOutboundIn(data)
80-
commandResponseQueue.insert(context.promise, at: 0)
80+
commandResponseQueue.insert(context.responsePromise, at: 0)
8181
ctx.write(wrapOutboundOut(context.command), promise: promise)
8282
}
8383
}
Lines changed: 59 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,111 @@
11
import Foundation
22

33
/// Capable of converting to / from `RESPValue`.
4-
public protocol RESPConvertible {
5-
/// Create an instance of `Self` from `RESPValue`.
6-
static func convertFromRESP(_ value: RESPValue) throws -> Self
4+
public protocol RESPValueConvertible {
5+
init?(_ value: RESPValue)
76

8-
/// Convert self to `RESPValue`.
9-
func convertToRESP() throws -> RESPValue
7+
/// Creates a `RESPValue` representation.
8+
func convertedToRESPValue() -> RESPValue
109
}
1110

12-
extension RESPValue: RESPConvertible {
13-
/// See `RESPValueConvertible`.
14-
public func convertToRESP() throws -> RESPValue {
15-
return self
11+
extension RESPValue: RESPValueConvertible {
12+
public init?(_ value: RESPValue) {
13+
self = value
1614
}
1715

18-
/// See `RESPValueConvertible`.
19-
public static func convertFromRESP(_ value: RESPValue) throws -> RESPValue {
20-
return value
16+
/// See `RESPValueConvertible`
17+
public func convertedToRESPValue() -> RESPValue {
18+
return self
2119
}
2220
}
2321

24-
extension String: RESPConvertible {
25-
/// See `RESPValueConvertible`.
26-
public static func convertFromRESP(_ value: RESPValue) throws -> String {
27-
guard let string = value.string else {
28-
throw RedisError(identifier: "string", reason: "Could not convert to string: \(value).")
29-
}
30-
return string
22+
extension String: RESPValueConvertible {
23+
public init?(_ value: RESPValue) {
24+
guard let string = value.string else { return nil }
25+
self = string
3126
}
3227

3328
/// See `RESPValueConvertible`.
34-
public func convertToRESP() throws -> RESPValue {
29+
public func convertedToRESPValue() -> RESPValue {
3530
return .bulkString(Data(self.utf8))
3631
}
3732
}
3833

3934
extension FixedWidthInteger {
40-
/// See `RESPValueConvertible`.
41-
public static func convertFromRESP(_ value: RESPValue) throws -> Self {
42-
guard let int = value.int else {
43-
guard let string = value.string else {
44-
throw RedisError(identifier: "string", reason: "Could not convert to string: \(value)")
45-
}
46-
47-
guard let int = Self(string) else {
48-
throw RedisError(identifier: "int", reason: "Could not convert to int: \(value)")
49-
}
50-
51-
return int
35+
public init?(_ value: RESPValue) {
36+
if let int = value.int {
37+
self = Self(int)
38+
} else {
39+
guard let string = value.string else { return nil }
40+
guard let int = Self(string) else { return nil }
41+
self = Self(int)
5242
}
53-
54-
return Self(int)
5543
}
5644

57-
/// See `RESPValueConvertible`.
58-
public func convertToRESP() throws -> RESPValue {
45+
/// See `convertedToRESP`.
46+
public func convertedToRESPValue() -> RESPValue {
5947
return .bulkString(Data(self.description.utf8))
6048
}
6149
}
6250

63-
extension Int: RESPConvertible {}
64-
extension Int8: RESPConvertible {}
65-
extension Int16: RESPConvertible {}
66-
extension Int32: RESPConvertible {}
67-
extension Int64: RESPConvertible {}
68-
extension UInt: RESPConvertible {}
69-
extension UInt8: RESPConvertible {}
70-
extension UInt16: RESPConvertible {}
71-
extension UInt32: RESPConvertible {}
72-
extension UInt64: RESPConvertible {}
73-
74-
extension Double: RESPConvertible {
75-
/// See `RESPValueConvertible`.
76-
public static func convertFromRESP(_ value: RESPValue) throws -> Double {
77-
guard let string = value.string else {
78-
throw RedisError(identifier: "string", reason: "Could not convert to string: \(value).")
79-
}
80-
81-
guard let float = Double(string) else {
82-
throw RedisError(identifier: "double", reason: "Could not convert to double: \(value).")
83-
}
84-
85-
return float
51+
extension Int: RESPValueConvertible {}
52+
extension Int8: RESPValueConvertible {}
53+
extension Int16: RESPValueConvertible {}
54+
extension Int32: RESPValueConvertible {}
55+
extension Int64: RESPValueConvertible {}
56+
extension UInt: RESPValueConvertible {}
57+
extension UInt8: RESPValueConvertible {}
58+
extension UInt16: RESPValueConvertible {}
59+
extension UInt32: RESPValueConvertible {}
60+
extension UInt64: RESPValueConvertible {}
61+
62+
extension Double: RESPValueConvertible {
63+
public init?(_ value: RESPValue) {
64+
guard let string = value.string else { return nil }
65+
guard let float = Double(string) else { return nil }
66+
self = float
8667
}
8768

8869
/// See `RESPValueConvertible`.
89-
public func convertToRESP() throws -> RESPValue {
70+
public func convertedToRESPValue() -> RESPValue {
9071
return .bulkString(Data(self.description.utf8))
9172
}
9273
}
9374

94-
extension Float: RESPConvertible {
95-
/// See `RESPValueConvertible`.
96-
public static func convertFromRESP(_ value: RESPValue) throws -> Float {
97-
guard let string = value.string else {
98-
throw RedisError(identifier: "string", reason: "Could not convert to string: \(value).")
99-
}
100-
101-
guard let float = Float(string) else {
102-
throw RedisError(identifier: "float", reason: "Could not convert to float: \(value).")
103-
}
104-
105-
return float
75+
extension Float: RESPValueConvertible {
76+
public init?(_ value: RESPValue) {
77+
guard let string = value.string else { return nil }
78+
guard let float = Float(string) else { return nil }
79+
self = float
10680
}
10781

10882
/// See `RESPValueConvertible`.
109-
public func convertToRESP() throws -> RESPValue {
83+
public func convertedToRESPValue() -> RESPValue {
11084
return .bulkString(Data(self.description.utf8))
11185
}
11286
}
11387

114-
extension Data: RESPConvertible {
115-
/// See `RESPValueConvertible`.
116-
public static func convertFromRESP(_ value: RESPValue) throws -> Data {
117-
guard let theData = value.data else {
118-
throw RedisError(identifier: "data", reason: "Could not convert to data: \(value).")
119-
}
120-
return theData
88+
extension Data: RESPValueConvertible {
89+
public init?(_ value: RESPValue) {
90+
guard let data = value.data else { return nil }
91+
self = data
12192
}
12293

12394
/// See `RESPValueConvertible`.
124-
public func convertToRESP() throws -> RESPValue {
95+
public func convertedToRESPValue() -> RESPValue {
12596
return .bulkString(self)
12697
}
12798
}
12899

129-
extension Array: RESPConvertible where Element: RESPConvertible {
130-
/// See `RESPValueConvertible`.
131-
public static func convertFromRESP(_ value: RESPValue) throws -> Array<Element> {
132-
guard let array = value.array else {
133-
throw RedisError(identifier: "array", reason: "Could not convert to array: \(value).")
134-
}
135-
return try array.map { try Element.convertFromRESP($0) }
100+
extension Array: RESPValueConvertible where Element: RESPValueConvertible {
101+
public init?(_ value: RESPValue) {
102+
guard let array = value.array else { return nil }
103+
self = array.compactMap { Element($0) }
136104
}
137105

138106
/// See `RESPValueConvertible`.
139-
public func convertToRESP() throws -> RESPValue {
140-
let dataArray = try map { try $0.convertToRESP() }
141-
return RESPValue.array(dataArray)
107+
public func convertedToRESPValue() -> RESPValue {
108+
let elements = map { $0.convertedToRESPValue() }
109+
return RESPValue.array(elements)
142110
}
143111
}

Sources/NIORedis/RedisConnection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public final class RedisConnection {
4242
/// - command: The command to execute.
4343
/// - arguments: The arguments to be sent with the command.
4444
/// - Returns: An `EventLoopFuture` that will resolve with the Redis command response.
45-
public func send(command: String, with arguments: [RESPConvertible] = []) throws -> EventLoopFuture<RESPValue> {
46-
let args = try arguments.map { try $0.convertToRESP() }
45+
public func send(command: String, with arguments: [RESPValueConvertible] = []) throws -> EventLoopFuture<RESPValue> {
46+
let args = arguments.map { $0.convertedToRESPValue() }
4747
return self.command(command, arguments: args)
4848
}
4949

Sources/NIORedis/RedisPipeline.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public final class RedisPipeline {
3838
/// - arguments: The arguments, if any, to send with the command.
3939
/// - Returns: A self-reference for chaining commands.
4040
@discardableResult
41-
public func enqueue(command: String, arguments: [RESPConvertible] = []) throws -> RedisPipeline {
42-
let args = try arguments.map { try $0.convertToRESP() }
41+
public func enqueue(command: String, arguments: [RESPValueConvertible] = []) throws -> RedisPipeline {
42+
let args = arguments.map { $0.convertedToRESPValue() }
4343

4444
let promise = channel.eventLoop.makePromise(of: RESPValue.self)
4545
let context = RedisCommandContext(

Tests/NIORedisTests/RedisDriverTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class RedisDriverTests: XCTestCase {
2929
func test_command_succeeds() throws {
3030
let result = try connection.command(
3131
"SADD",
32-
arguments: [.bulkString("key".convertedToData()), try 3.convertToRESP()
32+
arguments: [.bulkString("key".convertedToData()), 3.convertedToRESPValue()
3333
]).wait()
3434

3535
XCTAssertNotNil(result.int)

circle.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ version: 2
33
jobs:
44
linux:
55
docker:
6-
- image: codevapor/swift:5.0
6+
- image: vapor/swift:5.0
77
- image: redis:5
88
steps:
99
- checkout
1010
- run: swift build
1111
- run: swift test
1212
linux-release:
1313
docker:
14-
- image: codevapor/swift:5.0
14+
- image: vapor/swift:5.0
1515
steps:
1616
- checkout
1717
- run: swift build -c release

0 commit comments

Comments
 (0)