@@ -21,7 +21,7 @@ struct MySQLRowDecoder {
21
21
}
22
22
23
23
func container< Key> ( keyedBy type: Key . Type ) throws -> KeyedDecodingContainer < Key > where Key : CodingKey {
24
- return . init( _KeyedDecodingContainer ( row : row , table : table ) )
24
+ return . init( _KeyedDecodingContainer ( self ) )
25
25
}
26
26
27
27
func unkeyedContainer( ) throws -> UnkeyedDecodingContainer {
@@ -34,36 +34,31 @@ struct MySQLRowDecoder {
34
34
}
35
35
36
36
private struct _KeyedDecodingContainer < Key> : KeyedDecodingContainerProtocol where Key: CodingKey {
37
- let allKeys : [ Key ]
38
- let codingPath : [ CodingKey ] = [ ]
39
- let row : [ MySQLColumn : MySQLData ]
40
- let table : String ?
37
+ var allKeys : [ Key ] {
38
+ return [ ]
39
+ }
40
+ var codingPath : [ CodingKey ] {
41
+ return [ ]
42
+ }
43
+ let decoder : _Decoder
41
44
42
- init ( row: [ MySQLColumn : MySQLData ] , table: String ? ) {
43
- self . row = row
44
- self . table = table
45
- self . allKeys = row. keys. compactMap { col in
46
- if table == nil || col. table == table || col. table == nil {
47
- return col. name
48
- } else {
49
- return nil
50
- }
51
- } . compactMap ( Key . init ( stringValue: ) )
45
+ init ( _ decoder: _Decoder ) {
46
+ self . decoder = decoder
52
47
}
53
48
54
49
func contains( _ key: Key ) -> Bool {
55
- return allKeys . contains { $0 . stringValue == key . stringValue }
50
+ return true
56
51
}
57
52
58
53
func decodeNil( forKey key: Key ) throws -> Bool {
59
- guard let data = row. firstValue ( forColumn: key. stringValue, inTable: table) else {
54
+ guard let data = decoder . row. firstValue ( forColumn: key. stringValue, inTable: decoder . table) else {
60
55
return true
61
56
}
62
57
return data. isNull
63
58
}
64
59
65
60
func decode< T> ( _ type: T . Type , forKey key: Key ) throws -> T where T : Decodable {
66
- guard let data = row. firstValue ( forColumn: key. stringValue, inTable: table) else {
61
+ guard let data = decoder . row. firstValue ( forColumn: key. stringValue, inTable: decoder . table) else {
67
62
throw DecodingError . valueNotFound ( T . self, . init( codingPath: codingPath + [ key] , debugDescription: " Could not decode \( T . self) . " ) )
68
63
}
69
64
return try MySQLDataDecoder ( ) . decode ( T . self, from: data)
0 commit comments