Skip to content

Commit

Permalink
Solve PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaafanador3 committed May 22, 2024
1 parent 7d5023f commit 2a3861f
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 171 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ store. Xcode 15.1 is now the minimum supported version.
* The `transferredBytes` and `transferrableBytes` fields on `Progress` have been deprecated
in favor of `progressEstimate` which is a value between 0.0 and 1.0 indicating the estimated
progress toward the upload/download transfer. ([#8476](https://github.com/realm/realm-swift/issues/8476))
UUID/ObjectId types. ([.Net * #3566](https://github.com/realm/realm-dotnet/issues/3566))
* Added `SyncConfiguration.initialSubscriptions` which describes the initial subscription configuration that was passed when constructing the `SyncConfiguration`. ([#8548](https://github.com/realm/realm-swift/issues/8548))

### Fixed
* `-[RLMUser allSessions]` did not include sessions which were currently
Expand Down
4 changes: 2 additions & 2 deletions Realm/RLMAccessor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -777,13 +777,13 @@ id RLMDynamicGetByName(__unsafe_unretained RLMObjectBase *const obj,
}

RLMArray *RLMGetSwiftPropertyArray(__unsafe_unretained RLMObjectBase *const obj, uint16_t key) {
return getCollection(obj, key);
return (RLMArray *)getCollection(obj, key);
}
RLMSet *RLMGetSwiftPropertySet(__unsafe_unretained RLMObjectBase *const obj, uint16_t key) {
return getCollection(obj, key);
}
RLMDictionary *RLMGetSwiftPropertyMap(__unsafe_unretained RLMObjectBase *const obj, uint16_t key) {
return getCollection(obj, key);
return (RLMDictionary *)getCollection(obj, key);
}

void RLMSetSwiftPropertyNil(__unsafe_unretained RLMObjectBase *const obj, uint16_t key) {
Expand Down
24 changes: 13 additions & 11 deletions Realm/RLMQueryUtil.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1849,10 +1849,11 @@ bool is_self_value_for_key_path_function_expression(NSExpression *expression)
}
}

// This function returns the nested subscripts from a NSPredicate with the following format `anyCol[0]['key']['all']`
// This function returns the nested subscripts from a NSPredicate with the following format `anyCol[0]['key'][#any]`
// and its respective keypath (including any linked keypath)
// This will iterate each argument of the NSExpression and its nested NSExpressions, takes the constant subscript
// and creates a PathElement to be used in the query.
// and creates a PathElement to be used in the query. If we use `#any` as a wildcard this will show in the parser
// predicate as NSKeyPathExpressionType.
NSString* QueryBuilder::get_path_elements(std::vector<PathElement> &paths, NSExpression *expression) {
NSString *keyPath = @"";
for (NSUInteger i = 0; i < expression.arguments.count; i++) {
Expand All @@ -1865,23 +1866,24 @@ 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:@"*"]) {
paths.push_back(PathElement{});
} else {
paths.push_back(PathElement{key.UTF8String});
}
paths.push_back(PathElement{key.UTF8String});
} else {
throwException(@"Invalid subscript type",
@"Only `Strings` or index are allowed subscripts");
@"Invalid subscript type '%@': Only `Strings` or index are allowed subscripts", expression);
}
} else if (expression.arguments[i].expressionType == NSKeyPathExpressionType) {
nestedKeyPath = expression.arguments[i].keyPath;
id path = [(id)expression.arguments[i] predicateFormat];
if ([path isEqual:@"#any"]) {
paths.emplace_back();
} else {
nestedKeyPath = path;
}
} else {
throwException(@"Invalid expression type",
@"Subscripts queries don't allow any other expression types");
@"Invalid expression type '%@': Subscripts queries don't allow any other expression types", expression);
}
if ([nestedKeyPath length] > 0) {
keyPath = ([keyPath length] > 0) ? [NSString stringWithFormat:@"%@.%@", keyPath, nestedKeyPath] : [NSString stringWithFormat:@"%@", nestedKeyPath];
keyPath = ([keyPath length] > 0) ? [NSString stringWithFormat:@"%@.%@", keyPath, nestedKeyPath] : nestedKeyPath;
}
}

