forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresDataDecoder.swift
158 lines (134 loc) · 4.74 KB
/
PostgresDataDecoder.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import Foundation
struct DecoderUnwrapper: Decodable {
let decoder: Decoder
init(from decoder: Decoder) {
self.decoder = decoder
}
}
public final class PostgresDataDecoder {
public let jsonDecoder: JSONDecoder
public init(json: JSONDecoder = JSONDecoder()) {
self.jsonDecoder = json
}
public func decode<T>(_ type: T.Type, from data: PostgresData) throws -> T
where T: Decodable
{
if let convertible = T.self as? PostgresDataConvertible.Type {
return convertible.init(postgresData: data)! as! T
} else {
return try T.init(from: _Decoder(data: data, json: self.jsonDecoder))
}
}
enum Error: Swift.Error, CustomStringConvertible {
case unexpectedDataType(PostgresDataType, expected: String)
case nestingNotSupported
var description: String {
switch self {
case .unexpectedDataType(let type, let expected):
return "Unexpected data type: \(type). Expected \(expected)."
case .nestingNotSupported:
return "Decoding nested containers is not supported."
}
}
}
final class _Decoder: Decoder {
var codingPath: [CodingKey] {
return []
}
var userInfo: [CodingUserInfoKey : Any] {
return [:]
}
let data: PostgresData
let json: JSONDecoder
init(data: PostgresData, json: JSONDecoder) {
self.data = data
self.json = json
}
func unkeyedContainer() throws -> UnkeyedDecodingContainer {
print(self.data.type)
guard let data = self.data.array else {
throw Error.unexpectedDataType(self.data.type, expected: "array")
}
return _UnkeyedDecoder(data: data, json: self.json)
}
func container<Key>(
keyedBy type: Key.Type
) throws -> KeyedDecodingContainer<Key> where Key : CodingKey {
let data: Data
if let jsonb = self.data.jsonb {
data = jsonb
} else if let json = self.data.json {
data = json
} else {
throw Error.unexpectedDataType(self.data.type, expected: "json")
}
return try self.json
.decode(DecoderUnwrapper.self, from: data)
.decoder.container(keyedBy: Key.self)
}
func singleValueContainer() throws -> SingleValueDecodingContainer {
_ValueDecoder(data: self.data, json: self.json)
}
}
struct _UnkeyedDecoder: UnkeyedDecodingContainer {
var count: Int? {
self.data.count
}
var isAtEnd: Bool {
self.currentIndex == self.data.count
}
var currentIndex: Int = 0
let data: [PostgresData]
let json: JSONDecoder
var codingPath: [CodingKey] {
[]
}
mutating func decodeNil() throws -> Bool {
defer { self.currentIndex += 1 }
return self.data[self.currentIndex].value == nil
}
mutating func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
defer { self.currentIndex += 1 }
let data = self.data[self.currentIndex]
let jsonData: Data
if let jsonb = data.jsonb {
jsonData = jsonb
} else if let json = data.json {
jsonData = json
} else {
throw Error.unexpectedDataType(data.type, expected: "json")
}
return try self.json.decode(T.self, from: jsonData)
}
mutating func nestedContainer<NestedKey>(
keyedBy type: NestedKey.Type
) throws -> KeyedDecodingContainer<NestedKey>
where NestedKey : CodingKey
{
throw Error.nestingNotSupported
}
mutating func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
throw Error.nestingNotSupported
}
mutating func superDecoder() throws -> Decoder {
throw Error.nestingNotSupported
}
}
struct _ValueDecoder: SingleValueDecodingContainer {
let data: PostgresData
let json: JSONDecoder
var codingPath: [CodingKey] {
[]
}
func decodeNil() -> Bool {
return self.data.value == nil
}
func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
if let convertible = T.self as? PostgresDataConvertible.Type {
return convertible.init(postgresData: self.data)! as! T
} else {
return try T.init(from: _Decoder(data: self.data, json: self.json))
}
}
}
}