Skip to content

Commit

Permalink
Get rid off DynamiRealmObject.getList<T>('x'). You can use get<List<T…
Browse files Browse the repository at this point in the history
…>>('x')
  • Loading branch information
nielsenko committed Sep 6, 2022
1 parent f93304e commit f1226d2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 92 deletions.
57 changes: 0 additions & 57 deletions lib/src/realm_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object?>` will be
/// returned.
List<T> getList<T extends Object?>(String propertyName) {
return get<List<T>>(propertyName);
}

RealmPropertyMetadata? _validatePropertyType<T extends Object?>(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<T>() && 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<T> is $T.");
}

final targetType = _getPropertyType<T>();
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 = <Type, RealmPropertyType>{
int: RealmPropertyType.int,
typeOf<int?>(): RealmPropertyType.int,
double: RealmPropertyType.double,
typeOf<double?>(): RealmPropertyType.double,
String: RealmPropertyType.string,
typeOf<String?>(): RealmPropertyType.string,
bool: RealmPropertyType.bool,
typeOf<bool?>(): RealmPropertyType.bool,
DateTime: RealmPropertyType.timestamp,
typeOf<DateTime?>(): RealmPropertyType.timestamp,
ObjectId: RealmPropertyType.objectid,
typeOf<ObjectId?>(): RealmPropertyType.objectid,
Uuid: RealmPropertyType.uuid,
typeOf<Uuid?>(): RealmPropertyType.uuid,
RealmObject: RealmPropertyType.object,
typeOf<RealmObject?>(): RealmPropertyType.object,
};

RealmPropertyType? _getPropertyType<T extends Object?>() => _propertyTypeMap[T];
}
63 changes: 28 additions & 35 deletions test/dynamic_realm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,39 +148,32 @@ Future<void> main([List<String>? args]) async {
}

void _validateDynamicLists(RealmObject actual, AllCollections expected) {
expect(actual.dynamic.getList<String>('strings'), expected.strings);
expect(actual.dynamic.getList('strings'), expected.strings);
expect(actual.dynamic.get<List<String>>('strings'), expected.strings);
expect(actual.dynamic.get<List>('strings'), expected.strings);
expect(actual.dynamic.get('strings'), expected.strings);

expect(actual.dynamic.getList<bool>('bools'), expected.bools);
expect(actual.dynamic.getList('bools'), expected.bools);
expect(actual.dynamic.get<List<bool>>('bools'), expected.bools);
expect(actual.dynamic.get<List>('bools'), expected.bools);
expect(actual.dynamic.get('bools'), expected.bools);

expect(actual.dynamic.getList<DateTime>('dates'), expected.dates);
expect(actual.dynamic.getList('dates'), expected.dates);
expect(actual.dynamic.get<List<DateTime>>('dates'), expected.dates);
expect(actual.dynamic.get<List>('dates'), expected.dates);
expect(actual.dynamic.get('dates'), expected.dates);

expect(actual.dynamic.getList<double>('doubles'), expected.doubles);
expect(actual.dynamic.getList('doubles'), expected.doubles);
expect(actual.dynamic.get<List<double>>('doubles'), expected.doubles);
expect(actual.dynamic.get<List>('doubles'), expected.doubles);
expect(actual.dynamic.get('doubles'), expected.doubles);

expect(actual.dynamic.getList<ObjectId>('objectIds'), expected.objectIds);
expect(actual.dynamic.getList('objectIds'), expected.objectIds);
expect(actual.dynamic.get<List<ObjectId>>('objectIds'), expected.objectIds);
expect(actual.dynamic.get<List>('objectIds'), expected.objectIds);
expect(actual.dynamic.get('objectIds'), expected.objectIds);

expect(actual.dynamic.getList<Uuid>('uuids'), expected.uuids);
expect(actual.dynamic.getList('uuids'), expected.uuids);
expect(actual.dynamic.get<List<Uuid>>('uuids'), expected.uuids);
expect(actual.dynamic.get<List>('uuids'), expected.uuids);
expect(actual.dynamic.get('uuids'), expected.uuids);

expect(actual.dynamic.getList<int>('ints'), expected.ints);
expect(actual.dynamic.getList('ints'), expected.ints);
expect(actual.dynamic.get<List<int>>('ints'), expected.ints);
expect(actual.dynamic.get<List>('ints'), expected.ints);
expect(actual.dynamic.get('ints'), expected.ints);

dynamic actualDynamic = actual;
Expand Down Expand Up @@ -267,9 +260,9 @@ Future<void> main([List<String>? args]) async {
expect(obj2.dynamic.get<RealmObject?>('link'), obj3);

// TODO(kasper): This won't work yet!
// final list = obj1.dynamic.getList<RealmObject>('list');
// final list = obj1.dynamic.get<List<RealmObject>>('list');
// instead we resort to this:
final list = obj1.dynamic.getList<Object>('list');
final list = obj1.dynamic.get<List<Object>>('list');

expect(list[0], obj1);
expect(list[1], obj2);
Expand Down Expand Up @@ -451,8 +444,8 @@ Future<void> main([List<String>? 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<List>('strings').isNotEmpty);
final obj2 = objects.singleWhere((element) => element.dynamic.get<List>('strings').isEmpty);

_validateDynamicLists(obj1, _getPopulatedAllCollections());
_validateDynamicLists(obj2, AllCollections());
Expand All @@ -476,14 +469,14 @@ Future<void> main([List<String>? args]) async {
final obj2 = dynamicRealm.dynamic.find(LinksClass.schema.name, uuid2)!;

// TODO(kasper): This won't work yet:
// expect(obj1.dynamic.getList<RealmObject>('list'), isEmpty);
// expect(obj1.dynamic.get<List<RealmObject>>('list'), isEmpty);
// instead we do:
expect(obj1.dynamic.getList<RealmObjectMarker>('list'), isEmpty);
expect(obj1.dynamic.getList('list'), isEmpty);
expect(obj1.dynamic.get<List<RealmObjectMarker>>('list'), isEmpty);
expect(obj1.dynamic.get<List>('list'), isEmpty);

expect(obj2.dynamic.getList<RealmObjectMarker>('list'), [obj1, obj1]); // -"-
expect(obj2.dynamic.getList('list'), [obj1, obj1]);
expect(obj2.dynamic.getList<RealmObjectMarker>('list').cast<RealmObject>()[0].dynamic.get<Uuid>('id'), uuid1); // -"-
expect(obj2.dynamic.get<List<RealmObjectMarker>>('list'), [obj1, obj1]); // -"-
expect(obj2.dynamic.get<List>('list'), [obj1, obj1]);
expect(obj2.dynamic.get<List<RealmObjectMarker>>('list').cast<RealmObject>()[0].dynamic.get<Uuid>('id'), uuid1); // -"-

dynamic dynamicObj1 = obj1;
dynamic dynamicObj2 = obj2;
Expand All @@ -503,7 +496,7 @@ Future<void> main([List<String>? args]) async {
final dynamicRealm = _getDynamicRealm(staticRealm);

final obj = dynamicRealm.dynamic.all(AllCollections.schema.name).single;
expect(() => obj.dynamic.getList('i-dont-exist'), throws<RealmException>("Property 'i-dont-exist' does not exist on class 'AllCollections'"));
expect(() => obj.dynamic.get<List>('i-dont-exist'), throws<RealmException>("Property 'i-dont-exist' does not exist on class 'AllCollections'"));
});

test('fails with wrong type', () {
Expand All @@ -517,7 +510,7 @@ Future<void> main([List<String>? args]) async {
final obj = dynamicRealm.dynamic.all(AllCollections.schema.name).single;

expect(
() => obj.dynamic.getList<int>('strings'),
() => obj.dynamic.get<List<int>>('strings'),
throws<RealmException>(
"Property 'strings' on class 'AllCollections' is not the correct type. Expected 'List<int>', got 'ManagedRealmList<String>'."));
});
Expand All @@ -531,8 +524,8 @@ Future<void> main([List<String>? args]) async {
final dynamicRealm = _getDynamicRealm(staticRealm);

final obj = dynamicRealm.dynamic.all(AllTypes.schema.name).single;
expect(() => obj.dynamic.getList('intProp'),
throws<RealmException>("Property 'intProp' on class 'AllTypes' is not the correct type. Expected 'List<Object?>', got 'int'."));
expect(() => obj.dynamic.get<List>('intProp'),
throws<RealmException>("Property 'intProp' on class 'AllTypes' is not the correct type. Expected 'List<dynamic>', got 'int'."));
});
});
}
Expand Down Expand Up @@ -612,14 +605,14 @@ Future<void> main([List<String>? args]) async {
final obj1 = realm.find<LinksClass>(uuid1)!;
final obj2 = realm.find<LinksClass>(uuid2)!;

expect(obj1.dynamic.getList<RealmObject>('list'), isEmpty);
expect(obj1.dynamic.getList('list'), isEmpty);
expect(obj1.dynamic.get<List<RealmObject>>('list'), isEmpty);
expect(obj1.dynamic.get<List>('list'), isEmpty);

expect(obj2.dynamic.getList<LinksClass>('list'), [obj1, obj1]);
expect(obj2.dynamic.getList<RealmObject>('list'), [obj1, obj1]);
final x = obj2.dynamic.getList('list');
expect(obj2.dynamic.getList('list'), [obj1, obj1]);
expect(obj2.dynamic.getList<RealmObject>('list')[0].dynamic.get<Uuid>('id'), uuid1);
expect(obj2.dynamic.get<List<LinksClass>>('list'), [obj1, obj1]);
expect(obj2.dynamic.get<List<RealmObject>>('list'), [obj1, obj1]);
final x = obj2.dynamic.get<List>('list');
expect(obj2.dynamic.get<List>('list'), [obj1, obj1]);
expect(obj2.dynamic.get<List<RealmObject>>('list')[0].dynamic.get<Uuid>('id'), uuid1);

dynamic dynamicObj1 = obj1;
dynamic dynamicObj2 = obj2;
Expand Down

0 comments on commit f1226d2

Please sign in to comment.