Skip to content

Commit 44c2916

Browse files
committed
Merge branch 'init-label' into '47-proposal-feedback'
Add `fromRESP` label to `RESPValueConvertible.init` See merge request Mordil/swift-redis-nio-client!56
2 parents f98b53e + 60d5c4c commit 44c2916

File tree

8 files changed

+48
-45
lines changed

8 files changed

+48
-45
lines changed

Sources/RedisNIO/Commands/HashCommands.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ extension RedisClient {
217217
@inlinable
218218
public func hget(_ field: String, from key: String) -> EventLoopFuture<String?> {
219219
return send(command: "HGET", with: [key, field])
220-
.map { return String($0) }
220+
.map { return String(fromRESP: $0) }
221221
}
222222

223223
/// Gets the values of a hash for the fields specified.

Sources/RedisNIO/Commands/ListCommands.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ extension RedisClient {
379379
return send(command: command, with: args)
380380
.flatMapThrowing {
381381
guard !$0.isNull else { return nil }
382-
guard let response = [RESPValue]($0) else {
382+
guard let response = [RESPValue](fromRESP: $0) else {
383383
throw RedisNIOError.responseConversion(to: [RESPValue].self)
384384
}
385385
assert(response.count == 2, "Unexpected response size returned!")

Sources/RedisNIO/Commands/SortedSetCommands.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extension RedisClient {
3030
repeat {
3131
let scoreItem = response[scoreIsFirst ? index : index + 1]
3232

33-
guard let score = Double(scoreItem) else {
33+
guard let score = Double(fromRESP: scoreItem) else {
3434
throw RedisNIOError.assertionFailure(message: "Unexpected response: '\(scoreItem)'")
3535
}
3636

@@ -125,7 +125,7 @@ extension RedisClient {
125125
@inlinable
126126
public func zscore(of element: RESPValueConvertible, in key: String) -> EventLoopFuture<Double?> {
127127
return send(command: "ZSCORE", with: [key, element])
128-
.map { return Double($0) }
128+
.map { return Double(fromRESP: $0) }
129129
}
130130

131131
/// Incrementally iterates over all elements in a sorted set.
@@ -420,13 +420,13 @@ extension RedisClient {
420420
// or an array with 3 elements in the form [Set Key, Element Score, Element Value]
421421
.flatMapThrowing {
422422
guard !$0.isNull else { return nil }
423-
guard let response = [RESPValue]($0) else {
423+
guard let response = [RESPValue](fromRESP: $0) else {
424424
throw RedisNIOError.responseConversion(to: [RESPValue].self)
425425
}
426426
assert(response.count == 3, "Unexpected response size returned!")
427427
guard
428428
let key = response[0].string,
429-
let score = Double(response[1])
429+
let score = Double(fromRESP: response[1])
430430
else {
431431
throw RedisNIOError.assertionFailure(message: "Unexpected structure in response: \(response)")
432432
}

Sources/RedisNIO/Extensions/SwiftNIO.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ extension EventLoopFuture where Value == RESPValue {
2525
file: StaticString = #function,
2626
function: StaticString = #function,
2727
line: UInt = #line
28-
) -> EventLoopFuture<T> where T: RESPValueConvertible
28+
)
29+
-> EventLoopFuture<T> where T: RESPValueConvertible
2930
{
3031
return self.flatMapThrowing {
31-
guard let value = T($0) else { throw RedisNIOError.responseConversion(to: type) }
32+
guard let value = T(fromRESP: $0) else {
33+
throw RedisNIOError.responseConversion(to: type)
34+
}
3235
return value
3336
}
3437
}

Sources/RedisNIO/RESP/RESPValueConvertible.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414

1515
/// Capable of converting to / from `RESPValue`.
1616
public protocol RESPValueConvertible {
17-
init?(_ value: RESPValue)
17+
init?(fromRESP value: RESPValue)
1818

1919
/// Creates a `RESPValue` representation.
2020
func convertedToRESPValue() -> RESPValue
2121
}
2222

2323
extension RESPValue: RESPValueConvertible {
24-
public init?(_ value: RESPValue) {
24+
public init?(fromRESP value: RESPValue) {
2525
self = value
2626
}
2727

@@ -32,7 +32,7 @@ extension RESPValue: RESPValueConvertible {
3232
}
3333

3434
extension RedisError: RESPValueConvertible {
35-
public init?(_ value: RESPValue) {
35+
public init?(fromRESP value: RESPValue) {
3636
guard let error = value.error else { return nil }
3737
self = error
3838
}
@@ -44,7 +44,7 @@ extension RedisError: RESPValueConvertible {
4444
}
4545

4646
extension String: RESPValueConvertible {
47-
public init?(_ value: RESPValue) {
47+
public init?(fromRESP value: RESPValue) {
4848
guard let string = value.string else { return nil }
4949
self = string
5050
}
@@ -56,7 +56,7 @@ extension String: RESPValueConvertible {
5656
}
5757

5858
extension FixedWidthInteger {
59-
public init?(_ value: RESPValue) {
59+
public init?(fromRESP value: RESPValue) {
6060
if let int = value.int {
6161
self = Self(int)
6262
} else {
@@ -84,7 +84,7 @@ extension UInt32: RESPValueConvertible {}
8484
extension UInt64: RESPValueConvertible {}
8585

8686
extension Double: RESPValueConvertible {
87-
public init?(_ value: RESPValue) {
87+
public init?(fromRESP value: RESPValue) {
8888
guard let string = value.string else { return nil }
8989
guard let float = Double(string) else { return nil }
9090
self = float
@@ -97,7 +97,7 @@ extension Double: RESPValueConvertible {
9797
}
9898

9999
extension Float: RESPValueConvertible {
100-
public init?(_ value: RESPValue) {
100+
public init?(fromRESP value: RESPValue) {
101101
guard let string = value.string else { return nil }
102102
guard let float = Float(string) else { return nil }
103103
self = float
@@ -121,14 +121,14 @@ extension Collection where Element: RESPValueConvertible {
121121
}
122122

123123
extension Array: RESPValueConvertible where Element: RESPValueConvertible {
124-
public init?(_ value: RESPValue) {
124+
public init?(fromRESP value: RESPValue) {
125125
guard let array = value.array else { return nil }
126-
self = array.compactMap { Element($0) }
126+
self = array.compactMap { Element(fromRESP: $0) }
127127
}
128128
}
129129

130130
extension ContiguousArray: RESPValueConvertible where Element: RESPValueConvertible {
131-
public init?(_ value: RESPValue) {
131+
public init?(fromRESP value: RESPValue) {
132132
guard let array = value.array else { return nil }
133133
self = array.compactMap(Element.init).withUnsafeBytes {
134134
.init(UnsafeRawBufferPointer($0).bindMemory(to: Element.self))
@@ -137,9 +137,9 @@ extension ContiguousArray: RESPValueConvertible where Element: RESPValueConverti
137137
}
138138

139139
extension Optional: RESPValueConvertible where Wrapped: RESPValueConvertible {
140-
public init?(_ value: RESPValue) {
140+
public init?(fromRESP value: RESPValue) {
141141
guard !value.isNull else { return nil }
142-
guard let wrapped = Wrapped(value) else { return nil }
142+
guard let wrapped = Wrapped(fromRESP: value) else { return nil }
143143

144144
self = .some(wrapped)
145145
}
@@ -156,7 +156,7 @@ extension Optional: RESPValueConvertible where Wrapped: RESPValueConvertible {
156156
import struct Foundation.Data
157157

158158
extension Data: RESPValueConvertible {
159-
public init?(_ value: RESPValue) {
159+
public init?(fromRESP value: RESPValue) {
160160
guard let data = value.data else { return nil }
161161
self = data
162162
}

Tests/RedisNIOTests/Commands/HashCommandsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ final class HashCommandsTests: XCTestCase {
127127
"second": "foo"
128128
]
129129
_ = try connection.hmset(dataset, in: #function).wait()
130-
let values = try connection.hvals(in: #function).wait().compactMap { String($0) }
130+
let values = try connection.hvals(in: #function).wait().compactMap { String(fromRESP: $0) }
131131
XCTAssertEqual(values.count, 2)
132132
XCTAssertTrue(values.allSatisfy(dataset.values.contains))
133133
}

Tests/RedisNIOTests/Commands/ListCommandsTests.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ final class ListCommandsTests: XCTestCase {
4848

4949
element = try connection.lindex(0, from: #function).wait()
5050
XCTAssertFalse(element.isNull)
51-
XCTAssertEqual(Int(element), 10)
51+
XCTAssertEqual(Int(fromRESP: element), 10)
5252
}
5353

5454
func test_lset() throws {
5555
XCTAssertThrowsError(try connection.lset(index: 0, to: 30, in: #function).wait())
5656
_ = try connection.lpush([10], into: #function).wait()
5757
XCTAssertNoThrow(try connection.lset(index: 0, to: 30, in: #function).wait())
5858
let element = try connection.lindex(0, from: #function).wait()
59-
XCTAssertEqual(Int(element), 30)
59+
XCTAssertEqual(Int(fromRESP: element), 30)
6060
}
6161

6262
func test_lrem() throws {
@@ -75,8 +75,8 @@ final class ListCommandsTests: XCTestCase {
7575

7676
elements = try connection.lrange(within: (0, 4), from: #function).wait()
7777
XCTAssertEqual(elements.count, 5)
78-
XCTAssertEqual(Int(elements[0]), 1)
79-
XCTAssertEqual(Int(elements[4]), 5)
78+
XCTAssertEqual(Int(fromRESP: elements[0]), 1)
79+
XCTAssertEqual(Int(fromRESP: elements[4]), 5)
8080

8181
elements = try connection.lrange(within: (2, 0), from: #function).wait()
8282
XCTAssertEqual(elements.count, 0)
@@ -93,20 +93,20 @@ final class ListCommandsTests: XCTestCase {
9393
_ = try connection.lpush([30], into: "second").wait()
9494

9595
var element = try connection.rpoplpush(from: "first", to: "second").wait()
96-
XCTAssertEqual(Int(element), 10)
96+
XCTAssertEqual(Int(fromRESP: element), 10)
9797
XCTAssertEqual(try connection.llen(of: "first").wait(), 0)
9898
XCTAssertEqual(try connection.llen(of: "second").wait(), 2)
9999

100100
element = try connection.rpoplpush(from: "second", to: "first").wait()
101-
XCTAssertEqual(Int(element), 30)
101+
XCTAssertEqual(Int(fromRESP: element), 30)
102102
XCTAssertEqual(try connection.llen(of: "second").wait(), 1)
103103
}
104104

105105
func test_brpoplpush() throws {
106106
_ = try connection.lpush([10], into: "first").wait()
107107

108108
let element = try connection.brpoplpush(from: "first", to: "second").wait() ?? .null
109-
XCTAssertEqual(Int(element), 10)
109+
XCTAssertEqual(Int(fromRESP: element), 10)
110110

111111
let blockingConnection = try Redis.makeConnection().wait()
112112
let expectation = XCTestExpectation(description: "brpoplpush should never return")
@@ -123,13 +123,13 @@ final class ListCommandsTests: XCTestCase {
123123

124124
_ = try connection.linsert(20, into: #function, after: 10).wait()
125125
var elements = try connection.lrange(within: (0, 1), from: #function)
126-
.map { response in response.compactMap { Int($0) } }
126+
.map { response in response.compactMap { Int(fromRESP: $0) } }
127127
.wait()
128128
XCTAssertEqual(elements, [10, 20])
129129

130130
_ = try connection.linsert(30, into: #function, before: 10).wait()
131131
elements = try connection.lrange(within: (0, 2), from: #function)
132-
.map { response in response.compactMap { Int($0) } }
132+
.map { response in response.compactMap { Int(fromRESP: $0) } }
133133
.wait()
134134
XCTAssertEqual(elements, [30, 10, 20])
135135
}
@@ -142,7 +142,7 @@ final class ListCommandsTests: XCTestCase {
142142

143143
element = try connection.lpop(from: #function).wait()
144144
XCTAssertFalse(element.isNull)
145-
XCTAssertEqual(Int(element), 30)
145+
XCTAssertEqual(Int(fromRESP: element), 30)
146146
}
147147

148148
func test_blpop() throws {
@@ -151,7 +151,7 @@ final class ListCommandsTests: XCTestCase {
151151

152152
_ = try connection.lpush([10, 20, 30], into: "first").wait()
153153
let pop1 = try connection.blpop(from: "first").wait() ?? .null
154-
XCTAssertEqual(Int(pop1), 30)
154+
XCTAssertEqual(Int(fromRESP: pop1), 30)
155155

156156
let pop2 = try connection.blpop(from: ["fake", "first"]).wait()
157157
XCTAssertEqual(pop2?.0, "first")
@@ -172,7 +172,7 @@ final class ListCommandsTests: XCTestCase {
172172
let size = try connection.lpush([100], into: #function).wait()
173173
let element = try connection.lindex(0, from: #function).wait()
174174
XCTAssertEqual(size, 4)
175-
XCTAssertEqual(Int(element), 100)
175+
XCTAssertEqual(Int(fromRESP: element), 100)
176176
}
177177

178178
func test_lpushx() throws {
@@ -184,7 +184,7 @@ final class ListCommandsTests: XCTestCase {
184184
size = try connection.lpushx(30, into: #function).wait()
185185
XCTAssertEqual(size, 2)
186186
let element = try connection.rpop(from: #function)
187-
.map { return Int($0) }
187+
.map { return Int(fromRESP: $0) }
188188
.wait()
189189
XCTAssertEqual(element, 10)
190190
}
@@ -194,7 +194,7 @@ final class ListCommandsTests: XCTestCase {
194194

195195
let element = try connection.rpop(from: #function).wait()
196196
XCTAssertNotNil(element)
197-
XCTAssertEqual(Int(element), 10)
197+
XCTAssertEqual(Int(fromRESP: element), 10)
198198

199199
_ = try connection.delete([#function]).wait()
200200

@@ -208,7 +208,7 @@ final class ListCommandsTests: XCTestCase {
208208

209209
_ = try connection.lpush([10, 20, 30], into: "first").wait()
210210
let pop1 = try connection.brpop(from: "first").wait() ?? .null
211-
XCTAssertEqual(Int(pop1), 10)
211+
XCTAssertEqual(Int(fromRESP: pop1), 10)
212212

213213
let pop2 = try connection.brpop(from: ["fake", "first"]).wait()
214214
XCTAssertEqual(pop2?.0, "first")
@@ -229,7 +229,7 @@ final class ListCommandsTests: XCTestCase {
229229
let size = try connection.rpush([100], into: #function).wait()
230230
let element = try connection.lindex(3, from: #function).wait()
231231
XCTAssertEqual(size, 4)
232-
XCTAssertEqual(Int(element), 100)
232+
XCTAssertEqual(Int(fromRESP: element), 100)
233233
}
234234

235235
func test_rpushx() throws {
@@ -241,7 +241,7 @@ final class ListCommandsTests: XCTestCase {
241241
size = try connection.rpushx(30, into: #function).wait()
242242
XCTAssertEqual(size, 2)
243243
let element = try connection.lpop(from: #function)
244-
.map { return Int($0) }
244+
.map { return Int(fromRESP: $0) }
245245
.wait()
246246
XCTAssertEqual(element, 10)
247247
}

Tests/RedisNIOTests/Commands/SortedSetCommandsTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ final class SortedSetCommandsTests: XCTestCase {
259259
XCTAssertEqual(elements.count, 6)
260260

261261
let values = try RedisConnection._mapSortedSetResponse(elements, scoreIsFirst: false)
262-
.map { (value, _) in return Int(value) }
262+
.map { (value, _) in return Int(fromRESP: value) }
263263

264264
XCTAssertEqual(values[0], 2)
265265
XCTAssertEqual(values[1], 3)
@@ -273,7 +273,7 @@ final class SortedSetCommandsTests: XCTestCase {
273273
XCTAssertEqual(elements.count, 6)
274274

275275
let values = try RedisConnection._mapSortedSetResponse(elements, scoreIsFirst: false)
276-
.map { (value, _) in return Int(value) }
276+
.map { (value, _) in return Int(fromRESP: value) }
277277

278278
XCTAssertEqual(values[0], 9)
279279
XCTAssertEqual(values[1], 8)
@@ -313,14 +313,14 @@ final class SortedSetCommandsTests: XCTestCase {
313313

314314
var elements = try connection.zrangebylex(within: ("[1", "[2"), from: #function)
315315
.wait()
316-
.map { Int($0) }
316+
.map { Int(fromRESP: $0) }
317317
XCTAssertEqual(elements.count, 2)
318318
XCTAssertEqual(elements[0], 1)
319319
XCTAssertEqual(elements[1], 2)
320320

321321
elements = try connection.zrangebylex(within: ("[1", "(4"), from: #function, limitBy: (offset: 1, count: 1))
322322
.wait()
323-
.map { Int($0) }
323+
.map { Int(fromRESP: $0) }
324324
XCTAssertEqual(elements.count, 1)
325325
XCTAssertEqual(elements[0], 2)
326326
}
@@ -330,14 +330,14 @@ final class SortedSetCommandsTests: XCTestCase {
330330

331331
var elements = try connection.zrevrangebylex(within: ("(2", "[4"), from: #function)
332332
.wait()
333-
.map { Int($0) }
333+
.map { Int(fromRESP: $0) }
334334
XCTAssertEqual(elements.count, 2)
335335
XCTAssertEqual(elements[0], 4)
336336
XCTAssertEqual(elements[1], 3)
337337

338338
elements = try connection.zrevrangebylex(within: ("[1", "(4"), from: #function, limitBy: (offset: 1, count: 2))
339339
.wait()
340-
.map { Int($0) }
340+
.map { Int(fromRESP: $0) }
341341
XCTAssertEqual(elements.count, 2)
342342
XCTAssertEqual(elements[0], 2)
343343
}

0 commit comments

Comments
 (0)