Skip to content

Commit be8566f

Browse files
authored
Merge pull request PerfectlySoft#51 from Bo98/int-conversion
Relax strictness on decoding to Int from different integer widths
2 parents c24ed7d + ee537ee commit be8566f

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

Sources/PerfectMySQL/MySQLCRUD.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ class MySQLCRUDRowReader<K : CodingKey>: KeyedDecodingContainerProtocol {
6060
return Int(i)
6161
case let i as Int32:
6262
return Int(i)
63+
case let i as Int16:
64+
return Int(i)
65+
case let i as Int8:
66+
return Int(i)
6367
case let i as Int:
6468
return i
6569
default:
66-
throw MySQLCRUDError("Could not convert \(String(describing: a)) into an Int.")
70+
throw MySQLCRUDError("Could not convert \(String(describing: a)) into an Int for key: \(key.stringValue)")
6771
}
6872
}
6973
func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 {
@@ -83,10 +87,16 @@ class MySQLCRUDRowReader<K : CodingKey>: KeyedDecodingContainerProtocol {
8387
switch a {
8488
case let i as UInt64:
8589
return UInt(i)
90+
case let i as UInt32:
91+
return UInt(i)
92+
case let i as UInt16:
93+
return UInt(i)
94+
case let i as UInt8:
95+
return UInt(i)
8696
case let i as UInt:
8797
return i
8898
default:
89-
throw MySQLCRUDError("Could not convert \(String(describing: a)) into an UInt.")
99+
throw MySQLCRUDError("Could not convert \(String(describing: a)) into an UInt for key: \(key.stringValue)")
90100
}
91101
}
92102
func decode(_ type: UInt8.Type, forKey key: Key) throws -> UInt8 {

Tests/PerfectMySQLTests/PerfectMySQLTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,25 @@ class PerfectMySQLTests: XCTestCase {
16761676
XCTFail("\(error)")
16771677
}
16781678
}
1679+
1680+
func testIntConversion() {
1681+
do {
1682+
let db = try getTestDB()
1683+
struct IntTest: Codable {
1684+
let id: Int
1685+
}
1686+
try db.sql("CREATE TABLE IntTest(id tinyint PRIMARY KEY)")
1687+
let table = db.table(IntTest.self)
1688+
let inserted = IntTest(id: 1)
1689+
try table.insert(inserted)
1690+
guard let selected = try db.table(IntTest.self).where(\IntTest.id == 1).first() else {
1691+
return XCTFail("Unable to find IntTest.")
1692+
}
1693+
XCTAssertEqual(selected.id, inserted.id)
1694+
} catch {
1695+
XCTFail("\(error)")
1696+
}
1697+
}
16791698

16801699
func testBespokeSQL() {
16811700
do {

0 commit comments

Comments
 (0)