|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// Supported `PostgreSQLData` data types. |
| 4 | +public enum PostgreSQLData { |
| 5 | + case string(String) |
| 6 | + case data(Data) |
| 7 | + case date(Date) |
| 8 | + |
| 9 | + case int8(Int8) |
| 10 | + case int16(Int16) |
| 11 | + case int32(Int32) |
| 12 | + case int(Int) |
| 13 | + |
| 14 | + case uint8(UInt8) |
| 15 | + case uint16(UInt16) |
| 16 | + case uint32(UInt32) |
| 17 | + case uint(UInt) |
| 18 | + |
| 19 | + case float(Float) |
| 20 | + case double(Double) |
| 21 | + |
| 22 | + case null |
| 23 | +} |
| 24 | + |
| 25 | +/// MARK: Polymorphic |
| 26 | + |
| 27 | +extension PostgreSQLData { |
| 28 | + /// Returns string value, `nil` if not a string. |
| 29 | + public var string: String? { |
| 30 | + switch self { |
| 31 | + case .string(let s): return s |
| 32 | + default: return nil |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// Returns int value, `nil` if not an int. |
| 37 | + public var int: Int? { |
| 38 | + switch self { |
| 39 | + case .int8(let i): return Int(i) |
| 40 | + case .int16(let i): return Int(i) |
| 41 | + case .int32(let i): return Int(i) |
| 42 | + case .int(let i): return i |
| 43 | + case .uint8(let ui): return Int(ui) |
| 44 | + case .uint16(let ui): return Int(ui) |
| 45 | + case .uint32(let ui): return Int(ui) |
| 46 | + case .uint(let ui): |
| 47 | + guard ui < UInt(Int.max) else { return nil } |
| 48 | + return Int(ui) |
| 49 | + case .string(let s): return Int(s) |
| 50 | + default: return nil |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// Returns bool value, `nil` if not a bool. |
| 55 | + public var bool: Bool? { |
| 56 | + if let int = self.int { |
| 57 | + switch int { |
| 58 | + case 1: return true |
| 59 | + case 0: return false |
| 60 | + default: return nil |
| 61 | + } |
| 62 | + } else if let string = self.string { |
| 63 | + switch string.lowercased() { |
| 64 | + case "t", "true": return true |
| 65 | + case "f", "false": return false |
| 66 | + default: return nil |
| 67 | + } |
| 68 | + } else { |
| 69 | + return nil |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// MARK: Equatable |
| 75 | + |
| 76 | +extension PostgreSQLData: Equatable { |
| 77 | + /// See Equatable.== |
| 78 | + public static func ==(lhs: PostgreSQLData, rhs: PostgreSQLData) -> Bool { |
| 79 | + switch (lhs, rhs) { |
| 80 | + case (.string(let a), .string(let b)): return a == b |
| 81 | + case (.data(let a), .data(let b)): return a == b |
| 82 | + case (.date(let a), .date(let b)): return a == b |
| 83 | + case (.int8(let a), .int8(let b)): return a == b |
| 84 | + case (.int16(let a), .int16(let b)): return a == b |
| 85 | + case (.int32(let a), .int32(let b)): return a == b |
| 86 | + case (.int(let a), .int(let b)): return a == b |
| 87 | + case (.uint8(let a), .uint8(let b)): return a == b |
| 88 | + case (.uint16(let a), .uint16(let b)): return a == b |
| 89 | + case (.uint32(let a), .uint32(let b)): return a == b |
| 90 | + case (.uint(let a), .uint(let b)): return a == b |
| 91 | + case (.float(let a), .float(let b)): return a == b |
| 92 | + case (.double(let a), .double(let b)): return a == b |
| 93 | + case (.null, .null): return true |
| 94 | + default: return false |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +/// MARK: Custom String |
| 102 | + |
| 103 | +extension PostgreSQLData: CustomStringConvertible { |
| 104 | + /// See CustomStringConvertible.description |
| 105 | + public var description: String { |
| 106 | + switch self { |
| 107 | + case .string(let val): return "\"\(val)\"" |
| 108 | + case .data(let val): return val.hexDebug |
| 109 | + case .date(let val): return val.description |
| 110 | + case .int8(let val): return "\(val) (int8)" |
| 111 | + case .int16(let val): return "\(val) (int16)" |
| 112 | + case .int32(let val): return "\(val) (int32)" |
| 113 | + case .int(let val): return "\(val) (int)" |
| 114 | + case .uint8(let val): return "\(val) (uint8)" |
| 115 | + case .uint16(let val): return "\(val) (uint16)" |
| 116 | + case .uint32(let val): return "\(val) (uint32)" |
| 117 | + case .uint(let val): return "\(val) (uint)" |
| 118 | + case .float(let val): return "\(val) (float)" |
| 119 | + case .double(let val): return "\(val) (double)" |
| 120 | + case .null: return "null" |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments