forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresCell.swift
88 lines (81 loc) · 3.27 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import NIOCore
/// A representation of a cell value within a ``PostgresRow`` and ``PostgresRandomAccessRow``.
public struct PostgresCell: Sendable, Equatable {
/// The cell's value as raw bytes.
public var bytes: ByteBuffer?
/// The cell's data type. This is important metadata when decoding the cell.
public var dataType: PostgresDataType
/// The format in which the cell's bytes are encoded.
public var format: PostgresFormat
/// The cell's column name within the row.
public var columnName: String
/// The cell's column index within the row.
public var columnIndex: Int
public 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 {
/// Decode the cell into a Swift type, that conforms to ``PostgresDecodable``
///
/// - Parameters:
/// - _: The Swift type, which conforms to ``PostgresDecodable``, to decode from the cell's ``PostgresCell/bytes`` values.
/// - context: A ``PostgresDecodingContext`` to supply a custom ``PostgresJSONDecoder`` for decoding JSON fields.
/// - file: The source file in which this method was called. Used in the error case in ``PostgresDecodingError``.
/// - line: The source file line in which this method was called. Used in the error case in ``PostgresDecodingError``.
/// - Returns: A decoded Swift type.
@inlinable
public func decode<T: PostgresDecodable, JSONDecoder: PostgresJSONDecoder>(
_: T.Type,
context: PostgresDecodingContext<JSONDecoder>,
file: String = #fileID,
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
)
}
}
/// Decode the cell into a Swift type, that conforms to ``PostgresDecodable``
///
/// - Parameters:
/// - _: The Swift type, which conforms to ``PostgresDecodable``, to decode from the cell's ``PostgresCell/bytes`` values.
/// - file: The source file in which this method was called. Used in the error case in ``PostgresDecodingError``.
/// - line: The source file line in which this method was called. Used in the error case in ``PostgresDecodingError``.
/// - Returns: A decoded Swift type.
@inlinable
public func decode<T: PostgresDecodable>(
_: T.Type,
file: String = #fileID,
line: Int = #line
) throws -> T {
try self.decode(T.self, context: .default, file: file, line: line)
}
}