-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/no-codable-box-1' into develop
- Loading branch information
Showing
5 changed files
with
70 additions
and
113 deletions.
There are no files selected for viewing
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
86 changes: 0 additions & 86 deletions
86
Sources/ManagedModels/SchemaCompatibility/CodableBox.swift
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
Sources/ManagedModels/SchemaCompatibility/CodableTransformer.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// Created by Helge Heß. | ||
// Copyright © 2023 ZeeZide GmbH. | ||
// | ||
|
||
import Foundation | ||
import CoreData | ||
|
||
final class CodableTransformer<T: Codable>: ValueTransformer { | ||
|
||
#if false | ||
override class func transformedValueClass() -> AnyClass { | ||
T.self // doesn't work | ||
} | ||
#endif | ||
override class func allowsReverseTransformation() -> Bool { true } | ||
|
||
override func transformedValue(_ value: Any?) -> Any? { | ||
// value is the box | ||
guard let value else { return nil } | ||
guard let typed = value as? T else { | ||
assertionFailure("Value to be transformed is not the right type? \(value)") | ||
return nil | ||
} | ||
do { | ||
return try JSONEncoder().encode(typed) | ||
} | ||
catch { | ||
assertionFailure("Could not encode JSON value of property? \(error)") | ||
return nil | ||
} | ||
} | ||
|
||
override func reverseTransformedValue(_ value: Any?) -> Any? { | ||
guard let value else { return nil } | ||
guard let data = value as? Data else { | ||
assert(value is Data, "Reverse value is not `Data`?") | ||
return nil | ||
} | ||
do { | ||
return try JSONDecoder().decode(T.self, from: data) | ||
} | ||
catch { | ||
assertionFailure("Could not decode JSON value of property? \(error)") | ||
return nil | ||
} | ||
} | ||
} |
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
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