diff --git a/CHANGELOG.md b/CHANGELOG.md index b65f0bb579..e05841e36b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ * Added `MutableSubscriptionSet.removeByType` for removing subscriptions by their realm object type. (Issue [#317](https://github.com/realm/realm-dart/issues/317)) * Support results of primitives, ie. `RealmResult`. (Issue [#162](https://github.com/realm/realm-dart/issues/162)) * Support notifications on all managed realm lists, including list of primitives, ie. `RealmList.changes` is supported. ([#893](https://github.com/realm/realm-dart/pull/893)) -* Support named backlinks on realm models. You can now add and annotate a realm object iterator field with `@Backlink(#symbolName)`. ([#996](https://github.com/realm/realm-dart/pull/996)) +* Support named backlinks on realm models. You can now add and annotate a realm object iterator field with `@Backlink(#fieldName)`. ([#996](https://github.com/realm/realm-dart/pull/996)) ### Fixed * Fixed a wrong mapping for `AuthProviderType` returned by `User.provider` for google, facebook and apple credentials. diff --git a/common/lib/src/realm_common_base.dart b/common/lib/src/realm_common_base.dart index b2aa4700fe..1db0dbf581 100644 --- a/common/lib/src/realm_common_base.dart +++ b/common/lib/src/realm_common_base.dart @@ -98,9 +98,10 @@ class Ignored { const Ignored(); } -/// Indicates a backlink property. +/// Indicates that the field it decorates is the inverse end of a relationship. /// {@category Annotations} class Backlink { - final Symbol symbol; - const Backlink(this.symbol); + /// The name of the field in the other class that links to this class. + final Symbol fieldName; + const Backlink(this.fieldName); } diff --git a/generator/lib/src/dart_type_ex.dart b/generator/lib/src/dart_type_ex.dart index 05602b662c..be1d34d6b4 100644 --- a/generator/lib/src/dart_type_ex.dart +++ b/generator/lib/src/dart_type_ex.dart @@ -61,11 +61,11 @@ extension DartTypeEx on DartType { DartType get mappedType { final self = this; - final provider = session.typeProvider; if (isRealmCollection) { if (self is ParameterizedType) { final mapped = self.typeArguments.last.mappedType; if (self != mapped) { + final provider = session.typeProvider; if (self.isDartCoreList) { final mappedList = provider.listType(mapped); return PseudoType('Realm${mappedList.getDisplayString(withNullability: true)}', nullabilitySuffix: mappedList.nullabilitySuffix); diff --git a/generator/lib/src/field_element_ex.dart b/generator/lib/src/field_element_ex.dart index 8470b5135c..6f7496adce 100644 --- a/generator/lib/src/field_element_ex.dart +++ b/generator/lib/src/field_element_ex.dart @@ -216,7 +216,7 @@ extension FieldElementEx on FieldElement { ); } - final sourceFieldName = backlink.value.getField('symbol')?.toSymbolValue(); + final sourceFieldName = backlink.value.getField('fieldName')?.toSymbolValue(); final sourceType = (type as ParameterizedType).typeArguments.first; final sourceField = (sourceType.element2 as ClassElement?)?.fields.where((f) => f.name == sourceFieldName).singleOrNull; diff --git a/generator/test/test_util.dart b/generator/test/test_util.dart index cc1b1eaf38..d2621eeaff 100644 --- a/generator/test/test_util.dart +++ b/generator/test/test_util.dart @@ -65,7 +65,7 @@ class LinesEqualsMatcher extends Matcher { } } - if (actualLines.length > expectedLines.length) { + if (actualLines.length != expectedLines.length) { matchState["Error"] = "Different number of lines. \nExpected: ${expectedLines.length}\nActual: ${actualLines.length}"; return false; } diff --git a/test/backlinks_test.dart b/test/backlinks_test.dart index 4f3481ffa0..62e3f8c0b5 100644 --- a/test/backlinks_test.dart +++ b/test/backlinks_test.dart @@ -35,12 +35,12 @@ class _Source { @RealmModel() class _Target { @Backlink(#oneTarget) - late Iterable<_Source> oneToMany; // computed property, so must go last in generated class! + late Iterable<_Source> oneToMany; // will be reordered by the generator to go after name String name = 'target'; @Backlink(#manyTargets) - late Iterable<_Source> manyToMany; // computed property, so must go last in generated class! + late Iterable<_Source> manyToMany; } Future main([List? args]) async {