@@ -24,11 +24,18 @@ public final class PostgresDataDecoder {
24
24
}
25
25
}
26
26
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
+ }
32
39
}
33
40
34
41
final class _Decoder : Decoder {
@@ -51,22 +58,24 @@ public final class PostgresDataDecoder {
51
58
func unkeyedContainer( ) throws -> UnkeyedDecodingContainer {
52
59
print ( self . data. type)
53
60
guard let data = self . data. array else {
54
- throw _Error . unkeyedArray
61
+ throw Error . unexpectedDataType ( self . data . type , expected : " array " )
55
62
}
56
63
return _UnkeyedDecoder ( data: data, json: self . json)
57
64
}
58
65
59
66
func container< Key> (
60
67
keyedBy type: Key . Type
61
68
) 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 " )
67
76
}
68
77
return try self . json
69
- . decode ( DecoderUnwrapper . self, from: json )
78
+ . decode ( DecoderUnwrapper . self, from: data )
70
79
. decoder. container ( keyedBy: Key . self)
71
80
}
72
81
@@ -99,29 +108,31 @@ public final class PostgresDataDecoder {
99
108
mutating func decode< T> ( _ type: T . Type ) throws -> T where T : Decodable {
100
109
defer { self . currentIndex += 1 }
101
110
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 " )
107
118
}
108
- return try self . json. decode ( T . self, from: json )
119
+ return try self . json. decode ( T . self, from: jsonData )
109
120
}
110
121
111
122
mutating func nestedContainer< NestedKey> (
112
123
keyedBy type: NestedKey . Type
113
124
) throws -> KeyedDecodingContainer < NestedKey >
114
125
where NestedKey : CodingKey
115
126
{
116
- throw _Error . nesting
127
+ throw Error . nestingNotSupported
117
128
}
118
129
119
130
mutating func nestedUnkeyedContainer( ) throws -> UnkeyedDecodingContainer {
120
- throw _Error . nesting
131
+ throw Error . nestingNotSupported
121
132
}
122
133
123
134
mutating func superDecoder( ) throws -> Decoder {
124
- throw _Error . nesting
135
+ throw Error . nestingNotSupported
125
136
}
126
137
}
127
138
0 commit comments