Skip to content

Commit 7933bf8

Browse files
committed
add optional type
1 parent 086c03f commit 7933bf8

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

Sources/PostgreSQL/Data/PostgreSQLData+BinaryFloatingPoint.swift

+8-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ extension BinaryFloatingPoint {
2929
case .int2: return try Self.init(value.makeFixedWidthInteger(Int16.self))
3030
case .int4: return try Self.init(value.makeFixedWidthInteger(Int32.self))
3131
case .int8: return try Self.init(value.makeFixedWidthInteger(Int64.self))
32-
default: throw DecodingError.typeMismatch(Self.self, .init(codingPath: [], debugDescription: ""))
32+
case .timestamp, .date, .time:
33+
let date = try Date.convertFromPostgreSQLData(data)
34+
return Self(date.timeIntervalSinceReferenceDate)
35+
default:
36+
throw PostgreSQLError(
37+
identifier: "binaryFloatingPoint",
38+
reason: "Could not decode \(Self.self) from binary data type: \(data.type)."
39+
)
3340
}
3441
case .text:
3542
let string = try data.decode(String.self)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Async
2+
import Foundation
3+
4+
extension OptionalType {
5+
/// FIXME: conditional conformance
6+
7+
/// See `PostgreSQLDataCustomConvertible.preferredDataType`
8+
public static var preferredDataType: PostgreSQLDataType? { return .bool }
9+
10+
/// See `PostgreSQLDataCustomConvertible.convertFromPostgreSQLData(_:)`
11+
public static func convertFromPostgreSQLData(_ data: PostgreSQLData) throws -> Self {
12+
guard let convertible = WrappedType.self as? PostgreSQLDataCustomConvertible.Type else {
13+
throw PostgreSQLError(identifier: "optional", reason: "Optional wrapped type \(WrappedType.self) is not PostgreSQLDataCustomConvertible")
14+
}
15+
let wrapped = try convertible.convertFromPostgreSQLData(data) as! WrappedType
16+
return Self.makeOptionalType(wrapped)
17+
}
18+
19+
/// See `PostgreSQLDataCustomConvertible.convertToPostgreSQLData()`
20+
public func convertToPostgreSQLData() throws -> PostgreSQLData {
21+
if let wrapped = self.wrapped {
22+
guard let convertible = wrapped as? PostgreSQLDataCustomConvertible else {
23+
throw PostgreSQLError(identifier: "optional", reason: "Optional wrapped type \(WrappedType.self) is not PostgreSQLDataCustomConvertible")
24+
}
25+
return try convertible.convertToPostgreSQLData()
26+
} else {
27+
return PostgreSQLData(type: .void, format: .binary, data: nil)
28+
}
29+
}
30+
}
31+
32+
extension Optional: PostgreSQLDataCustomConvertible { }
33+

Sources/PostgreSQL/Data/PostgreSQLData+String.swift

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ extension String: PostgreSQLDataCustomConvertible {
2525
case .point:
2626
let point = try PostgreSQLPoint.convertFromPostgreSQLData(data)
2727
return point.description
28+
case .uuid:
29+
return try UUID.convertFromPostgreSQLData(data).uuidString
2830
case .numeric:
2931
/// create mutable value since we will be using `.extract` which advances the buffer's view
3032
var value = value

Sources/PostgreSQL/Data/PostgreSQLData.swift

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public struct PostgreSQLData {
1818
}
1919
}
2020

21+
extension PostgreSQLData: CustomStringConvertible {
22+
/// See `CustomStringConvertible.description`
23+
public var description: String {
24+
return "\(type) (\(format)) \(data?.hexDebug ?? "nil")"
25+
}
26+
}
27+
2128
/// MARK: Equatable
2229

2330
extension PostgreSQLData: Equatable {

0 commit comments

Comments
 (0)