Commit 9741a2ef authored by Nathan Harris's avatar Nathan Harris

Merge branch 'respvalue-nil-init' into 'master'

Change `RESPValue.init(bulk:)` initializers to accept a wider range of values

See merge request !81
parents b7092362 479c024d
Pipeline #83752301 passed with stages
in 6 minutes and 39 seconds
......@@ -39,16 +39,21 @@ public enum RESPValue {
/// Initializes a `bulkString` value.
/// - Parameter value: The `String` to store in a `.bulkString` representation.
public init(bulk value: String) {
var buffer = RESPValue.allocator.buffer(capacity: value.count)
buffer.writeString(value)
public init(bulk value: String?) {
guard let unwrappedValue = value else {
self = .bulkString(nil)
return
}
var buffer = RESPValue.allocator.buffer(capacity: unwrappedValue.count)
buffer.writeString(unwrappedValue)
self = .bulkString(buffer)
}
/// Initializes a `bulkString` value.
/// - Parameter value: The `Int` value to store in a `.bulkString` representation.
public init(bulk value: Int) {
self.init(bulk: value.description)
public init<Value: FixedWidthInteger>(bulk value: Value?) {
self.init(bulk: value?.description)
}
/// Stores the representation determined by the `RESPValueConvertible` value.
......
......@@ -37,6 +37,7 @@ extension RESPTranslatorTests {
XCTAssertTrue(writingTestPass(input: .bulkString(buffer), expected: "$5\r\n".bytes + bytes + "\r\n".bytes))
XCTAssertTrue(writingTestPass(input: .init(bulk: "®in§³¾"), expected: "$10\r\n®in§³¾\r\n"))
XCTAssertTrue(writingTestPass(input: .init(bulk: ""), expected: "$0\r\n\r\n"))
XCTAssertTrue(writingTestPass(input: .init(bulk: Optional<Int>.none), expected: "$0\r\n\r\n"))
}
func testWriting_integers() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment