https://github.com/krzysztofzablocki/Versionable
Migration for `Codable` objects.
https://github.com/krzysztofzablocki/Versionable
Last synced: 3 months ago
JSON representation
Migration for `Codable` objects.
- Host: GitHub
- URL: https://github.com/krzysztofzablocki/Versionable
- Owner: krzysztofzablocki
- License: mit
- Created: 2020-06-10T13:20:14.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-01-08T08:22:52.000Z (almost 5 years ago)
- Last Synced: 2025-07-08T12:42:56.429Z (3 months ago)
- Language: Swift
- Size: 37.1 KB
- Stars: 85
- Watchers: 5
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Versionable
Example showing 2 ways to implement migrations for `Codable` models.
[Related article](http://merowing.info/2020/06/adding-support-for-versioning-and-migration-to-your-codable-models./)
# Protocol conformance
Migrations in both ways are handled by conforming to `Versionable` protocol like so:
```swift
extension Object: Versionable {
enum Version: Int, VersionType {
case v1 = 1
case v2 = 2
case v3 = 3
}static func migrate(to: Version) -> Migration {
switch to {
case .v1:
return .none
case .v2:
return .migrate { payload in
payload["text"] = "defaultText"
}
case .v3:
return .migrate { payload in
payload["number"] = (payload["text"] as? String) == "defaultText" ? 1 : 200
}
}
}
}
```# Method 1
If you are only encoding flat structures you can use provided `VersionableDecoder` to get back your models, in this approach we rely on using a special decoder for the logic, instead of swift standards `JSONDecoder`.
- `+` Good for flat structures
- `+` Faster than container method
- `-` Won't work with composed tree hierarchy# Method 2
If you want to encode whole tree's of objects (versionable object A containing other versionable objects via composition) then you want to be able to use regular system encoders, for this scenario you can wrap your objects into `VersionableContainer`, custom implementation of that container will deal with all migration logic.
- `+` Works with any encoder
- `+` Supports arbitrary model tree setup
- `-` Slower