Skip to content
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

RCOCOA-2271: Collections in Mixed #8546

Merged
merged 12 commits into from
Jun 6, 2024
13 changes: 7 additions & 6 deletions Realm/RLMQueryUtil.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1866,17 +1866,18 @@ bool is_self_value_for_key_path_function_expression(NSExpression *expression)
paths.push_back(PathElement{[(NSNumber *)value intValue]});
} else if ([value isKindOfClass:[NSString class]]) {
NSString *key = (NSString *)value;
if ([key isEqual:@"#any"]) {
paths.emplace_back();
} else {
paths.push_back(PathElement{key.UTF8String});
}
paths.push_back(PathElement{key.UTF8String});
} else {
throwException(@"Invalid subscript type",
@"Invalid subscript type '%@': Only `Strings` or index are allowed subscripts", expression);
}
} else if (expression.arguments[i].expressionType == NSKeyPathExpressionType) {
nestedKeyPath = [(id)expression.arguments[i] predicateFormat];
auto keyPath = [(id)expression.arguments[i] predicateFormat];
if ([keyPath isEqual:@"#any"]) {
paths.emplace_back();
} else {
nestedKeyPath = keyPath;
}
} else {
throwException(@"Invalid expression type",
@"Invalid expression type '%@': Subscripts queries don't allow any other expression types", expression);
Expand Down
3 changes: 2 additions & 1 deletion RealmSwift/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,8 @@ private func buildPredicate(_ root: QueryNode, subqueryCount: Int = 0) -> (Strin
case .mapAnySubscripts(let keyPath, let keys):
build(keyPath)
for key in keys {
formatStr.append("[%@]")
let arg = (key as? String == "#any") ? "[%K]" : "[%@]"
dianaafanador3 marked this conversation as resolved.
Show resolved Hide resolved
formatStr.append(arg)
arguments.add(key)
}
case .geoWithin(let keyPath, let value):
Expand Down
2 changes: 1 addition & 1 deletion RealmSwift/RealmCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public protocol _RealmMapValue {
public mutating func next() -> Element? {
let next = generatorBase.next()
if let key = next as? Key,
let value = collection[key as AnyObject].map(Value._rlmFromObjc(_:)), let value {
let value = collection[key as AnyObject].map(Value._rlmFromObjc(_:)), let value {
return (key: key, value: value)
}
return nil
Expand Down
18 changes: 9 additions & 9 deletions RealmSwift/Tests/QueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ class QueryTests: TestCase {
$0.anyCol[1] == .none
}

assertQuery("(anyCol[%@] == %@)", values: ["#any", AnyRealmValue.none], count: 1) {
assertQuery("(anyCol[%K] == %@)", values: ["#any", AnyRealmValue.none], count: 1) {
$0.anyCol.any == .none
}

Expand All @@ -1216,7 +1216,7 @@ class QueryTests: TestCase {
$0.anyCol[0][0][1] == .double(76.54)
}

assertQuery("(anyCol[%@][%@][%@] == %@)", values: [0, 0, "#any", AnyRealmValue.double(76.54)], count: 1) {
assertQuery("(anyCol[%@][%@][%K] == %@)", values: [0, 0, "#any", AnyRealmValue.double(76.54)], count: 1) {
$0.anyCol[0][0].any == .double(76.54)
}

Expand All @@ -1228,11 +1228,11 @@ class QueryTests: TestCase {
$0.anyCol[0][0][0][1] == .bool(false)
}

assertQuery("(anyCol[%@][%@][%@][%@] == %@)", values: [0, 0, 0, "#any", AnyRealmValue.string("john")], count: 1) {
assertQuery("(anyCol[%@][%@][%@][%K] == %@)", values: [0, 0, 0, "#any", AnyRealmValue.string("john")], count: 1) {
$0.anyCol[0][0][0].any == .string("john")
}

assertQuery("(anyCol[%@][%@][%@][%@] == %@)", values: [0, 0, 0, "#any", AnyRealmValue.bool(false)], count: 1) {
assertQuery("(anyCol[%@][%@][%@][%K] == %@)", values: [0, 0, 0, "#any", AnyRealmValue.bool(false)], count: 1) {
$0.anyCol[0][0][0].any == .bool(false)
}

Expand Down Expand Up @@ -1263,23 +1263,23 @@ class QueryTests: TestCase {
$0.anyCol["key1"] == .none
}

assertQuery("(anyCol[%@] == %@)", values: ["#any", AnyRealmValue.none], count: 1) {
assertQuery("(anyCol[%K] == %@)", values: ["#any", AnyRealmValue.none], count: 1) {
$0.anyCol.any == .none
}

assertQuery("(anyCol[%@][%@] == %@)", values: ["key0", "key3", AnyRealmValue.int(99)], count: 1) {
$0.anyCol["key0"]["key3"] == .int(99)
}

assertQuery("(anyCol[%@][%@] == %@)", values: ["key0", "#any", AnyRealmValue.int(99)], count: 1) {
assertQuery("(anyCol[%@][%K] == %@)", values: ["key0", "#any", AnyRealmValue.int(99)], count: 1) {
$0.anyCol["key0"].any == .int(99)
}

assertQuery("(anyCol[%@][%@][%@] == %@)", values: ["key0", "key2", "key5", AnyRealmValue.double(76.54)], count: 1) {
$0.anyCol["key0"]["key2"]["key5"] == .double(76.54)
}

assertQuery("(anyCol[%@][%@][%@] == %@)", values: ["key0", "key2", "#any", AnyRealmValue.double(76.54)], count: 1) {
assertQuery("(anyCol[%@][%@][%K] == %@)", values: ["key0", "key2", "#any", AnyRealmValue.double(76.54)], count: 1) {
$0.anyCol["key0"]["key2"].any == .double(76.54)
}

Expand All @@ -1291,11 +1291,11 @@ class QueryTests: TestCase {
$0.anyCol["key0"]["key2"]["key4"]["key7"] == .bool(false)
}

assertQuery("(anyCol[%@][%@] >= %@)", values: ["key0", "#any", AnyRealmValue.int(99)], count: 1) {
assertQuery("(anyCol[%@][%K] >= %@)", values: ["key0", "#any", AnyRealmValue.int(99)], count: 1) {
$0.anyCol["key0"].any >= .int(99)
}

assertQuery("(anyCol[%@][%@] <= %@)", values: ["key0", "#any", AnyRealmValue.int(99)], count: 1) {
assertQuery("(anyCol[%@][%K] <= %@)", values: ["key0", "#any", AnyRealmValue.int(99)], count: 1) {
$0.anyCol["key0"].any <= .int(99)
}

Expand Down
18 changes: 9 additions & 9 deletions RealmSwift/Tests/QueryTests.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ class QueryTests: TestCase {
$0.anyCol[1] == .none
}

assertQuery("(anyCol[%@] == %@)", values: ["#any", AnyRealmValue.none], count: 1) {
assertQuery("(anyCol[%K] == %@)", values: ["#any", AnyRealmValue.none], count: 1) {
$0.anyCol.any == .none
}

Expand All @@ -527,7 +527,7 @@ class QueryTests: TestCase {
$0.anyCol[0][0][1] == .double(76.54)
}

assertQuery("(anyCol[%@][%@][%@] == %@)", values: [0, 0, "#any", AnyRealmValue.double(76.54)], count: 1) {
assertQuery("(anyCol[%@][%@][%K] == %@)", values: [0, 0, "#any", AnyRealmValue.double(76.54)], count: 1) {
$0.anyCol[0][0].any == .double(76.54)
}

Expand All @@ -539,11 +539,11 @@ class QueryTests: TestCase {
$0.anyCol[0][0][0][1] == .bool(false)
}

assertQuery("(anyCol[%@][%@][%@][%@] == %@)", values: [0, 0, 0, "#any", AnyRealmValue.string("john")], count: 1) {
assertQuery("(anyCol[%@][%@][%@][%K] == %@)", values: [0, 0, 0, "#any", AnyRealmValue.string("john")], count: 1) {
$0.anyCol[0][0][0].any == .string("john")
}

assertQuery("(anyCol[%@][%@][%@][%@] == %@)", values: [0, 0, 0, "#any", AnyRealmValue.bool(false)], count: 1) {
assertQuery("(anyCol[%@][%@][%@][%K] == %@)", values: [0, 0, 0, "#any", AnyRealmValue.bool(false)], count: 1) {
$0.anyCol[0][0][0].any == .bool(false)
}

Expand Down Expand Up @@ -574,23 +574,23 @@ class QueryTests: TestCase {
$0.anyCol["key1"] == .none
}

assertQuery("(anyCol[%@] == %@)", values: ["#any", AnyRealmValue.none], count: 1) {
assertQuery("(anyCol[%K] == %@)", values: ["#any", AnyRealmValue.none], count: 1) {
$0.anyCol.any == .none
}

assertQuery("(anyCol[%@][%@] == %@)", values: ["key0", "key3", AnyRealmValue.int(99)], count: 1) {
$0.anyCol["key0"]["key3"] == .int(99)
}

assertQuery("(anyCol[%@][%@] == %@)", values: ["key0", "#any", AnyRealmValue.int(99)], count: 1) {
assertQuery("(anyCol[%@][%K] == %@)", values: ["key0", "#any", AnyRealmValue.int(99)], count: 1) {
$0.anyCol["key0"].any == .int(99)
}

assertQuery("(anyCol[%@][%@][%@] == %@)", values: ["key0", "key2", "key5", AnyRealmValue.double(76.54)], count: 1) {
$0.anyCol["key0"]["key2"]["key5"] == .double(76.54)
}

assertQuery("(anyCol[%@][%@][%@] == %@)", values: ["key0", "key2", "#any", AnyRealmValue.double(76.54)], count: 1) {
assertQuery("(anyCol[%@][%@][%K] == %@)", values: ["key0", "key2", "#any", AnyRealmValue.double(76.54)], count: 1) {
$0.anyCol["key0"]["key2"].any == .double(76.54)
}

Expand All @@ -602,11 +602,11 @@ class QueryTests: TestCase {
$0.anyCol["key0"]["key2"]["key4"]["key7"] == .bool(false)
}

assertQuery("(anyCol[%@][%@] >= %@)", values: ["key0", "#any", AnyRealmValue.int(99)], count: 1) {
assertQuery("(anyCol[%@][%K] >= %@)", values: ["key0", "#any", AnyRealmValue.int(99)], count: 1) {
$0.anyCol["key0"].any >= .int(99)
}

assertQuery("(anyCol[%@][%@] <= %@)", values: ["key0", "#any", AnyRealmValue.int(99)], count: 1) {
assertQuery("(anyCol[%@][%K] <= %@)", values: ["key0", "#any", AnyRealmValue.int(99)], count: 1) {
$0.anyCol["key0"].any <= .int(99)
}

Expand Down
Loading