Skip to content

Commit 5741049

Browse files
committed
use json methods
1 parent 16f0c86 commit 5741049

File tree

2 files changed

+19
-32
lines changed

2 files changed

+19
-32
lines changed

Sources/MySQLKit/MySQLDataDecoder.swift

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
import Foundation
22

3-
#warning("TODO: move to codable kit")
4-
struct DecoderUnwrapper: Decodable {
3+
private struct DecoderUnwrapper: Decodable {
54
let decoder: Decoder
65
init(from decoder: Decoder) {
76
self.decoder = decoder
87
}
98
}
9+
private struct DoJSON: Error { }
1010

1111
public struct MySQLDataDecoder {
1212
public init() {}
1313

1414
public func decode<T>(_ type: T.Type, from data: MySQLData) throws -> T
1515
where T: Decodable
1616
{
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+
}
1828
}
1929

20-
#warning("TODO: finish implementing")
21-
2230
private final class _Decoder: Decoder {
2331
var codingPath: [CodingKey] {
2432
return []
@@ -37,12 +45,10 @@ public struct MySQLDataDecoder {
3745
fatalError()
3846
}
3947

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()
4652
}
4753

4854
func singleValueContainer() throws -> SingleValueDecodingContainer {
@@ -66,7 +72,7 @@ public struct MySQLDataDecoder {
6672
func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
6773
if let convertible = T.self as? MySQLDataConvertible.Type {
6874
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(
7076
codingPath: self.codingPath,
7177
debugDescription: "Could not convert from MySQL data: \(T.self)"
7278
))

Sources/MySQLKit/MySQLDataEncoder.swift

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,7 @@ public struct MySQLDataEncoder {
1212
try type.encode(to: encoder)
1313
return encoder.data
1414
} catch is DoJSON {
15-
let json = JSONEncoder()
16-
let data = try json.encode(Wrapper(type))
17-
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
18-
buffer.writeBytes(data)
19-
return MySQLData(
20-
type: .string,
21-
format: .text,
22-
buffer: buffer,
23-
isUnsigned: true
24-
)
15+
return try MySQLData(json: type)
2516
}
2617
}
2718
}
@@ -53,16 +44,6 @@ public struct MySQLDataEncoder {
5344
}
5445

5546
struct DoJSON: Error {}
56-
57-
struct Wrapper: Encodable {
58-
let encodable: Encodable
59-
init(_ encodable: Encodable) {
60-
self.encodable = encodable
61-
}
62-
func encode(to encoder: Encoder) throws {
63-
try self.encodable.encode(to: encoder)
64-
}
65-
}
6647

6748
private struct _KeyedValueEncoder<Key>: KeyedEncodingContainerProtocol where Key: CodingKey {
6849
var codingPath: [CodingKey] {

0 commit comments

Comments
 (0)