-
Notifications
You must be signed in to change notification settings - Fork 34
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
Refactoring the ModelChange API to use a real model instead of optionals #48
Changes from all commits
05487c8
80f683c
694bf23
8b6ab38
f968b08
64ed1df
5171b73
9719b91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// © 2016 LinkedIn Corp. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
|
||
import Foundation | ||
|
||
/** | ||
This class is used to show which child models have changed. This object is usually in a dictionary: `[String: ModelChange]`. | ||
The strings represent IDs and it shows what's changed in this ID. Either the ID has been deleted or updated. | ||
If it's been updated, there are potentially several models that have updated if you are using projections. | ||
*/ | ||
public enum ModelChange: Equatable { | ||
/** | ||
This indicates a model has been updated and lists the new models. | ||
If you are using projections, there may be multiple models that represent this change. | ||
Otherwise, there will just be one model here. | ||
*/ | ||
case updated([ConsistencyManagerModel]) | ||
/** | ||
This indicates a model has been deleted. | ||
*/ | ||
case deleted | ||
|
||
public static func ==(lhs: ModelChange, rhs: ModelChange) -> Bool { | ||
switch (lhs, rhs) { | ||
case (.updated(let l), .updated(let r)): | ||
guard l.count == r.count else { | ||
return false | ||
} | ||
return zip(l, r).reduce(true) { isEqual, tuple in | ||
return isEqual && tuple.0.isEqualToModel(tuple.1) | ||
} | ||
case (.deleted, .deleted): | ||
return true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kind of weird that all deletes are considered equal. Maybe this data structure should capture the ID or the old model for deletes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is so you can do: I'm not sure it should capture the old ID. I think the problem here is that this captures the whole change. It doesn't, it only specifies that something has changed. For instance, updates don't capture the old model, only the new model. |
||
default: | ||
return false | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// © 2016 LinkedIn Corp. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
|
||
import XCTest | ||
import ConsistencyManager | ||
|
||
class ModelChangeTests: ConsistencyManagerTestCase { | ||
|
||
func testDeleteEquality() { | ||
XCTAssertEqual(ModelChange.deleted, ModelChange.deleted) | ||
XCTAssertNotEqual(ModelChange.deleted, ModelChange.updated([])) | ||
} | ||
|
||
func testUpdatedEquality() { | ||
let model = TestModel(id: "0", data: nil, children: [], requiredModel: TestRequiredModel(id: nil, data: 0)) | ||
let otherModel = TestModel(id: "1", data: nil, children: [], requiredModel: TestRequiredModel(id: nil, data: 0)) | ||
|
||
XCTAssertEqual(ModelChange.updated([model]), ModelChange.updated([model])) | ||
XCTAssertNotEqual(ModelChange.updated([model]), ModelChange.updated([otherModel])) | ||
XCTAssertNotEqual(ModelChange.updated([model]), ModelChange.updated([model, model])) | ||
XCTAssertNotEqual(ModelChange.updated([model]), ModelChange.deleted) | ||
} | ||
} |
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.
withUpdates
label seems out of date now. Should either match the type (e.g.withModelChanges
) or just bewith
since it now has a strong type (my preference is the latter).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.
Fixed