|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// Capable of converting to / from `RedisData`. |
| 4 | +public protocol RedisDataConvertible { |
| 5 | + /// Create an instance of `Self` from `RedisData`. |
| 6 | + static func convertFromRedisData(_ data: RedisData) throws -> Self |
| 7 | + |
| 8 | + /// Convert self to `RedisData`. |
| 9 | + func convertToRedisData() throws -> RedisData |
| 10 | +} |
| 11 | + |
| 12 | +extension RedisData: RedisDataConvertible { |
| 13 | + /// See `RedisDataConvertible`. |
| 14 | + public func convertToRedisData() throws -> RedisData { |
| 15 | + return self |
| 16 | + } |
| 17 | + |
| 18 | + /// See `RedisDataConvertible`. |
| 19 | + public static func convertFromRedisData(_ data: RedisData) throws -> RedisData { |
| 20 | + return data |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +extension String: RedisDataConvertible { |
| 25 | + /// See `RedisDataConvertible`. |
| 26 | + public static func convertFromRedisData(_ data: RedisData) throws -> String { |
| 27 | + guard let string = data.string else { |
| 28 | + throw RedisError(identifier: "string", reason: "Could not convert to string: \(data).") |
| 29 | + } |
| 30 | + return string |
| 31 | + } |
| 32 | + |
| 33 | + /// See `RedisDataConvertible`. |
| 34 | + public func convertToRedisData() throws -> RedisData { |
| 35 | + return .bulkString(Data(self.utf8)) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +extension FixedWidthInteger { |
| 40 | + /// See `RedisDataConvertible`. |
| 41 | + public static func convertFromRedisData(_ data: RedisData) throws -> Self { |
| 42 | + guard let int = data.int else { |
| 43 | + guard let string = data.string else { |
| 44 | + throw RedisError(identifier: "string", reason: "Could not convert to string: \(data)") |
| 45 | + } |
| 46 | + |
| 47 | + guard let int = Self(string) else { |
| 48 | + throw RedisError(identifier: "int", reason: "Could not convert to int: \(data)") |
| 49 | + } |
| 50 | + |
| 51 | + return int |
| 52 | + } |
| 53 | + |
| 54 | + return Self(int) |
| 55 | + } |
| 56 | + |
| 57 | + /// See `RedisDataConvertible`. |
| 58 | + public func convertToRedisData() throws -> RedisData { |
| 59 | + return .bulkString(Data(self.description.utf8)) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +extension Int: RedisDataConvertible {} |
| 64 | +extension Int8: RedisDataConvertible {} |
| 65 | +extension Int16: RedisDataConvertible {} |
| 66 | +extension Int32: RedisDataConvertible {} |
| 67 | +extension Int64: RedisDataConvertible {} |
| 68 | +extension UInt: RedisDataConvertible {} |
| 69 | +extension UInt8: RedisDataConvertible {} |
| 70 | +extension UInt16: RedisDataConvertible {} |
| 71 | +extension UInt32: RedisDataConvertible {} |
| 72 | +extension UInt64: RedisDataConvertible {} |
| 73 | + |
| 74 | +extension Double: RedisDataConvertible { |
| 75 | + /// See `RedisDataConvertible`. |
| 76 | + public static func convertFromRedisData(_ data: RedisData) throws -> Double { |
| 77 | + guard let string = data.string else { |
| 78 | + throw RedisError(identifier: "string", reason: "Could not convert to string: \(data).") |
| 79 | + } |
| 80 | + |
| 81 | + guard let float = Double(string) else { |
| 82 | + throw RedisError(identifier: "double", reason: "Could not convert to double: \(data).") |
| 83 | + } |
| 84 | + |
| 85 | + return float |
| 86 | + } |
| 87 | + |
| 88 | + /// See `RedisDataConvertible`. |
| 89 | + public func convertToRedisData() throws -> RedisData { |
| 90 | + return .bulkString(Data(self.description.utf8)) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +extension Float: RedisDataConvertible { |
| 95 | + /// See `RedisDataConvertible`. |
| 96 | + public static func convertFromRedisData(_ data: RedisData) throws -> Float { |
| 97 | + guard let string = data.string else { |
| 98 | + throw RedisError(identifier: "string", reason: "Could not convert to string: \(data).") |
| 99 | + } |
| 100 | + |
| 101 | + guard let float = Float(string) else { |
| 102 | + throw RedisError(identifier: "float", reason: "Could not convert to float: \(data).") |
| 103 | + } |
| 104 | + |
| 105 | + return float |
| 106 | + } |
| 107 | + |
| 108 | + /// See `RedisDataConvertible`. |
| 109 | + public func convertToRedisData() throws -> RedisData { |
| 110 | + return .bulkString(Data(self.description.utf8)) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +extension Data: RedisDataConvertible { |
| 115 | + /// See `RedisDataConvertible`. |
| 116 | + public static func convertFromRedisData(_ data: RedisData) throws -> Data { |
| 117 | + guard let theData = data.data else { |
| 118 | + throw RedisError(identifier: "data", reason: "Could not convert to data: \(data).") |
| 119 | + } |
| 120 | + return theData |
| 121 | + } |
| 122 | + |
| 123 | + /// See `RedisDataConvertible`. |
| 124 | + public func convertToRedisData() throws -> RedisData { |
| 125 | + return .bulkString(self) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +extension Array: RedisDataConvertible where Element: RedisDataConvertible { |
| 130 | + /// See `RedisDataConvertible`. |
| 131 | + public static func convertFromRedisData(_ data: RedisData) throws -> Array<Element> { |
| 132 | + guard let array = data.array else { |
| 133 | + throw RedisError(identifier: "array", reason: "Could not convert to array: \(data).") |
| 134 | + } |
| 135 | + return try array.map { try Element.convertFromRedisData($0) } |
| 136 | + } |
| 137 | + |
| 138 | + /// See `RedisDataConvertible`. |
| 139 | + public func convertToRedisData() throws -> RedisData { |
| 140 | + let dataArray = try map { try $0.convertToRedisData() } |
| 141 | + return RedisData.array(dataArray) |
| 142 | + } |
| 143 | +} |
0 commit comments