Skip to content

Commit 575e5be

Browse files
committed
unkeyed decoding container
1 parent 856bdda commit 575e5be

4 files changed

+164
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// Represents an array index.
2+
struct PostgreSQLDataArrayKey: CodingKey {
3+
/// See `CodingKey.intValue`
4+
var intValue: Int?
5+
6+
/// See `CodingKey.stringValue`
7+
var stringValue: String
8+
9+
/// See `CodingKey.init(stringValue:)`
10+
init?(stringValue: String) {
11+
return nil
12+
}
13+
14+
/// See `CodingKey.init(intValue:)`
15+
init?(intValue: Int) {
16+
self.init(intValue)
17+
}
18+
19+
/// Creates a new `PostgreSQLDataArrayKey` from the supplied index.
20+
init(_ index: Int) {
21+
self.intValue = index
22+
self.stringValue = index.description
23+
}
24+
}

Sources/PostgreSQL/Coders/PostgreSQLDataDecoder.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal final class _PostgreSQLDataDecoder: Decoder {
4040

4141
/// See `Decoder.unkeyedContainer`
4242
func unkeyedContainer() -> UnkeyedDecodingContainer {
43-
fatalError()
43+
return PostgreSQLDataUnkeyedDecodingContainer(partialData: partialData, at: codingPath)
4444
}
4545

4646
/// See `Decoder.singleValueContainer`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/// Internal `UnkeyedDecodingContainer` for `PostgreSQLDataDecoder`
2+
final class PostgreSQLDataUnkeyedDecodingContainer: UnkeyedDecodingContainer {
3+
/// See `UnkeyedDecodingContainer.count`
4+
var count: Int?
5+
6+
/// See `UnkeyedDecodingContainer.isAtEnd`
7+
var isAtEnd: Bool {
8+
return currentIndex == count
9+
}
10+
11+
/// See `UnkeyedDecodingContainer.currentIndex`
12+
var currentIndex: Int
13+
14+
/// Creates a coding key for the current index, then increments the count.
15+
var index: CodingKey {
16+
defer { currentIndex += 1}
17+
return PostgreSQLDataArrayKey(currentIndex)
18+
}
19+
20+
/// See `UnkeyedDecodingContainer.codingPath`
21+
var codingPath: [CodingKey]
22+
23+
/// Data being encoded.
24+
let partialData: PartialPostgreSQLData
25+
26+
/// Creates a new internal `PostgreSQLDataUnkeyedDecodingContainer`.
27+
init(partialData: PartialPostgreSQLData, at path: [CodingKey]) {
28+
self.codingPath = path
29+
self.partialData = partialData
30+
switch partialData.get(at: codingPath) {
31+
case .some(let w):
32+
switch w {
33+
case .array(let a): count = a.count
34+
default: count = nil
35+
}
36+
case .none: count = nil
37+
}
38+
currentIndex = 0
39+
}
40+
41+
/// See `UnkeyedDecodingContainer.decodeNil`
42+
func decodeNil() throws -> Bool {
43+
return partialData.get(at: codingPath + [index]) == nil
44+
}
45+
46+
/// See `UnkeyedDecodingContainer.decode`
47+
func decode(_ type: Bool.Type) throws -> Bool {
48+
return try partialData.requireBool(at: codingPath + [index])
49+
}
50+
51+
/// See `UnkeyedDecodingContainer.decode`
52+
func decode(_ type: Int.Type) throws -> Int {
53+
return try partialData.requireFixedWidthItenger(at: codingPath + [index])
54+
}
55+
56+
/// See `UnkeyedDecodingContainer.decode`
57+
func decode(_ type: Int8.Type) throws -> Int8 {
58+
return try partialData.requireFixedWidthItenger(at: codingPath + [index])
59+
}
60+
61+
/// See `UnkeyedDecodingContainer.decode`
62+
func decode(_ type: Int16.Type) throws -> Int16 {
63+
return try partialData.requireFixedWidthItenger(at: codingPath + [index])
64+
}
65+
66+
/// See `UnkeyedDecodingContainer.decode`
67+
func decode(_ type: Int32.Type) throws -> Int32 {
68+
return try partialData.requireFixedWidthItenger(at: codingPath + [index])
69+
}
70+
71+
/// See `UnkeyedDecodingContainer.decode`
72+
func decode(_ type: Int64.Type) throws -> Int64 {
73+
return try partialData.requireFixedWidthItenger(at: codingPath + [index])
74+
}
75+
76+
/// See `UnkeyedDecodingContainer.decode`
77+
func decode(_ type: UInt.Type) throws -> UInt {
78+
return try partialData.requireFixedWidthItenger(at: codingPath + [index])
79+
}
80+
81+
/// See `UnkeyedDecodingContainer.decode`
82+
func decode(_ type: UInt8.Type) throws -> UInt8 {
83+
return try partialData.requireFixedWidthItenger(at: codingPath + [index])
84+
}
85+
86+
/// See `UnkeyedDecodingContainer.decode`
87+
func decode(_ type: UInt16.Type) throws -> UInt16 {
88+
return try partialData.requireFixedWidthItenger(at: codingPath + [index])
89+
}
90+
91+
/// See `UnkeyedDecodingContainer.decode`
92+
func decode(_ type: UInt32.Type) throws -> UInt32 {
93+
return try partialData.requireFixedWidthItenger(at: codingPath + [index])
94+
}
95+
96+
/// See `UnkeyedDecodingContainer.decode`
97+
func decode(_ type: UInt64.Type) throws -> UInt64 {
98+
return try partialData.requireFixedWidthItenger(at: codingPath + [index])
99+
}
100+
101+
/// See `UnkeyedDecodingContainer.decode`
102+
func decode(_ type: Float.Type) throws -> Float {
103+
return try partialData.requireFloatingPoint(at: codingPath + [index])
104+
}
105+
106+
/// See `UnkeyedDecodingContainer.decode`
107+
func decode(_ type: Double.Type) throws -> Double {
108+
return try partialData.requireFloatingPoint(at: codingPath + [index])
109+
}
110+
111+
/// See `UnkeyedDecodingContainer.decode`
112+
func decode(_ type: String.Type) throws -> String {
113+
return try partialData.requireString(at: codingPath + [index])
114+
}
115+
116+
/// See `UnkeyedDecodingContainer.decode`
117+
func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
118+
let decoder = _PostgreSQLDataDecoder(partialData: partialData, at: codingPath + [index])
119+
return try T(from: decoder)
120+
}
121+
122+
/// See `UnkeyedDecodingContainer.nestedContainer`
123+
func nestedContainer<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key>
124+
where Key: CodingKey
125+
{
126+
let container = PostgreSQLDataKeyedDecodingContainer<Key>(partialData: partialData, at: codingPath + [index])
127+
return .init(container)
128+
}
129+
130+
/// See `UnkeyedDecodingContainer.nestedUnkeyedContainer`
131+
func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
132+
return PostgreSQLDataUnkeyedDecodingContainer(partialData: partialData, at: codingPath + [index])
133+
}
134+
135+
/// See `UnkeyedDecodingContainer.superDecoder`
136+
func superDecoder() throws -> Decoder {
137+
return _PostgreSQLDataDecoder(partialData: partialData, at: codingPath + [index])
138+
}
139+
}

Sources/PostgreSQL/Coders/PostgreSQLDataUnkeyedEncodingContainer.swift

-25
Original file line numberDiff line numberDiff line change
@@ -121,28 +121,3 @@ internal final class PostgreSQLDataUnkeyedEncodingContainer: UnkeyedEncodingCont
121121
return _PostgreSQLDataEncoder(partialData: partialData, at: codingPath + [index])
122122
}
123123
}
124-
125-
/// Represents an array index.
126-
fileprivate struct PostgreSQLDataArrayKey: CodingKey {
127-
/// See `CodingKey.intValue`
128-
var intValue: Int?
129-
130-
/// See `CodingKey.stringValue`
131-
var stringValue: String
132-
133-
/// See `CodingKey.init(stringValue:)`
134-
init?(stringValue: String) {
135-
return nil
136-
}
137-
138-
/// See `CodingKey.init(intValue:)`
139-
init?(intValue: Int) {
140-
self.init(intValue)
141-
}
142-
143-
/// Creates a new `PostgreSQLDataArrayKey` from the supplied index.
144-
init(_ index: Int) {
145-
self.intValue = index
146-
self.stringValue = index.description
147-
}
148-
}

0 commit comments

Comments
 (0)