Skip to content

Commit 479c024

Browse files
committed
Change RESPValue.init(bulk:) initializers to accept a wider range of values
Motivation: While working to add more test coverage with `RESPTranslator`, it was made apparent that a `.bulkString(.none)` is impossible to create directly with the `RESPValue` initializers, even though it is a reasonable possibility. Additionally, forcing all integer types to have to be stored in an `Int` is unnecessarily restrictive. Modifications: - Change `RESPValue.init(bulk:)` initializers to accept `Optional` instances - Change `RESPValue.init(bulk:)` for `Int` initializer to be generic on `FixedWidthInteger` Result: Converting types to and from `RESPValue` should be more bi-directional and seamless.
1 parent b709236 commit 479c024

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

Sources/RediStack/RESP/RESPValue.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,21 @@ public enum RESPValue {
3939

4040
/// Initializes a `bulkString` value.
4141
/// - Parameter value: The `String` to store in a `.bulkString` representation.
42-
public init(bulk value: String) {
43-
var buffer = RESPValue.allocator.buffer(capacity: value.count)
44-
buffer.writeString(value)
42+
public init(bulk value: String?) {
43+
guard let unwrappedValue = value else {
44+
self = .bulkString(nil)
45+
return
46+
}
47+
48+
var buffer = RESPValue.allocator.buffer(capacity: unwrappedValue.count)
49+
buffer.writeString(unwrappedValue)
4550
self = .bulkString(buffer)
4651
}
4752

4853
/// Initializes a `bulkString` value.
4954
/// - Parameter value: The `Int` value to store in a `.bulkString` representation.
50-
public init(bulk value: Int) {
51-
self.init(bulk: value.description)
55+
public init<Value: FixedWidthInteger>(bulk value: Value?) {
56+
self.init(bulk: value?.description)
5257
}
5358

5459
/// Stores the representation determined by the `RESPValueConvertible` value.

Tests/RediStackTests/RESPTranslatorTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ extension RESPTranslatorTests {
3737
XCTAssertTrue(writingTestPass(input: .bulkString(buffer), expected: "$5\r\n".bytes + bytes + "\r\n".bytes))
3838
XCTAssertTrue(writingTestPass(input: .init(bulk: "®in§³¾"), expected: "$10\r\n®in§³¾\r\n"))
3939
XCTAssertTrue(writingTestPass(input: .init(bulk: ""), expected: "$0\r\n\r\n"))
40+
XCTAssertTrue(writingTestPass(input: .init(bulk: Optional<Int>.none), expected: "$0\r\n\r\n"))
4041
}
4142

4243
func testWriting_integers() {

0 commit comments

Comments
 (0)