mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
RowConvertible.init(dictionary:)
This commit is contained in:
1 parent
d1e7af866d
commit
df2d4da51d
8 files changed
+236
-2
No files matched your search
@@ -3,6 +3,22 @@ Release Notes
|
||||
|
||||
## Next Release
|
||||
|
||||
**New**
|
||||
|
||||
- `RowConvertible` and `Record` get a dictionary initializer for free:
|
||||
|
||||
```swift
|
||||
class Person: Record { ... }
|
||||
let person = Person(dictionary: ["name": "Arthur", "age": 41])
|
||||
```
|
||||
|
||||
- Improved Foundation support:
|
||||
|
||||
```swift
|
||||
Row(dictionary: NSDictionary)
|
||||
Row.toDictionary() -> NSDictionary
|
||||
```
|
||||
|
||||
**Breaking Changes**
|
||||
|
||||
- `TraceFunction` is now defined as `(String) -> ()`
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
struct SimpleRowConvertible : RowConvertible {
|
||||
var firstName: String
|
||||
var lastName: String
|
||||
|
||||
init(row: Row) {
|
||||
firstName = row.value(named: "firstName")
|
||||
lastName = row.value(named: "lastName")
|
||||
}
|
||||
}
|
||||
|
||||
class RowConvertibleTests: GRDBTestCase {
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute("CREATE TABLE structs (firstName TEXT, lastName TEXT)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testRowInitializer() {
|
||||
let row = Row(dictionary: ["firstName": "Arthur", "lastName": "Martin"])
|
||||
let s = SimpleRowConvertible(row: row)
|
||||
XCTAssertEqual(s.firstName, "Arthur")
|
||||
XCTAssertEqual(s.lastName, "Martin")
|
||||
}
|
||||
|
||||
func testDictionaryInitializer() {
|
||||
let s = SimpleRowConvertible(dictionary: ["firstName": "Arthur", "lastName": "Martin"])
|
||||
XCTAssertEqual(s.firstName, "Arthur")
|
||||
XCTAssertEqual(s.lastName, "Martin")
|
||||
}
|
||||
|
||||
func testFetchFromDatabase() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute("INSERT INTO structs (firstName, lastName) VALUES (?, ?)", arguments: ["Arthur", "Martin"])
|
||||
let ss = SimpleRowConvertible.fetch(db, "SELECT * FROM structs")
|
||||
let s = Array(ss).first!
|
||||
XCTAssertEqual(s.firstName, "Arthur")
|
||||
XCTAssertEqual(s.lastName, "Martin")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testFetchAllFromDatabase() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute("INSERT INTO structs (firstName, lastName) VALUES (?, ?)", arguments: ["Arthur", "Martin"])
|
||||
let ss = SimpleRowConvertible.fetchAll(db, "SELECT * FROM structs")
|
||||
let s = ss.first!
|
||||
XCTAssertEqual(s.firstName, "Arthur")
|
||||
XCTAssertEqual(s.lastName, "Martin")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testFetchOneFromDatabase() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let missingS = SimpleRowConvertible.fetchOne(db, "SELECT * FROM structs")
|
||||
XCTAssertTrue(missingS == nil)
|
||||
|
||||
try db.execute("INSERT INTO structs (firstName, lastName) VALUES (?, ?)", arguments: ["Arthur", "Martin"])
|
||||
let s = SimpleRowConvertible.fetchOne(db, "SELECT * FROM structs")!
|
||||
XCTAssertEqual(s.firstName, "Arthur")
|
||||
XCTAssertEqual(s.lastName, "Martin")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testFetchFromStatement() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute("INSERT INTO structs (firstName, lastName) VALUES (?, ?)", arguments: ["Arthur", "Martin"])
|
||||
let statement = db.selectStatement("SELECT * FROM structs")
|
||||
let ss = SimpleRowConvertible.fetch(statement)
|
||||
let s = Array(ss).first!
|
||||
XCTAssertEqual(s.firstName, "Arthur")
|
||||
XCTAssertEqual(s.lastName, "Martin")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testFetchAllFromStatement() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute("INSERT INTO structs (firstName, lastName) VALUES (?, ?)", arguments: ["Arthur", "Martin"])
|
||||
let statement = db.selectStatement("SELECT * FROM structs")
|
||||
let ss = SimpleRowConvertible.fetchAll(statement)
|
||||
let s = ss.first!
|
||||
XCTAssertEqual(s.firstName, "Arthur")
|
||||
XCTAssertEqual(s.lastName, "Martin")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testFetchOneFromStatement() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let statement = db.selectStatement("SELECT * FROM structs")
|
||||
let missingS = SimpleRowConvertible.fetchOne(statement)
|
||||
XCTAssertTrue(missingS == nil)
|
||||
|
||||
try db.execute("INSERT INTO structs (firstName, lastName) VALUES (?, ?)", arguments: ["Arthur", "Martin"])
|
||||
let s = SimpleRowConvertible.fetchOne(statement)!
|
||||
XCTAssertEqual(s.firstName, "Arthur")
|
||||
XCTAssertEqual(s.lastName, "Martin")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
struct PointOfInterest : RowConvertible {
|
||||
let latitude: Double
|
||||
let longitude: Double
|
||||
let title: String?
|
||||
|
||||
init(row: Row) {
|
||||
latitude = row.value(named: "latitude")
|
||||
longitude = row.value(named: "longitude")
|
||||
title = row.value(named: "title")
|
||||
}
|
||||
}
|
||||
|
||||
class RowConvertibleFoundationTests: GRDBTestCase {
|
||||
|
||||
func testNSDictionaryInitializer() {
|
||||
let latitude = 41.8919300
|
||||
let longitude = 12.5113300
|
||||
let dictionary: NSDictionary = ["latitude": latitude, "longitude": longitude, "title": NSNull()]
|
||||
let s = PointOfInterest(dictionary: dictionary)
|
||||
XCTAssertEqual(s.latitude, latitude)
|
||||
XCTAssertEqual(s.longitude, longitude)
|
||||
XCTAssertTrue(s.title == nil)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -93,6 +93,17 @@ class RecordWithImmutablePropertyAndCustomInitializer : Record {
|
||||
}
|
||||
}
|
||||
|
||||
class RecordForDictionaryInitializerTest : Record {
|
||||
var firstName: String?
|
||||
var lastName: String?
|
||||
|
||||
override func updateFromRow(row: Row) {
|
||||
firstName = row.value(named: "firstName")
|
||||
lastName = row.value(named: "lastName")
|
||||
super.updateFromRow(row)
|
||||
}
|
||||
}
|
||||
|
||||
class RecordInitializersTests : GRDBTestCase {
|
||||
|
||||
func testFetchedRecordAreInitializedFromRow() {
|
||||
@@ -112,4 +123,10 @@ class RecordInitializersTests : GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDictionaryInitializer() {
|
||||
let record = RecordForDictionaryInitializerTest(dictionary: ["firstName": "Arthur", "lastName": "Martin"])
|
||||
XCTAssertEqual(record.firstName!, "Arthur")
|
||||
XCTAssertEqual(record.lastName!, "Martin")
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,12 @@
|
||||
5614DEF01BA9DA15003163B3 /* SQLiteStatementConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5614DEEF1BA9DA15003163B3 /* SQLiteStatementConvertible.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
5614DEF11BA9DA15003163B3 /* SQLiteStatementConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5614DEEF1BA9DA15003163B3 /* SQLiteStatementConvertible.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
5614DEF81BAA1B43003163B3 /* DatabaseValueConversionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A2381B1B9C74A90082EB20 /* DatabaseValueConversionTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
565B0FEF1BBC7D980098DE03 /* RowConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565B0FEE1BBC7D980098DE03 /* RowConvertibleTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
565B0FF01BBC7D980098DE03 /* RowConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565B0FEE1BBC7D980098DE03 /* RowConvertibleTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
565B0FF81BBC7FE90098DE03 /* RowConvertible+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565B0FF71BBC7FE90098DE03 /* RowConvertible+Foundation.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
565B0FF91BBC7FE90098DE03 /* RowConvertible+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565B0FF71BBC7FE90098DE03 /* RowConvertible+Foundation.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
565B0FFB1BBC80720098DE03 /* RowConvertible+FoundationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565B0FFA1BBC80720098DE03 /* RowConvertible+FoundationTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
565B0FFC1BBC80720098DE03 /* RowConvertible+FoundationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565B0FFA1BBC80720098DE03 /* RowConvertible+FoundationTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
565D5D681BBC604000DC9BD4 /* Row+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565D5D671BBC604000DC9BD4 /* Row+Foundation.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
565D5D691BBC604000DC9BD4 /* Row+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565D5D671BBC604000DC9BD4 /* Row+Foundation.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
565D5D711BBC694D00DC9BD4 /* Row+FoundationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565D5D701BBC694D00DC9BD4 /* Row+FoundationTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
@@ -282,6 +288,9 @@
|
||||
5607EFD21BB8254800605DE3 /* TransactionObserverTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionObserverTests.swift; sourceTree = "<group>"; };
|
||||
5614DEEF1BA9DA15003163B3 /* SQLiteStatementConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteStatementConvertible.swift; sourceTree = "<group>"; };
|
||||
5623E0901B4AFACC00B20B7F /* GRDBTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GRDBTestCase.swift; sourceTree = "<group>"; };
|
||||
565B0FEE1BBC7D980098DE03 /* RowConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowConvertibleTests.swift; sourceTree = "<group>"; };
|
||||
565B0FF71BBC7FE90098DE03 /* RowConvertible+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "RowConvertible+Foundation.swift"; sourceTree = "<group>"; };
|
||||
565B0FFA1BBC80720098DE03 /* RowConvertible+FoundationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "RowConvertible+FoundationTests.swift"; sourceTree = "<group>"; };
|
||||
565D5D671BBC604000DC9BD4 /* Row+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Row+Foundation.swift"; sourceTree = "<group>"; };
|
||||
565D5D701BBC694D00DC9BD4 /* Row+FoundationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Row+FoundationTests.swift"; sourceTree = "<group>"; };
|
||||
565D5D731BBC70AE00DC9BD4 /* StatementArguments+FoundationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "StatementArguments+FoundationTests.swift"; sourceTree = "<group>"; };
|
||||
@@ -488,6 +497,7 @@
|
||||
56A238AF1B9C90BB0082EB20 /* NSString.swift */,
|
||||
56E5891E1BBA5D5400CC0140 /* NSURL.swift */,
|
||||
565D5D671BBC604000DC9BD4 /* Row+Foundation.swift */,
|
||||
565B0FF71BBC7FE90098DE03 /* RowConvertible+Foundation.swift */,
|
||||
56A238A91B9C7E540082EB20 /* StatementArguments+Foundation.swift */,
|
||||
);
|
||||
path = Foundation;
|
||||
@@ -562,6 +572,7 @@
|
||||
56A2381F1B9C74A90082EB20 /* DetachedRowTests.swift */,
|
||||
56A2381E1B9C74A90082EB20 /* DictionaryRowTests.swift */,
|
||||
56FDECE11BB32DFD009AD709 /* MetalRowTests.swift */,
|
||||
565B0FEE1BBC7D980098DE03 /* RowConvertibleTests.swift */,
|
||||
);
|
||||
path = Row;
|
||||
sourceTree = "<group>";
|
||||
@@ -749,6 +760,7 @@
|
||||
56F0B98D1B6001C600A2F135 /* NSDateComponentsTests.swift */,
|
||||
56F0B98E1B6001C600A2F135 /* NSDateTests.swift */,
|
||||
565D5D701BBC694D00DC9BD4 /* Row+FoundationTests.swift */,
|
||||
565B0FFA1BBC80720098DE03 /* RowConvertible+FoundationTests.swift */,
|
||||
565D5D731BBC70AE00DC9BD4 /* StatementArguments+FoundationTests.swift */,
|
||||
);
|
||||
path = Foundation;
|
||||
@@ -1092,6 +1104,7 @@
|
||||
5614DEF11BA9DA15003163B3 /* SQLiteStatementConvertible.swift in Sources */,
|
||||
56A2387E1B9C75030082EB20 /* Database.swift in Sources */,
|
||||
56A238941B9C750B0082EB20 /* DatabaseMigrator.swift in Sources */,
|
||||
565B0FF91BBC7FE90098DE03 /* RowConvertible+Foundation.swift in Sources */,
|
||||
56A238AB1B9C7E540082EB20 /* StatementArguments+Foundation.swift in Sources */,
|
||||
56A2389E1B9C752B0082EB20 /* RowConvertible.swift in Sources */,
|
||||
56A238031B9C6A650082EB20 /* DatabaseDateComponents.swift in Sources */,
|
||||
@@ -1147,6 +1160,7 @@
|
||||
56CA21B81BB3F89F009A04C5 /* NSDataTests.swift in Sources */,
|
||||
56BB862A1BA988F2001F9168 /* FMDatabaseQueue.m in Sources */,
|
||||
56A238681B9C74A90082EB20 /* RecordInitializersTests.swift in Sources */,
|
||||
565B0FFC1BBC80720098DE03 /* RowConvertible+FoundationTests.swift in Sources */,
|
||||
56A2385E1B9C74A90082EB20 /* RecordCopyTests.swift in Sources */,
|
||||
56FC78691BBAEB8C00CA1285 /* ManagedDataControllerTests.swift in Sources */,
|
||||
56A238421B9C74A90082EB20 /* DatabaseValueConversionTests.swift in Sources */,
|
||||
@@ -1158,6 +1172,7 @@
|
||||
56CA22231BB41565009A04C5 /* PerformanceRecord.swift in Sources */,
|
||||
56A2386A1B9C74A90082EB20 /* RecordSubClassTests.swift in Sources */,
|
||||
56A2383E1B9C74A90082EB20 /* DatabaseValueTests.swift in Sources */,
|
||||
565B0FF01BBC7D980098DE03 /* RowConvertibleTests.swift in Sources */,
|
||||
56E8CE0E1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift in Sources */,
|
||||
56A238581B9C74A90082EB20 /* PrimaryKeyRowIDTests.swift in Sources */,
|
||||
56A238621B9C74A90082EB20 /* RecordEditedTests.swift in Sources */,
|
||||
@@ -1208,6 +1223,7 @@
|
||||
56A238671B9C74A90082EB20 /* RecordInitializersTests.swift in Sources */,
|
||||
56A2385D1B9C74A90082EB20 /* RecordCopyTests.swift in Sources */,
|
||||
56A238431B9C74A90082EB20 /* RawRepresentableTests.swift in Sources */,
|
||||
565B0FFB1BBC80720098DE03 /* RowConvertible+FoundationTests.swift in Sources */,
|
||||
56BB86271BA988F2001F9168 /* FMDatabasePool.m in Sources */,
|
||||
56FC78681BBAEB8C00CA1285 /* ManagedDataControllerTests.swift in Sources */,
|
||||
56CA22221BB41565009A04C5 /* PerformanceRecord.swift in Sources */,
|
||||
@@ -1219,6 +1235,7 @@
|
||||
56A238571B9C74A90082EB20 /* PrimaryKeyRowIDTests.swift in Sources */,
|
||||
56A238611B9C74A90082EB20 /* RecordEditedTests.swift in Sources */,
|
||||
56A2385B1B9C74A90082EB20 /* PrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
|
||||
565B0FEF1BBC7D980098DE03 /* RowConvertibleTests.swift in Sources */,
|
||||
56DAC70E1BB504400010FE01 /* FMDBPerformanceTests.swift in Sources */,
|
||||
56997A8E1B5390D4005EBEED /* DatabaseErrorFromStatementCreationTests.swift in Sources */,
|
||||
56A2383F1B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift in Sources */,
|
||||
@@ -1251,6 +1268,7 @@
|
||||
5614DEF01BA9DA15003163B3 /* SQLiteStatementConvertible.swift in Sources */,
|
||||
56A238051B9C6D670082EB20 /* NSDate.swift in Sources */,
|
||||
56A2387B1B9C75030082EB20 /* Configuration.swift in Sources */,
|
||||
565B0FF81BBC7FE90098DE03 /* RowConvertible+Foundation.swift in Sources */,
|
||||
56A2389B1B9C752B0082EB20 /* DatabaseValueConvertible.swift in Sources */,
|
||||
56A2380F1B9C74460082EB20 /* RawRepresentable.swift in Sources */,
|
||||
56A238AA1B9C7E540082EB20 /* StatementArguments+Foundation.swift in Sources */,
|
||||
|
||||
@@ -43,6 +43,16 @@ public protocol RowConvertible {
|
||||
|
||||
extension RowConvertible {
|
||||
|
||||
/**
|
||||
Dictionary initializer.
|
||||
|
||||
- parameter dictionary: A Dictionary.
|
||||
*/
|
||||
public init(dictionary: [String: DatabaseValueConvertible?]) {
|
||||
let row = Row(dictionary: dictionary)
|
||||
self.init(row: row)
|
||||
}
|
||||
|
||||
/// Default implementation, which does nothing.
|
||||
public func awakeFromFetch(row: Row) { }
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import Foundation
|
||||
|
||||
extension RowConvertible {
|
||||
|
||||
/**
|
||||
NSDictionary initializer.
|
||||
|
||||
- parameter dictionary: An NSDictionary.
|
||||
*/
|
||||
public init(dictionary: NSDictionary) {
|
||||
let row = Row(dictionary: dictionary)
|
||||
self.init(row: row)
|
||||
}
|
||||
}
|
||||
@@ -388,10 +388,10 @@ final class PointOfInterest : NSObject, MKAnnotation, RowConvertible {
|
||||
var title: String?
|
||||
|
||||
init(row: Row) {
|
||||
self.coordinate = CLLocationCoordinate2DMake(
|
||||
coordinate = CLLocationCoordinate2DMake(
|
||||
row.value(named: "latitude"),
|
||||
row.value(named: "longitude"))
|
||||
self.title = row.value(named: "title")
|
||||
title = row.value(named: "title")
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -404,6 +404,14 @@ PointOfInterest.fetchAll(db, "SELECT ...") // [PointOfInterest]
|
||||
PointOfInterest.fetchOne(db, "SELECT ...") // PointOfInterest?
|
||||
```
|
||||
|
||||
You also get a dictionary initializer for free:
|
||||
|
||||
```swift
|
||||
PointOfInterest(dictionary: [
|
||||
"latitude": 41.8919300,
|
||||
"longitude": 12.5113300,
|
||||
"title": "Rome"])
|
||||
```
|
||||
|
||||
See also the [Record](#records) class, which builds on top of RowConvertible and adds a few extra features like CRUD operations, and changes tracking.
|
||||
|
||||
@@ -1194,6 +1202,12 @@ dbQueue.inDatabase { db in
|
||||
> for person in persons { ... } // OK
|
||||
> ```
|
||||
|
||||
Oh, and you also get a dictionary initializer for free:
|
||||
|
||||
```swift
|
||||
let person = Person(dictionary: ["name": "Arthur", "age": 41])
|
||||
```
|
||||
|
||||
|
||||
#### Ad Hoc Subclasses
|
||||
|
||||
|
||||
Reference in new issue
Block a user