1
1
import Foundation
2
2
3
- #warning("TODO: move to codable kit")
4
- struct DecoderUnwrapper : Decodable {
3
+ private struct DecoderUnwrapper : Decodable {
5
4
let decoder : Decoder
6
5
init ( from decoder: Decoder ) {
7
6
self . decoder = decoder
8
7
}
9
8
}
9
+ private struct DoJSON : Error { }
10
10
11
11
public struct MySQLDataDecoder {
12
12
public init ( ) { }
13
13
14
14
public func decode< T> ( _ type: T . Type , from data: MySQLData ) throws -> T
15
15
where T: Decodable
16
16
{
17
- return try T . init ( from: _Decoder ( data: data) )
17
+ do {
18
+ return try T . init ( from: _Decoder ( data: data) )
19
+ } catch is DoJSON {
20
+ guard let value = try data. json ( as: T . self) else {
21
+ throw DecodingError . typeMismatch ( T . self, DecodingError . Context. init (
22
+ codingPath: [ ] ,
23
+ debugDescription: " Could not convert from MySQL data: \( T . self) "
24
+ ) )
25
+ }
26
+ return value
27
+ }
18
28
}
19
29
20
- #warning("TODO: finish implementing")
21
-
22
30
private final class _Decoder : Decoder {
23
31
var codingPath : [ CodingKey ] {
24
32
return [ ]
@@ -37,12 +45,10 @@ public struct MySQLDataDecoder {
37
45
fatalError ( )
38
46
}
39
47
40
- func container< Key> ( keyedBy type: Key . Type ) throws -> KeyedDecodingContainer < Key > where Key : CodingKey {
41
- #warning("TODO: use NIOFoundationCompat")
42
- var buffer = self . data. buffer!
43
- let data = buffer. readBytes ( length: buffer. readableBytes) !
44
- let unwrapper = try JSONDecoder ( ) . decode ( DecoderUnwrapper . self, from: Data ( data) )
45
- return try unwrapper. decoder. container ( keyedBy: Key . self)
48
+ func container< Key> ( keyedBy type: Key . Type ) throws -> KeyedDecodingContainer < Key >
49
+ where Key : CodingKey
50
+ {
51
+ throw DoJSON ( )
46
52
}
47
53
48
54
func singleValueContainer( ) throws -> SingleValueDecodingContainer {
@@ -66,7 +72,7 @@ public struct MySQLDataDecoder {
66
72
func decode< T> ( _ type: T . Type ) throws -> T where T : Decodable {
67
73
if let convertible = T . self as? MySQLDataConvertible . Type {
68
74
guard let value = convertible. init ( mysqlData: self . decoder. data) else {
69
- throw DecodingError . typeMismatch ( convertible , DecodingError . Context. init (
75
+ throw DecodingError . typeMismatch ( T . self , DecodingError . Context. init (
70
76
codingPath: self . codingPath,
71
77
debugDescription: " Could not convert from MySQL data: \( T . self) "
72
78
) )
0 commit comments