Last active
May 25, 2021 22:19
-
-
Save andreweades/190c93972d45ebd287a798f90add5df1 to your computer and use it in GitHub Desktop.
Support multiple Codable data format versions and easily migrate even the oldest saved data to the latest version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// How to make Codable versioning tolerable | |
// | |
// Watch the video: https://youtu.be/BOiFrlii13 | |
// | |
// Created by Andrew Eades on 25/05/2021. | |
// | |
// Run as a test in XCode | |
import XCTest | |
typealias Person = Person_V3 | |
struct Person_V1: Codable, Equatable { | |
var name = "" | |
init(name: String) { | |
self.name = name | |
} | |
} | |
struct Person_V2: Codable, Equatable { | |
var firstName = "" | |
var lastName = "" | |
init(firstName: String, lastName: String) { | |
self.firstName = firstName | |
self.lastName = lastName | |
} | |
init(previousVersion: Person_V1) { | |
let names = previousVersion.name.components(separatedBy: .whitespaces) | |
self.init(firstName: names.first!, lastName: names.last!) | |
} | |
} | |
struct Person_V3: Codable, Equatable { | |
var firstName = "" | |
var lastName = "" | |
var age: Int? | |
init(firstName: String, lastName: String, age: Int?) { | |
self.firstName = firstName | |
self.lastName = lastName | |
self.age = age | |
} | |
init(previousVersion: Person_V2) { | |
self.init(firstName: previousVersion.firstName, lastName: previousVersion.lastName, age: nil) | |
} | |
} | |
struct VersionSerialiser { | |
var v1_0_0: Person_V1? = nil | |
var v2_0_0: Person_V2? = nil | |
var v3_0_0: Person_V3? = nil | |
} | |
extension VersionSerialiser: Codable { | |
init(from decoder: Decoder) { | |
do { | |
let container = try decoder.container(keyedBy: CodingKeys.self) | |
if let person = try? container.decode(Person_V3.self, forKey: .v3_0_0) { | |
self.v3_0_0 = person | |
} else if let previousVersion = try? container.decode(Person_V2.self, forKey: .v2_0_0) { | |
self.v3_0_0 = Person_V3(previousVersion: previousVersion) | |
} else if let previousVersion = try? container.decode(Person_V1.self, forKey: .v1_0_0) { | |
self.v3_0_0 = Person_V3(previousVersion: Person_V2(previousVersion: previousVersion)) | |
} else { | |
XCTFail("Unknown version") | |
} | |
} catch { | |
XCTFail("Unknown version") | |
} | |
} | |
} | |
class Tests_macOS: XCTestCase { | |
var personV1: VersionSerialiser? | |
var dataV1: Data? | |
var jsonV1: String? | |
override func setUp() { | |
personV1 = VersionSerialiser(v1_0_0: Person_V1(name: "Andrew Eades")) | |
dataV1 = try! JSONEncoder().encode(personV1) | |
jsonV1 = String(data: dataV1!, encoding: .utf8) | |
} | |
func testMigration() { | |
let versions = try! JSONDecoder().decode(VersionSerialiser.self, from: dataV1!) | |
let person = versions.v3_0_0! | |
XCTAssertEqual(person, Person_V3(firstName: "Andrew", lastName: "Eades", age: nil)) | |
} | |
func testEncoding() { | |
let v1 = try! JSONEncoder().encode( | |
Person_V1(name: "Andrew Eades") | |
) | |
XCTAssertEqual(String(data: v1, encoding: .utf8)!, | |
#"{"name":"Andrew Eades"}"#) | |
let v2 = try! JSONEncoder().encode( | |
Person_V2(firstName: "Andrew", lastName: "Eades")) | |
XCTAssertEqual(String(data: v2, encoding: .utf8)!, | |
#"{"firstName":"Andrew","lastName":"Eades"}"#) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The video is here.