forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresCell.swift
60 lines (53 loc) · 1.59 KB
/
PostgresCell.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#if swift(>=5.6)
@preconcurrency import NIOCore
#else
import NIOCore
#endif
public struct PostgresCell: Equatable {
public var bytes: ByteBuffer?
public var dataType: PostgresDataType
public var format: PostgresFormat
public var columnName: String
public var columnIndex: Int
init(bytes: ByteBuffer?, dataType: PostgresDataType, format: PostgresFormat, columnName: String, columnIndex: Int) {
self.bytes = bytes
self.dataType = dataType
self.format = format
self.columnName = columnName
self.columnIndex = columnIndex
}
}
extension PostgresCell {
@inlinable
public func decode<T: PostgresDecodable, JSONDecoder: PostgresJSONDecoder>(
_: T.Type,
context: PostgresDecodingContext<JSONDecoder>,
file: String = #file,
line: Int = #line
) throws -> T {
var copy = self.bytes
do {
return try T._decodeRaw(
from: ©,
type: self.dataType,
format: self.format,
context: context
)
} catch let code as PostgresDecodingError.Code {
throw PostgresDecodingError(
code: code,
columnName: self.columnName,
columnIndex: self.columnIndex,
targetType: T.self,
postgresType: self.dataType,
postgresFormat: self.format,
postgresData: copy,
file: file,
line: line
)
}
}
}
#if swift(>=5.6)
extension PostgresCell: Sendable {}
#endif