diff --git a/lib/src/realm_object.dart b/lib/src/realm_object.dart index 59f9b374d8..3ed9a1df86 100644 --- a/lib/src/realm_object.dart +++ b/lib/src/realm_object.dart @@ -409,61 +409,4 @@ class DynamicRealmObject { throw RealmException( "Property '$propertyName' on class '${_obj.instanceSchema.name}' is not the correct type. Expected '$T', got '${result.runtimeType}'."); } - - /// Gets a list by the property name. If a generic type is specified, the property - /// type will be validated against the type. Otherwise, a `List` will be - /// returned. - List getList(String propertyName) { - return get>(propertyName); - } - - RealmPropertyMetadata? _validatePropertyType(String name, RealmCollectionType expectedCollectionType) { - final accessor = _obj.accessor; - if (accessor is RealmCoreAccessor) { - final prop = accessor.metadata[name]; - - if (prop.schema.collectionType != expectedCollectionType) { - throw RealmException( - "Property '$name' on class '${accessor.metadata.name}' is '${prop.schema.collectionType}' but the method used to access it expected '$expectedCollectionType'."); - } - - // If the user passed in a type argument, we should validate its nullability; if they invoked - // the method without a type arg, we don't - if (!isNullable() && prop.schema.optional) { - throw RealmException( - "Property '$name' on class '${accessor.metadata.name}' is ${prop.schema.optional ? 'nullable' : 'required'} but the generic argument passed to get is $T."); - } - - final targetType = _getPropertyType(); - if (targetType != null && targetType != prop.schema.propertyType) { - throw RealmException( - "Property '$name' on class '${accessor.metadata.name}' is not the correct type. Expected '$targetType', got '${prop.schema.propertyType}'."); - } - - return prop; - } - - return null; - } - - static final _propertyTypeMap = { - int: RealmPropertyType.int, - typeOf(): RealmPropertyType.int, - double: RealmPropertyType.double, - typeOf(): RealmPropertyType.double, - String: RealmPropertyType.string, - typeOf(): RealmPropertyType.string, - bool: RealmPropertyType.bool, - typeOf(): RealmPropertyType.bool, - DateTime: RealmPropertyType.timestamp, - typeOf(): RealmPropertyType.timestamp, - ObjectId: RealmPropertyType.objectid, - typeOf(): RealmPropertyType.objectid, - Uuid: RealmPropertyType.uuid, - typeOf(): RealmPropertyType.uuid, - RealmObject: RealmPropertyType.object, - typeOf(): RealmPropertyType.object, - }; - - RealmPropertyType? _getPropertyType() => _propertyTypeMap[T]; } diff --git a/test/dynamic_realm_test.dart b/test/dynamic_realm_test.dart index efbce16d15..a760c9bde3 100644 --- a/test/dynamic_realm_test.dart +++ b/test/dynamic_realm_test.dart @@ -148,39 +148,32 @@ Future main([List? args]) async { } void _validateDynamicLists(RealmObject actual, AllCollections expected) { - expect(actual.dynamic.getList('strings'), expected.strings); - expect(actual.dynamic.getList('strings'), expected.strings); expect(actual.dynamic.get>('strings'), expected.strings); + expect(actual.dynamic.get('strings'), expected.strings); expect(actual.dynamic.get('strings'), expected.strings); - expect(actual.dynamic.getList('bools'), expected.bools); - expect(actual.dynamic.getList('bools'), expected.bools); expect(actual.dynamic.get>('bools'), expected.bools); + expect(actual.dynamic.get('bools'), expected.bools); expect(actual.dynamic.get('bools'), expected.bools); - expect(actual.dynamic.getList('dates'), expected.dates); - expect(actual.dynamic.getList('dates'), expected.dates); expect(actual.dynamic.get>('dates'), expected.dates); + expect(actual.dynamic.get('dates'), expected.dates); expect(actual.dynamic.get('dates'), expected.dates); - expect(actual.dynamic.getList('doubles'), expected.doubles); - expect(actual.dynamic.getList('doubles'), expected.doubles); expect(actual.dynamic.get>('doubles'), expected.doubles); + expect(actual.dynamic.get('doubles'), expected.doubles); expect(actual.dynamic.get('doubles'), expected.doubles); - expect(actual.dynamic.getList('objectIds'), expected.objectIds); - expect(actual.dynamic.getList('objectIds'), expected.objectIds); expect(actual.dynamic.get>('objectIds'), expected.objectIds); + expect(actual.dynamic.get('objectIds'), expected.objectIds); expect(actual.dynamic.get('objectIds'), expected.objectIds); - expect(actual.dynamic.getList('uuids'), expected.uuids); - expect(actual.dynamic.getList('uuids'), expected.uuids); expect(actual.dynamic.get>('uuids'), expected.uuids); + expect(actual.dynamic.get('uuids'), expected.uuids); expect(actual.dynamic.get('uuids'), expected.uuids); - expect(actual.dynamic.getList('ints'), expected.ints); - expect(actual.dynamic.getList('ints'), expected.ints); expect(actual.dynamic.get>('ints'), expected.ints); + expect(actual.dynamic.get('ints'), expected.ints); expect(actual.dynamic.get('ints'), expected.ints); dynamic actualDynamic = actual; @@ -267,9 +260,9 @@ Future main([List? args]) async { expect(obj2.dynamic.get('link'), obj3); // TODO(kasper): This won't work yet! - // final list = obj1.dynamic.getList('list'); + // final list = obj1.dynamic.get>('list'); // instead we resort to this: - final list = obj1.dynamic.getList('list'); + final list = obj1.dynamic.get>('list'); expect(list[0], obj1); expect(list[1], obj2); @@ -451,8 +444,8 @@ Future main([List? args]) async { final dynamicRealm = _getDynamicRealm(staticRealm); final objects = dynamicRealm.dynamic.all(AllCollections.schema.name); - final obj1 = objects.singleWhere((element) => element.dynamic.getList('strings').isNotEmpty); - final obj2 = objects.singleWhere((element) => element.dynamic.getList('strings').isEmpty); + final obj1 = objects.singleWhere((element) => element.dynamic.get('strings').isNotEmpty); + final obj2 = objects.singleWhere((element) => element.dynamic.get('strings').isEmpty); _validateDynamicLists(obj1, _getPopulatedAllCollections()); _validateDynamicLists(obj2, AllCollections()); @@ -476,14 +469,14 @@ Future main([List? args]) async { final obj2 = dynamicRealm.dynamic.find(LinksClass.schema.name, uuid2)!; // TODO(kasper): This won't work yet: - // expect(obj1.dynamic.getList('list'), isEmpty); + // expect(obj1.dynamic.get>('list'), isEmpty); // instead we do: - expect(obj1.dynamic.getList('list'), isEmpty); - expect(obj1.dynamic.getList('list'), isEmpty); + expect(obj1.dynamic.get>('list'), isEmpty); + expect(obj1.dynamic.get('list'), isEmpty); - expect(obj2.dynamic.getList('list'), [obj1, obj1]); // -"- - expect(obj2.dynamic.getList('list'), [obj1, obj1]); - expect(obj2.dynamic.getList('list').cast()[0].dynamic.get('id'), uuid1); // -"- + expect(obj2.dynamic.get>('list'), [obj1, obj1]); // -"- + expect(obj2.dynamic.get('list'), [obj1, obj1]); + expect(obj2.dynamic.get>('list').cast()[0].dynamic.get('id'), uuid1); // -"- dynamic dynamicObj1 = obj1; dynamic dynamicObj2 = obj2; @@ -503,7 +496,7 @@ Future main([List? args]) async { final dynamicRealm = _getDynamicRealm(staticRealm); final obj = dynamicRealm.dynamic.all(AllCollections.schema.name).single; - expect(() => obj.dynamic.getList('i-dont-exist'), throws("Property 'i-dont-exist' does not exist on class 'AllCollections'")); + expect(() => obj.dynamic.get('i-dont-exist'), throws("Property 'i-dont-exist' does not exist on class 'AllCollections'")); }); test('fails with wrong type', () { @@ -517,7 +510,7 @@ Future main([List? args]) async { final obj = dynamicRealm.dynamic.all(AllCollections.schema.name).single; expect( - () => obj.dynamic.getList('strings'), + () => obj.dynamic.get>('strings'), throws( "Property 'strings' on class 'AllCollections' is not the correct type. Expected 'List', got 'ManagedRealmList'.")); }); @@ -531,8 +524,8 @@ Future main([List? args]) async { final dynamicRealm = _getDynamicRealm(staticRealm); final obj = dynamicRealm.dynamic.all(AllTypes.schema.name).single; - expect(() => obj.dynamic.getList('intProp'), - throws("Property 'intProp' on class 'AllTypes' is not the correct type. Expected 'List', got 'int'.")); + expect(() => obj.dynamic.get('intProp'), + throws("Property 'intProp' on class 'AllTypes' is not the correct type. Expected 'List', got 'int'.")); }); }); } @@ -612,14 +605,14 @@ Future main([List? args]) async { final obj1 = realm.find(uuid1)!; final obj2 = realm.find(uuid2)!; - expect(obj1.dynamic.getList('list'), isEmpty); - expect(obj1.dynamic.getList('list'), isEmpty); + expect(obj1.dynamic.get>('list'), isEmpty); + expect(obj1.dynamic.get('list'), isEmpty); - expect(obj2.dynamic.getList('list'), [obj1, obj1]); - expect(obj2.dynamic.getList('list'), [obj1, obj1]); - final x = obj2.dynamic.getList('list'); - expect(obj2.dynamic.getList('list'), [obj1, obj1]); - expect(obj2.dynamic.getList('list')[0].dynamic.get('id'), uuid1); + expect(obj2.dynamic.get>('list'), [obj1, obj1]); + expect(obj2.dynamic.get>('list'), [obj1, obj1]); + final x = obj2.dynamic.get('list'); + expect(obj2.dynamic.get('list'), [obj1, obj1]); + expect(obj2.dynamic.get>('list')[0].dynamic.get('id'), uuid1); dynamic dynamicObj1 = obj1; dynamic dynamicObj2 = obj2;