Skip to content

Commit

Permalink
fix(datastore-v1): correct the filter predicate logic applied to opti…
Browse files Browse the repository at this point in the history
…onal fields
  • Loading branch information
5d committed Jan 27, 2024
1 parent 6ed4fc3 commit 671f0e9
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 65 deletions.
36 changes: 36 additions & 0 deletions Amplify/Categories/DataStore/Model/Internal/Persistable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ struct PersistableHelper {
return lhs == rhs
case let (lhs, rhs) as (String, String):
return lhs == rhs
case let (lhs, rhs) as (any EnumPersistable, String):
return lhs.rawValue == rhs
case let (lhs, rhs) as (String, any EnumPersistable):
return lhs == rhs.rawValue
case let (lhs, rhs) as (any EnumPersistable, any EnumPersistable):
return lhs.rawValue == rhs.rawValue
default:
return false
}
Expand Down Expand Up @@ -95,6 +101,12 @@ struct PersistableHelper {
return lhs == Double(rhs)
case let (lhs, rhs) as (String, String):
return lhs == rhs
case let (lhs, rhs) as (any EnumPersistable, String):
return lhs.rawValue == rhs
case let (lhs, rhs) as (String, any EnumPersistable):
return lhs == rhs.rawValue
case let (lhs, rhs) as (any EnumPersistable, any EnumPersistable):
return lhs.rawValue == rhs.rawValue
default:
return false
}
Expand Down Expand Up @@ -123,6 +135,12 @@ struct PersistableHelper {
return lhs <= Double(rhs)
case let (lhs, rhs) as (String, String):
return lhs <= rhs
case let (lhs, rhs) as (any EnumPersistable, String):
return lhs.rawValue <= rhs
case let (lhs, rhs) as (String, any EnumPersistable):
return lhs <= rhs.rawValue
case let (lhs, rhs) as (any EnumPersistable, any EnumPersistable):
return lhs.rawValue <= rhs.rawValue
default:
return false
}
Expand Down Expand Up @@ -151,6 +169,12 @@ struct PersistableHelper {
return lhs < Double(rhs)
case let (lhs, rhs) as (String, String):
return lhs < rhs
case let (lhs, rhs) as (any EnumPersistable, String):
return lhs.rawValue < rhs
case let (lhs, rhs) as (String, any EnumPersistable):
return lhs < rhs.rawValue
case let (lhs, rhs) as (any EnumPersistable, any EnumPersistable):
return lhs.rawValue < rhs.rawValue
default:
return false
}
Expand Down Expand Up @@ -179,6 +203,12 @@ struct PersistableHelper {
return lhs >= Double(rhs)
case let (lhs, rhs) as (String, String):
return lhs >= rhs
case let (lhs, rhs) as (any EnumPersistable, String):
return lhs.rawValue >= rhs
case let (lhs, rhs) as (String, any EnumPersistable):
return lhs >= rhs.rawValue
case let (lhs, rhs) as (any EnumPersistable, any EnumPersistable):
return lhs.rawValue >= rhs.rawValue
default:
return false
}
Expand Down Expand Up @@ -207,6 +237,12 @@ struct PersistableHelper {
return Double(lhs) > rhs
case let (lhs, rhs) as (String, String):
return lhs > rhs
case let (lhs, rhs) as (any EnumPersistable, String):
return lhs.rawValue > rhs
case let (lhs, rhs) as (String, any EnumPersistable):
return lhs > rhs.rawValue
case let (lhs, rhs) as (any EnumPersistable, any EnumPersistable):
return lhs.rawValue > rhs.rawValue
default:
return false
}
Expand Down
6 changes: 3 additions & 3 deletions Amplify/Categories/DataStore/Query/QueryOperator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public enum QueryOperator {
case beginsWith(_ value: String)

// swiftlint:disable:next cyclomatic_complexity
public func evaluate(target: Any) -> Bool {
public func evaluate(target: Any?) -> Bool {
switch self {
case .notEqual(let predicateValue):
return !PersistableHelper.isEqual(target, predicateValue)
Expand All @@ -34,14 +34,14 @@ public enum QueryOperator {
case .greaterThan(let predicateValue):
return PersistableHelper.isGreaterThan(target, predicateValue)
case .contains(let predicateString):
if let targetString = target as? String {
if let targetString = target.flatMap({ $0 as? String }) {
return targetString.contains(predicateString)
}
return false
case .between(let start, let end):
return PersistableHelper.isBetween(start, end, target)
case .beginsWith(let predicateValue):
if let targetString = target as? String {
if let targetString = target.flatMap({ $0 as? String }) {
return targetString.starts(with: predicateValue)
}
}
Expand Down
30 changes: 1 addition & 29 deletions Amplify/Categories/DataStore/Query/QueryPredicate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,34 +127,6 @@ public class QueryPredicateOperation: QueryPredicate {
}

public func evaluate(target: Model) -> Bool {
guard let fieldValue = target[field] else {
return false
}

guard let value = fieldValue else {
return false
}

if let booleanValue = value as? Bool {
return self.operator.evaluate(target: booleanValue)
}

if let doubleValue = value as? Double {
return self.operator.evaluate(target: doubleValue)
}

if let intValue = value as? Int {
return self.operator.evaluate(target: intValue)
}

if let timeValue = value as? Temporal.Time {
return self.operator.evaluate(target: timeValue)
}

if let enumValue = value as? EnumPersistable {
return self.operator.evaluate(target: enumValue.rawValue)
}

return self.operator.evaluate(target: value)
return self.operator.evaluate(target: target[field]?.flatMap { $0 })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class QueryPredicateEvaluateGeneratedBoolTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testBoolfalsenotEqualBooltrue() throws {
Expand Down Expand Up @@ -70,7 +70,7 @@ class QueryPredicateEvaluateGeneratedBoolTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testBooltrueequalsBooltrue() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class QueryPredicateEvaluateGeneratedDateTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testTemporalDateTemporal_Date_now_addvalue1to_daynotEqualTemporalDateTemporal_Date_now() throws {
Expand Down Expand Up @@ -109,7 +109,7 @@ class QueryPredicateEvaluateGeneratedDateTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testTemporalDateTemporal_Date_now_addvalue2to_daynotEqualTemporalDateTemporal_Date_now() throws {
Expand Down Expand Up @@ -158,7 +158,7 @@ class QueryPredicateEvaluateGeneratedDateTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testTemporalDateTemporal_Date_now_addvalue3to_daynotEqualTemporalDateTemporal_Date_now() throws {
Expand Down Expand Up @@ -207,7 +207,7 @@ class QueryPredicateEvaluateGeneratedDateTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testTemporalDateTemporal_Date_nowequalsTemporalDateTemporal_Date_now() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class QueryPredicateEvaluateGeneratedDateTimeTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testTemporalDateTimeTemporal_DateTime_now_addvalue1to_hournotEqualTemporalDateTimeTemporal_DateTime_now() throws {
Expand Down Expand Up @@ -120,7 +120,7 @@ class QueryPredicateEvaluateGeneratedDateTimeTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testTemporalDateTimeTemporal_DateTime_now_addvalue2to_hournotEqualTemporalDateTimeTemporal_DateTime_now() throws {
Expand Down Expand Up @@ -174,7 +174,7 @@ class QueryPredicateEvaluateGeneratedDateTimeTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testTemporalDateTimeTemporal_DateTime_now_addvalue3to_hournotEqualTemporalDateTimeTemporal_DateTime_now() throws {
Expand Down Expand Up @@ -228,7 +228,7 @@ class QueryPredicateEvaluateGeneratedDateTimeTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testTemporalDateTimeTemporal_DateTime_nowequalsTemporalDateTimeTemporal_DateTime_now() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class QueryPredicateEvaluateGeneratedDoubleIntTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testDouble2_1notEqualInt1() throws {
Expand Down Expand Up @@ -89,7 +89,7 @@ class QueryPredicateEvaluateGeneratedDoubleIntTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testDouble3_1notEqualInt1() throws {
Expand Down Expand Up @@ -128,7 +128,7 @@ class QueryPredicateEvaluateGeneratedDoubleIntTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testDouble1notEqualInt1() throws {
Expand Down Expand Up @@ -167,7 +167,7 @@ class QueryPredicateEvaluateGeneratedDoubleIntTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testDouble2notEqualInt1() throws {
Expand Down Expand Up @@ -206,7 +206,7 @@ class QueryPredicateEvaluateGeneratedDoubleIntTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testDouble3notEqualInt1() throws {
Expand Down Expand Up @@ -245,7 +245,7 @@ class QueryPredicateEvaluateGeneratedDoubleIntTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testDouble1_1equalsInt1() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class QueryPredicateEvaluateGeneratedDoubleTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testDouble2_1notEqualDouble1_1() throws {
Expand Down Expand Up @@ -149,7 +149,7 @@ class QueryPredicateEvaluateGeneratedDoubleTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testDouble3_1notEqualDouble1_1() throws {
Expand Down Expand Up @@ -218,7 +218,7 @@ class QueryPredicateEvaluateGeneratedDoubleTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testDouble1notEqualDouble1_1() throws {
Expand Down Expand Up @@ -287,7 +287,7 @@ class QueryPredicateEvaluateGeneratedDoubleTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testDouble2notEqualDouble1_1() throws {
Expand Down Expand Up @@ -356,7 +356,7 @@ class QueryPredicateEvaluateGeneratedDoubleTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testDouble3notEqualDouble1_1() throws {
Expand Down Expand Up @@ -425,7 +425,7 @@ class QueryPredicateEvaluateGeneratedDoubleTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testDouble1_1equalsDouble1_1() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class QueryPredicateEvaluateGeneratedIntBetweenTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testInt2notEqualInt1() throws {
Expand Down Expand Up @@ -93,7 +93,7 @@ class QueryPredicateEvaluateGeneratedIntBetweenTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testInt3notEqualInt1() throws {
Expand Down Expand Up @@ -132,7 +132,7 @@ class QueryPredicateEvaluateGeneratedIntBetweenTests: XCTestCase {

let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)

XCTAssertFalse(evaluation)
XCTAssertTrue(evaluation)
}

func testInt1equalsInt1() throws {
Expand Down
Loading

0 comments on commit 671f0e9

Please sign in to comment.