Expand Down
4 changes: 2 additions & 2 deletions Realm/RLMUtil.mm
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@
#endif

static inline RLMArray *asRLMArray(__unsafe_unretained id const value) {
return RLMDynamicCast<RLMArray>(value) ?: RLMDynamicCast<RLMSwiftCollectionBase>(value)._rlmCollection;
return RLMDynamicCast<RLMArray>(value) ?: (RLMArray *)RLMDynamicCast<RLMSwiftCollectionBase>(value)._rlmCollection;
}

static inline RLMSet *asRLMSet(__unsafe_unretained id const value) {
return RLMDynamicCast<RLMSet>(value) ?: RLMDynamicCast<RLMSwiftCollectionBase>(value)._rlmCollection;
}

static inline RLMDictionary *asRLMDictionary(__unsafe_unretained id const value) {
return RLMDynamicCast<RLMDictionary>(value) ?: RLMDynamicCast<RLMSwiftCollectionBase>(value)._rlmCollection;
return RLMDynamicCast<RLMDictionary>(value) ?: (RLMDictionary *)RLMDynamicCast<RLMSwiftCollectionBase>(value)._rlmCollection;
}

static inline bool checkCollectionType(__unsafe_unretained id<RLMCollection> const collection,
Expand Down
8 changes: 4 additions & 4 deletions Realm/Tests/QueryTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -3810,8 +3810,8 @@ - (void)testDictionarySubscriptThrowsException {
@"Invalid keypath 'set[\"invalid\"]': only dictionaries and mixed support subscript predicates.");
RLMAssertThrowsWithReason(([realm objects:@"OwnerObject" where:@"dog['dogName'] = NULL"]),
@"Aggregate operations can only be used on key paths that include an collection property");
RLMAssertThrowsWithReason(([realm objects:@"DictionaryPropertyObject" where:@"stringDictionary[%@] = NULL", [RLMObjectId objectId]]),
@"Only `Strings` or index are allowed subscripts");
RLMAssertThrows(([realm objects:@"DictionaryPropertyObject" where:@"stringDictionary[%@] = NULL", [RLMObjectId objectId]]),
@"Invalid subscript type 'anyCol[[a-z0-9]+]': Only `Strings` or index are allowed subscripts");
RLMAssertThrowsWithReason(([realm objects:@"DictionaryPropertyObject" where:@"stringDictionary['aKey']['bKey'] = NULL"]),
@"Invalid subscript size 'stringDictionary[\"aKey\"][\"bKey\"]': nested dictionaries queries are only allowed in mixed properties.");
RLMAssertThrowsWithReason(([realm objects:@"DictionaryPropertyObject" where:@"stringDictionary[0] = NULL"]),
Expand All @@ -3820,8 +3820,8 @@ - (void)testDictionarySubscriptThrowsException {

- (void)testMixedSubscriptsThrowsException {
RLMRealm *realm = [self realm];
RLMAssertThrowsWithReason(([realm objects:@"AllTypesObject" where:@"anyCol[%@] = NULL", [NSDate date]]),
@"Only `Strings` or index are allowed subscripts");
RLMAssertThrows(([realm objects:@"AllTypesObject" where:@"anyCol[%@] = NULL", [RLMObjectId objectId]]),
@"Invalid subscript type 'anyCol[[a-z0-9]+]': Only `Strings` or index are allowed subscripts");
}

- (void)testCollectionsQueryAllValuesAllKeys {
Expand Down
4 changes: 2 additions & 2 deletions RealmSwift/AnyRealmValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ public enum AnyRealmValue: Hashable {
hasher.combine(u.hashValue)
case let .object(o):
hasher.combine(o.hashValue)
case let .dictionary(d):
case .dictionary:
hasher.combine(12)
case let .list(l):
case .list:
hasher.combine(13)
case .none:
hasher.combine(14)
Expand Down
6 changes: 2 additions & 4 deletions RealmSwift/ObjectiveCSupport+AnyRealmValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ public extension ObjectiveCSupport {
case let .object(o):
return o
case let .dictionary(d):
let rlmDictionary = Map<String, AnyRealmValue>._rlmFromObjc(d)?.rlmDictionary
return rlmDictionary
return d.rlmDictionary
case let .list(l):
let rlmArray = List<AnyRealmValue>._rlmFromObjc(l)?.rlmArray
return rlmArray
return l.rlmArray
default:
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions RealmSwift/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ extension Query where T == AnyRealmValue {
.init(appendKeyPath("['\(key)']", options: [.isPath]))
}
/// Query all indexes or keys in a mixed nested collecttion.
public var all: Query<AnyRealmValue> {
.init(appendKeyPath("['*']", options: [.isPath]))
public var any: Query<AnyRealmValue> {
.init(appendKeyPath("[#any]", options: [.isPath]))
}
}

Expand Down
13 changes: 3 additions & 10 deletions RealmSwift/RealmCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,9 @@ public protocol _RealmMapValue {
/// Advance to the next element and return it, or `nil` if no next element exists.
public mutating func next() -> Element? {
let next = generatorBase.next()
if let key = next as? Key {
if let value = collection[key as AnyObject].map(dynamicBridgeCast) as? Value {
return (key: key, value: value)
} else if let value = collection[key as AnyObject] as? RLMDictionary<AnyObject, AnyObject> {
let map = Map<String, AnyRealmValue>(objc: value)
return (key: key, value: AnyRealmValue.dictionary(map) as! Value)
} else if let value = collection[key as AnyObject] as? RLMArray<AnyObject> {
let list = List<AnyRealmValue>(collection: value)
return (key: key, value: AnyRealmValue.list(list) as! Value)
}
if let key = next as? Key,
let value = collection[key as AnyObject].map(Value._rlmFromObjc(_:)), let value {
return (key: key, value: value)
}
return nil
}
Expand Down
24 changes: 12 additions & 12 deletions RealmSwift/Tests/MixedCollectionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MixedCollectionTest: TestCase {
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key3"], .int(234))
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key4"], .float(789.123))
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key5"], .double(12345.678901))
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key6"], .data("a".data(using: .utf8)!))
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key6"], .data(Data("a".utf8)))
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key7"], .date(d))
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key9"], .objectId(oid))
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key10"], .uuid(uuid))
Expand All @@ -44,7 +44,7 @@ class MixedCollectionTest: TestCase {
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key3"]?.intValue, 234)
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key4"]?.floatValue, 789.123)
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key5"]?.doubleValue, 12345.678901)
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key6"]?.dataValue, "a".data(using: .utf8)!)
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key6"]?.dataValue, Data("a".utf8))
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key7"]?.dateValue, d)
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key8"]?.object(SwiftStringObject.self)?.stringCol, so.stringCol)
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key9"]?.objectIdValue, oid)
Expand All @@ -58,7 +58,7 @@ class MixedCollectionTest: TestCase {
"key3": .int(234),
"key4": .float(789.123),
"key5": .double(12345.678901),
"key6": .data("a".data(using: .utf8)!),
"key6": .data(Data("a".utf8)),
"key7": .date(d),
"key8": .object(so),
"key9": .objectId(oid),
Expand Down Expand Up @@ -120,17 +120,17 @@ class MixedCollectionTest: TestCase {

// Unmanaged Update
o.anyValue.value.dictionaryValue?["key1"] = .object(so)
o.anyValue.value.dictionaryValue?["key2"] = .data("a".data(using: .utf8)!)
o.anyValue.value.dictionaryValue?["key2"] = .data(Data("a".utf8))
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key1"]?.object(SwiftStringObject.self)?.stringCol, so.stringCol)
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key2"], .data("a".data(using: .utf8)!))
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key2"], .data(Data("a".utf8)))

// Add mixed collection to object
let realm = realmWithTestPath()
try realm.write {
realm.add(o)
}
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key1"]?.object(SwiftStringObject.self)?.stringCol, so.stringCol)
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key2"], .data("a".data(using: .utf8)!))
XCTAssertEqual(o.anyValue.value.dictionaryValue?["key2"], .data(Data("a".utf8)))

// Update managed object
try realm.write {
Expand Down Expand Up @@ -232,7 +232,7 @@ class MixedCollectionTest: TestCase {
XCTAssertEqual(o.anyValue.value.listValue?[2], .int(234))
XCTAssertEqual(o.anyValue.value.listValue?[3], .float(789.123))
XCTAssertEqual(o.anyValue.value.listValue?[4], .double(12345.678901))
XCTAssertEqual(o.anyValue.value.listValue?[5], .data("a".data(using: .utf8)!))
XCTAssertEqual(o.anyValue.value.listValue?[5], .data(Data("a".utf8)))
XCTAssertEqual(o.anyValue.value.listValue?[6], .date(d))
XCTAssertEqual(o.anyValue.value.listValue?[8], .objectId(oid))
XCTAssertEqual(o.anyValue.value.listValue?[9], .uuid(uuid))
Expand All @@ -243,7 +243,7 @@ class MixedCollectionTest: TestCase {
XCTAssertEqual(o.anyValue.value.listValue?[2].intValue, 234)
XCTAssertEqual(o.anyValue.value.listValue?[3].floatValue, 789.123)
XCTAssertEqual(o.anyValue.value.listValue?[4].doubleValue, 12345.678901)
XCTAssertEqual(o.anyValue.value.listValue?[5].dataValue, "a".data(using: .utf8)!)
XCTAssertEqual(o.anyValue.value.listValue?[5].dataValue, Data("a".utf8))
XCTAssertEqual(o.anyValue.value.listValue?[6].dateValue, d)
XCTAssertEqual(o.anyValue.value.listValue?[7].object(SwiftStringObject.self)?.stringCol, so.stringCol)
XCTAssertEqual(o.anyValue.value.listValue?[8].objectIdValue, oid)
Expand All @@ -257,7 +257,7 @@ class MixedCollectionTest: TestCase {
.int(234),
.float(789.123),
.double(12345.678901),
.data("a".data(using: .utf8)!),
.data(Data("a".utf8)),
.date(d),
.object(so),
.objectId(oid),
Expand Down Expand Up @@ -319,17 +319,17 @@ class MixedCollectionTest: TestCase {

// Unmanaged Update
o.anyValue.value.listValue?[0] = .object(so)
o.anyValue.value.listValue?[1] = .data("a".data(using: .utf8)!)
o.anyValue.value.listValue?[1] = .data(Data("a".utf8))
XCTAssertEqual(o.anyValue.value.listValue?[0].object(SwiftStringObject.self)?.stringCol, so.stringCol)
XCTAssertEqual(o.anyValue.value.listValue?[1], .data("a".data(using: .utf8)!))
XCTAssertEqual(o.anyValue.value.listValue?[1], .data(Data("a".utf8)))

// Add mixed collection to object
let realm = realmWithTestPath()
try realm.write {
realm.add(o)
}
XCTAssertEqual(o.anyValue.value.listValue?[0].object(SwiftStringObject.self)?.stringCol, so.stringCol)
XCTAssertEqual(o.anyValue.value.listValue?[1], .data("a".data(using: .utf8)!))
XCTAssertEqual(o.anyValue.value.listValue?[1], .data(Data("a".utf8)))

// Update managed object
try realm.write {
Expand Down
2 changes: 1 addition & 1 deletion RealmSwift/Tests/ObjectCreationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,7 @@ class ObjectCreationTests: TestCase {
case .object: return ["invalid", ["a"], ["boolCol": "a"], SwiftIntObject()]
case .objectId: return ["invalid", 123]
case .decimal128: return ["invalid"]
case .any: fatalError("not invalid")
case .any: return [MutableSet<String>()]
case .linkingObjects: fatalError("not supported")
case .UUID: return ["invalid"]
default:
Expand Down
Loading

0 comments on commit 2a3861f

Please sign in to comment.