forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRawRepresentable+PostgresCodable.swift
37 lines (31 loc) · 1.24 KB
/
RawRepresentable+PostgresCodable.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
import NIOCore
extension PostgresEncodable where Self: RawRepresentable, RawValue: PostgresEncodable {
public static var psqlType: PostgresDataType {
RawValue.psqlType
}
public static var psqlFormat: PostgresFormat {
RawValue.psqlFormat
}
@inlinable
public func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PostgresEncodingContext<JSONEncoder>
) throws {
try rawValue.encode(into: &byteBuffer, context: context)
}
}
extension PostgresDecodable where Self: RawRepresentable, RawValue: PostgresDecodable, RawValue._DecodableType == RawValue {
init<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws {
guard let rawValue = try? RawValue(from: &buffer, type: type, format: format, context: context),
let selfValue = Self.init(rawValue: rawValue) else {
throw PostgresDecodingError.Code.failure
}
self = selfValue
}
}
extension PostgresCodable where Self: RawRepresentable, RawValue: PostgresCodable, RawValue._DecodableType == RawValue {}