Skip to content

Commit d70f50b

Browse files
authored
Support JSON or JSONB for decoding structs (#168)
1 parent 2f47c2f commit d70f50b

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

Sources/PostgresKit/PostgresDataDecoder.swift

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ public final class PostgresDataDecoder {
2424
}
2525
}
2626

27-
enum _Error: Error {
28-
case keyedElement
29-
case unkeyedArray
30-
case arrayElementJSON
31-
case nesting
27+
enum Error: Swift.Error, CustomStringConvertible {
28+
case unexpectedDataType(PostgresDataType, expected: String)
29+
case nestingNotSupported
30+
31+
var description: String {
32+
switch self {
33+
case .unexpectedDataType(let type, let expected):
34+
return "Unexpected data type: \(type). Expected \(expected)."
35+
case .nestingNotSupported:
36+
return "Decoding nested containers is not supported."
37+
}
38+
}
3239
}
3340

3441
final class _Decoder: Decoder {
@@ -51,22 +58,24 @@ public final class PostgresDataDecoder {
5158
func unkeyedContainer() throws -> UnkeyedDecodingContainer {
5259
print(self.data.type)
5360
guard let data = self.data.array else {
54-
throw _Error.unkeyedArray
61+
throw Error.unexpectedDataType(self.data.type, expected: "array")
5562
}
5663
return _UnkeyedDecoder(data: data, json: self.json)
5764
}
5865

5966
func container<Key>(
6067
keyedBy type: Key.Type
6168
) throws -> KeyedDecodingContainer<Key> where Key : CodingKey {
62-
guard self.data.type == .jsonb else {
63-
throw _Error.arrayElementJSON
64-
}
65-
guard let json = self.data.jsonb else {
66-
throw _Error.arrayElementJSON
69+
let data: Data
70+
if let jsonb = self.data.jsonb {
71+
data = jsonb
72+
} else if let json = self.data.json {
73+
data = json
74+
} else {
75+
throw Error.unexpectedDataType(self.data.type, expected: "json")
6776
}
6877
return try self.json
69-
.decode(DecoderUnwrapper.self, from: json)
78+
.decode(DecoderUnwrapper.self, from: data)
7079
.decoder.container(keyedBy: Key.self)
7180
}
7281

@@ -99,29 +108,31 @@ public final class PostgresDataDecoder {
99108
mutating func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
100109
defer { self.currentIndex += 1 }
101110
let data = self.data[self.currentIndex]
102-
guard data.type == .jsonb else {
103-
throw _Error.arrayElementJSON
104-
}
105-
guard let json = data.jsonb else {
106-
throw _Error.arrayElementJSON
111+
let jsonData: Data
112+
if let jsonb = data.jsonb {
113+
jsonData = jsonb
114+
} else if let json = data.json {
115+
jsonData = json
116+
} else {
117+
throw Error.unexpectedDataType(data.type, expected: "json")
107118
}
108-
return try self.json.decode(T.self, from: json)
119+
return try self.json.decode(T.self, from: jsonData)
109120
}
110121

111122
mutating func nestedContainer<NestedKey>(
112123
keyedBy type: NestedKey.Type
113124
) throws -> KeyedDecodingContainer<NestedKey>
114125
where NestedKey : CodingKey
115126
{
116-
throw _Error.nesting
127+
throw Error.nestingNotSupported
117128
}
118129

119130
mutating func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
120-
throw _Error.nesting
131+
throw Error.nestingNotSupported
121132
}
122133

123134
mutating func superDecoder() throws -> Decoder {
124-
throw _Error.nesting
135+
throw Error.nestingNotSupported
125136
}
126137
}
127138

0 commit comments

Comments
 (0)