Skip to content

Commit

Permalink
amended the documentation and tests to include Dictionary support to …
Browse files Browse the repository at this point in the history
…Codable types
  • Loading branch information
Vlad Alexa committed Aug 15, 2018
1 parent 0cdc7d8 commit 5f41c57
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2567,19 +2567,19 @@ struct Link : PersistableRecord {

## Codable Records

[Swift Archival & Serialization](https://github.com/apple/swift-evolution/blob/master/proposals/0166-swift-archival-serialization.md) was introduced with Swift 4, GRDB supports all Codable conforming types with the exception of Dictionaries.
[Swift Archival & Serialization](https://github.com/apple/swift-evolution/blob/master/proposals/0166-swift-archival-serialization.md) was introduced with Swift 4, GRDB supports all Codable conforming types

GRDB provides default implementations for [`FetchableRecord.init(row:)`](#fetchablerecord-protocol) and [`PersistableRecord.encode(to:)`](#persistablerecord-protocol) for record types that also adopt an archival protocol (`Codable`, `Encodable` or `Decodable`). When all their properties are themselves codable, Swift generates the archiving methods, and you don't need to write them down:

```swift
// Declare a Codable struct or class, nested Codable objects as well as Sets, Arrays, and Optionals are supported with the exception of Dictionaries
// Declare a Codable struct or class, nested Codable objects as well as Sets, Arrays, and Optionals are supported
struct Player: Codable {
let name: String
let score: Int
let scores: [Int]
let lastMedal: PlayerMedal
let medals: [PlayerMedal]
//let timeline: [String: PlayerMedal] // <- Conforms to Codable but is not supported by GRDB
//let timeline: [String: PlayerMedal]
}

// A simple Codable that will be nested in a parent Codable
Expand Down
19 changes: 13 additions & 6 deletions Tests/GRDBTests/FetchableRecordDecodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ extension FetchableRecordDecodableTests {
let scores: [Int]
let lastMedal: PlayerMedal
let medals: [PlayerMedal]
//let timeline: [String: PlayerMedal] // <- Conforms to Codable but is not supported by GRDB
let timeline: [String: PlayerMedal]
}

// A simple Codable that will be nested in a parent Codable
Expand All @@ -523,28 +523,35 @@ extension FetchableRecordDecodableTests {
t.column("scores", .integer)
t.column("lastMedal", .text)
t.column("medals", .text)
//t.column("timeline", .text)
t.column("timeline", .text)
}

let medal1 = PlayerMedal(name: "First", type: "Gold")
let medal2 = PlayerMedal(name: "Second", type: "Silver")
//let timeline = ["Local Contest":medal1, "National Contest":medal2]
let value = Player(name: "PlayerName", score: 10, scores: [1,2,3,4,5], lastMedal: medal1, medals: [medal1, medal2])
let timeline = ["Local Contest": medal1, "National Contest": medal2]
let value = Player(name: "PlayerName", score: 10, scores: [1,2,3,4,5], lastMedal: medal1, medals: [medal1, medal2], timeline: timeline)
try value.insert(db)

let parentModel = try Player.fetchAll(db)

guard let arrayOfNestedModel = parentModel.first?.medals, let firstNestedModelInArray = arrayOfNestedModel.first, let secondNestedModelInArray = arrayOfNestedModel.last else {
guard let first = parentModel.first, let firstNestedModelInArray = first.medals.first, let secondNestedModelInArray = first.medals.last else {
XCTFail()
return
}

// Check there are two models in array
XCTAssertTrue(arrayOfNestedModel.count == 2)
XCTAssertTrue(first.medals.count == 2)

// Check the nested model contains the expected values of first and last name
XCTAssertEqual(firstNestedModelInArray.name, "First")
XCTAssertEqual(secondNestedModelInArray.name, "Second")

XCTAssertEqual(first.name, "PlayerName")
XCTAssertEqual(first.score, 10)
XCTAssertEqual(first.scores, [1,2,3,4,5])
XCTAssertEqual(first.lastMedal.name, medal1.name)
XCTAssertEqual(first.timeline["Local Contest"]?.name, medal1.name)
XCTAssertEqual(first.timeline["National Contest"]?.name, medal2.name)
}

}
Expand Down

0 comments on commit 5f41c57

Please sign in to comment.