-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix custom to fully support Codable protocol when using SwiftData #5
Open
t-p-dev
wants to merge
2
commits into
CreateAPI:main
Choose a base branch
from
t-p-dev:fix-decode-from-json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,110 @@ | ||
import XCTest | ||
|
||
#if canImport(SwiftData) | ||
import SwiftData | ||
#endif | ||
|
||
import NaiveDate | ||
|
||
@available(iOS 17, *) | ||
@Model class ModelWithDates: Codable { | ||
var date: NaiveDate | ||
var time: NaiveTime | ||
var dateTime: NaiveDateTime | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case date | ||
case time | ||
case dateTime | ||
} | ||
|
||
init(date: NaiveDate, time: NaiveTime, dateTime: NaiveDateTime) { | ||
self.date = date | ||
self.time = time | ||
self.dateTime = dateTime | ||
} | ||
|
||
required init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
date = try container.decode(NaiveDate.self, forKey: .date) | ||
time = try container.decode(NaiveTime.self, forKey: .time) | ||
dateTime = try container.decode(NaiveDateTime.self, forKey: .dateTime) | ||
} | ||
|
||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(date, forKey: .date) | ||
try container.encode(time, forKey: .time) | ||
try container.encode(dateTime, forKey: .dateTime) | ||
} | ||
} | ||
|
||
@available(iOS 17, *) | ||
class SwiftDataTests : XCTestCase { | ||
func createModelContainer() throws -> ModelContainer { | ||
let schema = Schema([ModelWithDates.self]) | ||
return try ModelContainer( | ||
for: schema, | ||
configurations: [ | ||
ModelConfiguration(schema: schema, isStoredInMemoryOnly: true) | ||
] | ||
) | ||
} | ||
|
||
var modelContainer: ModelContainer! | ||
|
||
override func setUp() { | ||
modelContainer = try! createModelContainer() | ||
} | ||
|
||
@MainActor | ||
func testSwiftDataCompositeEncodeDecode() throws { | ||
|
||
let data = """ | ||
{ | ||
"dateTime":"2024-02-01T10:09:08", | ||
"time": "11:12:13", | ||
"date": "2025-12-24" | ||
} | ||
""".data(using: .utf8)! | ||
|
||
let model = try! JSONDecoder().decode(ModelWithDates.self, from: data) | ||
|
||
let expectedDateTime = NaiveDateTime(date: NaiveDate(year: 2024, month: 2, day: 1), time: NaiveTime(hour: 10, minute: 9, second: 8)) | ||
let expectedDate = NaiveDate(year: 2025, month: 12, day: 24) | ||
let expectedTime = NaiveTime(hour: 11, minute: 12, second: 13) | ||
|
||
XCTAssertEqual(model.dateTime, expectedDateTime) | ||
XCTAssertEqual(model.date, expectedDate) | ||
XCTAssertEqual(model.time, expectedTime) | ||
|
||
modelContainer.mainContext.insert(model) | ||
|
||
/// Save to persistent store | ||
try modelContainer.mainContext.save() | ||
|
||
let exp = expectation(description: "Background task finished") | ||
|
||
/// Ensure we fetch object directly from persistent store and not reusing in-memory one | ||
/// It is ensured through creating a detached task, which forces non-main actor queue | ||
/// And so forces a ModelContext to use other thread | ||
Task.detached { | ||
/// Background context | ||
let otherContext = ModelContext(self.modelContainer) | ||
|
||
/// Fetching our model | ||
var fetchDescriptor = FetchDescriptor<ModelWithDates>() | ||
fetchDescriptor.fetchLimit = 1 | ||
let fetchedModel = try otherContext.fetch(fetchDescriptor)[0] | ||
|
||
/// Ensuring data persisted and transformed properly | ||
XCTAssertEqual(fetchedModel.dateTime, expectedDateTime) | ||
XCTAssertEqual(fetchedModel.date, expectedDate) | ||
XCTAssertEqual(fetchedModel.time, expectedTime) | ||
|
||
exp.fulfill() | ||
} | ||
|
||
wait(for: [exp], timeout: 1) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to implement it without hardcoding "SwiftData.CompositeEncoder"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, I didn't have a time to find a way just yet