From a9501c904eebb48a56110beded2316d4854db4bd Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Wed, 19 Oct 2022 22:13:14 +0200 Subject: [PATCH 01/13] Port embedded object support to master --- CHANGELOG.md | 1 + common/lib/src/realm_common_base.dart | 34 +- common/lib/src/realm_types.dart | 10 +- example/bin/myapp.g.dart | 61 +- .../tests/test_driver/realm_test.dart | 2 + generator/lib/src/class_element_ex.dart | 26 +- generator/lib/src/realm_field_info.dart | 4 +- generator/lib/src/realm_model_info.dart | 19 +- lib/src/cli/atlas_apps/baas_client.dart | 1 - lib/src/cli/metrics/metrics.g.dart | 2 +- lib/src/configuration.dart | 12 +- lib/src/list.dart | 38 +- lib/src/native/realm_core.dart | 109 +- lib/src/realm_class.dart | 38 +- lib/src/realm_object.dart | 130 +- lib/src/results.dart | 12 +- test/dynamic_realm_test.dart | 21 +- test/embedded_test.dart | 537 ++++++++ test/migration_test.g.dart | 106 +- test/realm_object_test.dart | 16 +- test/realm_object_test.g.dart | 181 +-- test/test.dart | 89 +- test/test.g.dart | 1193 ++++++++++++----- 23 files changed, 2003 insertions(+), 639 deletions(-) create mode 100644 test/embedded_test.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e5f1385f..fc1c3f1c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * Added more validations when using `User.apiKeys` to return more meaningful errors when the user cannot perform API key actions - e.g. when the user has been logged in with API key credentials or when the user has been logged out. (Issue [#950](https://github.com/realm/realm-dart/issues/950)) * Fixed `dart run realm_dart generate` and `flutter pub run realm generate` commands to exit with the correct error code on failure. * Added more descriptive error messages when passing objects managed by another Realm as arguments to `Realm.add/delete/deleteMany`. (PR [#942](https://github.com/realm/realm-dart/pull/942)) +* Fixed a bug where `list.remove` would not correctly remove the value if the value is the first element in the list. (PR [#975](https://github.com/realm/realm-dart/pull/975)) ### Compatibility * Realm Studio: 12.0.0 or later. diff --git a/common/lib/src/realm_common_base.dart b/common/lib/src/realm_common_base.dart index 34bd28471..58d23362a 100644 --- a/common/lib/src/realm_common_base.dart +++ b/common/lib/src/realm_common_base.dart @@ -16,12 +16,36 @@ // // ////////////////////////////////////////////////////////////////////////////// +/// An enum controlling the base type for a [RealmModel]. +/// +/// {@category Annotations} +enum ObjectType { + /// A standalone top-level object that can be persisted in Realm. It can link + /// to other objects or collections of other objects. + topLevel('RealmObject'), + + /// An object that can be embedded in other objects. It is considered owned + /// by its parent and will be deleted if its parent is deleted. + embedded('EmbeddedObject'), + + /// A special type of object used to facilitate unidirectional synchronization + /// with Atlas App Services. It is used to push data to Realm without the ability + /// to query or modify it. + _asymmetric('AsymmetricObject'); + + const ObjectType([this.className = 'Unknown']); + + /// The name of the base class exposed by the Realm SDK. + final String className; +} /// Annotation class used to define `Realm` data model classes and their properties /// /// {@category Annotations} class RealmModel { - const RealmModel(); + final ObjectType type; + + const RealmModel([this.type = ObjectType.topLevel]); } /// MapTo annotation for class member level. @@ -36,7 +60,7 @@ class MapTo { } /// Indicates a primary key property. -/// +/// /// It enables quick lookup of objects and enforces uniqueness of the values stored. /// It may only be applied to a single property in a [RealmModel] class. /// Only [String] and [int] can be used as primary keys. @@ -47,8 +71,8 @@ class PrimaryKey { const PrimaryKey(); } -/// Indicates an indexed property. -/// +/// Indicates an indexed property. +/// /// Indexed properties slightly slow down insertions but can greatly speed up queries. /// /// {@category Annotations} @@ -57,7 +81,7 @@ class Indexed { } /// Indicates an ignored property. -/// +/// /// Ignored properties will not be persisted in the `Realm`. /// /// {@category Annotations} diff --git a/common/lib/src/realm_types.dart b/common/lib/src/realm_types.dart index c42d6f0e9..f9b6cae33 100644 --- a/common/lib/src/realm_types.dart +++ b/common/lib/src/realm_types.dart @@ -89,7 +89,13 @@ class RealmStateError extends StateError implements RealmError { class Decimal128 {} // TODO Support decimal128 datatype https://github.com/realm/realm-dart/issues/725 /// @nodoc -class RealmObjectMarker {} +class RealmObjectBaseMarker {} + +/// @nodoc +class RealmObjectMarker extends RealmObjectBaseMarker {} + +/// @nodoc +class EmbeddedObjectMarker extends RealmObjectBaseMarker {} // Union type /// @nodoc @@ -107,7 +113,7 @@ class RealmAny { const RealmAny.double(double d) : this._(d); const RealmAny.uint8List(Uint8List data) : this._(data); // TODO: RealmObjectMarker introduced to avoid dependency inversion. It would be better if we could use RealmObject directly. https://github.com/realm/realm-dart/issues/701 - const RealmAny.realmObject(RealmObjectMarker o) : this._(o); + const RealmAny.realmObject(RealmObjectBaseMarker o) : this._(o); const RealmAny.dateTime(DateTime timestamp) : this._(timestamp); const RealmAny.objectId(ObjectId id) : this._(id); const RealmAny.decimal128(Decimal128 decimal) : this._(decimal); diff --git a/example/bin/myapp.g.dart b/example/bin/myapp.g.dart index 8422a878f..6e347cb07 100644 --- a/example/bin/myapp.g.dart +++ b/example/bin/myapp.g.dart @@ -6,7 +6,7 @@ part of 'myapp.dart'; // RealmObjectGenerator // ************************************************************************** -class Car extends _Car with RealmEntity, RealmObject { +class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static var _defaultsSet = false; Car( @@ -16,50 +16,51 @@ class Car extends _Car with RealmEntity, RealmObject { Person? owner, }) { if (!_defaultsSet) { - _defaultsSet = RealmObject.setDefaults({ + _defaultsSet = RealmObjectBase.setDefaults({ 'kilometers': 500, }); } - RealmObject.set(this, 'make', make); - RealmObject.set(this, 'model', model); - RealmObject.set(this, 'kilometers', kilometers); - RealmObject.set(this, 'owner', owner); + RealmObjectBase.set(this, 'make', make); + RealmObjectBase.set(this, 'model', model); + RealmObjectBase.set(this, 'kilometers', kilometers); + RealmObjectBase.set(this, 'owner', owner); } Car._(); @override - String get make => RealmObject.get(this, 'make') as String; + String get make => RealmObjectBase.get(this, 'make') as String; @override - set make(String value) => RealmObject.set(this, 'make', value); + set make(String value) => RealmObjectBase.set(this, 'make', value); @override - String? get model => RealmObject.get(this, 'model') as String?; + String? get model => RealmObjectBase.get(this, 'model') as String?; @override - set model(String? value) => RealmObject.set(this, 'model', value); + set model(String? value) => RealmObjectBase.set(this, 'model', value); @override - int? get kilometers => RealmObject.get(this, 'kilometers') as int?; + int? get kilometers => RealmObjectBase.get(this, 'kilometers') as int?; @override - set kilometers(int? value) => RealmObject.set(this, 'kilometers', value); + set kilometers(int? value) => RealmObjectBase.set(this, 'kilometers', value); @override - Person? get owner => RealmObject.get(this, 'owner') as Person?; + Person? get owner => RealmObjectBase.get(this, 'owner') as Person?; @override - set owner(covariant Person? value) => RealmObject.set(this, 'owner', value); + set owner(covariant Person? value) => + RealmObjectBase.set(this, 'owner', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Car freeze() => RealmObject.freezeObject(this); + Car freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Car._); - return const SchemaObject(Car, 'Car', [ + RealmObjectBase.registerFactory(Car._); + return const SchemaObject(ObjectType.topLevel, Car, 'Car', [ SchemaProperty('make', RealmPropertyType.string), SchemaProperty('model', RealmPropertyType.string, optional: true), SchemaProperty('kilometers', RealmPropertyType.int, optional: true), @@ -69,7 +70,7 @@ class Car extends _Car with RealmEntity, RealmObject { } } -class Person extends _Person with RealmEntity, RealmObject { +class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static var _defaultsSet = false; Person( @@ -77,38 +78,38 @@ class Person extends _Person with RealmEntity, RealmObject { int age = 1, }) { if (!_defaultsSet) { - _defaultsSet = RealmObject.setDefaults({ + _defaultsSet = RealmObjectBase.setDefaults({ 'age': 1, }); } - RealmObject.set(this, 'name', name); - RealmObject.set(this, 'age', age); + RealmObjectBase.set(this, 'name', name); + RealmObjectBase.set(this, 'age', age); } Person._(); @override - String get name => RealmObject.get(this, 'name') as String; + String get name => RealmObjectBase.get(this, 'name') as String; @override - set name(String value) => RealmObject.set(this, 'name', value); + set name(String value) => RealmObjectBase.set(this, 'name', value); @override - int get age => RealmObject.get(this, 'age') as int; + int get age => RealmObjectBase.get(this, 'age') as int; @override - set age(int value) => RealmObject.set(this, 'age', value); + set age(int value) => RealmObjectBase.set(this, 'age', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Person freeze() => RealmObject.freezeObject(this); + Person freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Person._); - return const SchemaObject(Person, 'Person', [ + RealmObjectBase.registerFactory(Person._); + return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('age', RealmPropertyType.int), ]); diff --git a/flutter/realm_flutter/tests/test_driver/realm_test.dart b/flutter/realm_flutter/tests/test_driver/realm_test.dart index f54e51727..d5cb2a3e1 100644 --- a/flutter/realm_flutter/tests/test_driver/realm_test.dart +++ b/flutter/realm_flutter/tests/test_driver/realm_test.dart @@ -10,6 +10,7 @@ import '../test/app_test.dart' as app_test; import '../test/configuration_test.dart' as configuration_test; import '../test/credentials_test.dart' as credentials_test; import '../test/dynamic_realm_test.dart' as dynamic_realm_test; +import '../test/embedded_test.dart' as embedded_test; import '../test/list_test.dart' as list_test; import '../test/migration_test.dart' as migration_test; import '../test/realm_object_test.dart' as realm_object_test; @@ -29,6 +30,7 @@ Future main(List args) async { await configuration_test.main(args); await credentials_test.main(args); await dynamic_realm_test.main(args); + await embedded_test.main(args); await list_test.main(args); await migration_test.main(args); await realm_object_test.main(args); diff --git a/generator/lib/src/class_element_ex.dart b/generator/lib/src/class_element_ex.dart index 339ef9bba..ab2d78b44 100644 --- a/generator/lib/src/class_element_ex.dart +++ b/generator/lib/src/class_element_ex.dart @@ -18,6 +18,7 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/element/element.dart'; +import 'package:realm_common/realm_common.dart'; import 'package:source_gen/source_gen.dart'; import 'annotation_value.dart'; import 'error.dart'; @@ -65,7 +66,10 @@ extension ClassElementEx on ClassElement { RealmModelInfo? get realmInfo { try { - if (realmModelInfo == null) return null; + final modelInfo = realmModelInfo; + if (modelInfo == null) { + return null; + } final modelName = this.name; @@ -143,13 +147,21 @@ extension ClassElementEx on ClassElement { ); } + final objectType = ObjectType.values[modelInfo.value.getField('type')!.getField('index')!.toIntValue()!]; + final mappedFields = fields.realmInfo.toList(); - return RealmModelInfo( - name, - modelName, - realmName, - mappedFields, - ); + + if (objectType == ObjectType.embedded && mappedFields.any((field) => field.isPrimaryKey)) { + final pkSpan = fields.firstWhere((field) => field.realmInfo?.isPrimaryKey == true).span; + throw RealmInvalidGenerationSourceError("Primary key not allowed on embedded objects", + element: this, + primarySpan: pkSpan, + secondarySpans: {span!: ''}, + primaryLabel: "$realmName is marked as embedded but has primary key defined", + todo: 'Remove the @PrimaryKey annotation from the field or set the object type to topLevel.'); + } + + return RealmModelInfo(name, modelName, realmName, mappedFields, objectType); } on InvalidGenerationSourceError catch (_) { rethrow; } catch (e, s) { diff --git a/generator/lib/src/realm_field_info.dart b/generator/lib/src/realm_field_info.dart index 0779589e1..d79ce80fc 100644 --- a/generator/lib/src/realm_field_info.dart +++ b/generator/lib/src/realm_field_info.dart @@ -62,11 +62,11 @@ class RealmFieldInfo { Iterable toCode() sync* { yield '@override'; - yield "$mappedTypeName get $name => RealmObject.get<$basicMappedTypeName>(this, '$realmName') as $mappedTypeName;"; + yield "$mappedTypeName get $name => RealmObjectBase.get<$basicMappedTypeName>(this, '$realmName') as $mappedTypeName;"; bool generateSetter = !isFinal && !isRealmCollection; if (generateSetter) { yield '@override'; - yield "set $name(${mappedTypeName != modelTypeName ? 'covariant ' : ''}$mappedTypeName value) => RealmObject.set(this, '$realmName', value);"; + yield "set $name(${mappedTypeName != modelTypeName ? 'covariant ' : ''}$mappedTypeName value) => RealmObjectBase.set(this, '$realmName', value);"; } else { bool generateThrowError = isLate || isRealmCollection; if (generateThrowError) { diff --git a/generator/lib/src/realm_model_info.dart b/generator/lib/src/realm_model_info.dart index 4ec591211..875896be8 100644 --- a/generator/lib/src/realm_model_info.dart +++ b/generator/lib/src/realm_model_info.dart @@ -27,11 +27,12 @@ class RealmModelInfo { final String modelName; final String realmName; final List fields; + final ObjectType baseType; - RealmModelInfo(this.name, this.modelName, this.realmName, this.fields); + RealmModelInfo(this.name, this.modelName, this.realmName, this.fields, this.baseType); Iterable toCode() sync* { - yield 'class $name extends $modelName with RealmEntity, RealmObject {'; + yield 'class $name extends $modelName with RealmEntity, RealmObjectBase, ${baseType.className} {'; { final allExceptCollections = fields.where((f) => !f.type.isRealmCollection).toList(); @@ -59,18 +60,18 @@ class RealmModelInfo { if (hasDefaults.isNotEmpty) { yield 'if (!_defaultsSet) {'; - yield ' _defaultsSet = RealmObject.setDefaults<$name>({'; + yield ' _defaultsSet = RealmObjectBase.setDefaults<$name>({'; yield* hasDefaults.map((f) => "'${f.realmName}': ${f.fieldElement.initializerExpression},"); yield ' });'; yield '}'; } yield* allExceptCollections.map((f) { - return "RealmObject.set(this, '${f.realmName}', ${f.name});"; + return "RealmObjectBase.set(this, '${f.realmName}', ${f.name});"; }); yield* collections.map((c) { - return "RealmObject.set<${c.mappedTypeName}>(this, '${c.realmName}', ${c.mappedTypeName}(${c.name}));"; + return "RealmObjectBase.set<${c.mappedTypeName}>(this, '${c.realmName}', ${c.mappedTypeName}(${c.name}));"; }); } yield '}'; @@ -84,19 +85,19 @@ class RealmModelInfo { ]); yield '@override'; - yield 'Stream> get changes => RealmObject.getChanges<$name>(this);'; + yield 'Stream> get changes => RealmObjectBase.getChanges<$name>(this);'; yield ''; yield '@override'; - yield '$name freeze() => RealmObject.freezeObject<$name>(this);'; + yield '$name freeze() => RealmObjectBase.freezeObject<$name>(this);'; yield ''; yield 'static SchemaObject get schema => _schema ??= _initSchema();'; yield 'static SchemaObject? _schema;'; yield 'static SchemaObject _initSchema() {'; { - yield 'RealmObject.registerFactory($name._);'; - yield "return const SchemaObject($name, '$realmName', ["; + yield 'RealmObjectBase.registerFactory($name._);'; + yield "return const SchemaObject(ObjectType.${baseType.name}, $name, '$realmName', ["; { yield* fields.map((f) { final namedArgs = { diff --git a/lib/src/cli/atlas_apps/baas_client.dart b/lib/src/cli/atlas_apps/baas_client.dart index 30cff1762..6bf66ab5f 100644 --- a/lib/src/cli/atlas_apps/baas_client.dart +++ b/lib/src/cli/atlas_apps/baas_client.dart @@ -18,7 +18,6 @@ import 'package:http/http.dart' as http; import 'dart:convert'; -import 'package:crypto/crypto.dart'; class BaasClient { static const String _confirmFuncSource = '''exports = async ({ token, tokenId, username }) => { diff --git a/lib/src/cli/metrics/metrics.g.dart b/lib/src/cli/metrics/metrics.g.dart index f8352e52b..d92e95bbc 100644 --- a/lib/src/cli/metrics/metrics.g.dart +++ b/lib/src/cli/metrics/metrics.g.dart @@ -61,7 +61,7 @@ Map _$PropertiesToJson(Properties instance) { val['Realm Version'] = instance.realmVersion; val['Host OS Type'] = instance.hostOsType; val['Host OS Version'] = instance.hostOsVersion; - val['Target OS Type'] = _$TargetOsTypeEnumMap[instance.targetOsType]; + writeNotNull('Target OS Type', _$TargetOsTypeEnumMap[instance.targetOsType]); writeNotNull('Target OS Version', instance.targetOsVersion); return val; } diff --git a/lib/src/configuration.dart b/lib/src/configuration.dart index 141f0df6d..45c7c9773 100644 --- a/lib/src/configuration.dart +++ b/lib/src/configuration.dart @@ -352,7 +352,7 @@ class InMemoryConfiguration extends Configuration { }) : super._(); } -/// A collection of properties describing the underlying schema of a [RealmObject]. +/// A collection of properties describing the underlying schema of a [RealmObjectBase]. /// /// {@category Configuration} class SchemaObject { @@ -365,8 +365,11 @@ class SchemaObject { /// Returns the name of this schema type. final String name; + /// Returns the base type of this schema object. + final ObjectType baseType; + /// Creates schema instance with object type and collection of object's properties. - const SchemaObject(this.type, this.name, this.properties); + const SchemaObject(this.baseType, this.type, this.name, this.properties); } /// Describes the complete set of classes which may be stored in a `Realm` @@ -405,3 +408,8 @@ class SyncClientResetErrorHandler { /// A client reset strategy where the user needs to fully take care of a client reset. typedef ManualSyncClientResetHandler = SyncClientResetErrorHandler; + +/// @nodoc +extension SchemaObjectInternal on SchemaObject { + bool get isGenericRealmObject => type == RealmObject || type == EmbeddedObject || type == RealmObjectBase; +} diff --git a/lib/src/list.dart b/lib/src/list.dart index 711b9367e..c000ce232 100644 --- a/lib/src/list.dart +++ b/lib/src/list.dart @@ -85,15 +85,16 @@ class ManagedRealmList with RealmEntity, ListMixin impleme @override bool remove(covariant T element) { - if (element is RealmObject && !element.isManaged) { + if (element is RealmObjectBase && !element.isManaged) { throw RealmStateError('Cannot call remove on a managed list with an element that is an unmanaged object'); } final index = indexOf(element); - final found = index > 0; - if (found) { - removeAt(index); + if (index < 0) { + return false; } - return found; + + removeAt(index); + return true; } @override @@ -122,7 +123,7 @@ class ManagedRealmList with RealmEntity, ListMixin impleme @override void insert(int index, T element) { - realmCore.listInsertElementAt(handle, index, element); + RealmListInternal.setValue(handle, realm, index, element, insert: true); } @override @@ -185,7 +186,7 @@ class UnmanagedRealmList extends collection.DelegatingList // The query operations on lists, as well as the ability to subscribe for notifications, // only work for list of objects (core restriction), so we add these as an extension methods // to allow the compiler to prevent misuse. -extension RealmListOfObject on RealmList { +extension RealmListOfObject on RealmList { /// Filters the list and returns a new [RealmResults] according to the provided [query] (with optional [arguments]). /// /// Only works for lists of [RealmObject]s. @@ -236,18 +237,33 @@ extension RealmListInternal on RealmList { static RealmList create(RealmListHandle handle, Realm realm, RealmObjectMetadata? metadata) => RealmList._(handle, realm, metadata); - static void setValue(RealmListHandle handle, Realm realm, int index, Object? value, {bool update = false}) { + static void setValue(RealmListHandle handle, Realm realm, int index, Object? value, {bool update = false, bool insert = false}) { if (index < 0) { - throw RealmException("Index out of range $index"); + throw RealmException("Index can not be negative: $index"); + } + + final length = realmCore.getListSize(handle); + if (index > length) { + throw RealmException('Index can not exceed the size of the list: $index, size: $length'); } try { + if (value is EmbeddedObject) { + if (value.isManaged) { + throw RealmError("Can't add to list an embedded object that is already managed"); + } + + final objHandle = + insert || index >= length ? realmCore.listInsertEmbeddedObjectAt(realm, handle, index) : realmCore.listSetEmbeddedObjectAt(realm, handle, index); + realm.manageEmbedded(objHandle, value); + return; + } + if (value is RealmObject && !value.isManaged) { realm.add(value, update: update); } - final length = realmCore.getListSize(handle); - if (index >= length) { + if (insert || index >= length) { realmCore.listInsertElementAt(handle, index, value); } else { realmCore.listSetElementAt(handle, index, value); diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index 6875c0937..193fb7d91 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -125,7 +125,7 @@ class _RealmCore { classInfo.num_properties = schemaObject.properties.length; classInfo.num_computed_properties = 0; classInfo.key = RLM_INVALID_CLASS_KEY; - classInfo.flags = realm_class_flags.RLM_CLASS_NORMAL; + classInfo.flags = schemaObject.baseType.toFlags(); final propertiesCount = schemaObject.properties.length; final properties = arena(propertiesCount); @@ -211,6 +211,7 @@ class _RealmCore { } else if (config is InMemoryConfiguration) { _realmLib.realm_config_set_in_memory(configHandle._pointer, true); } else if (config is FlexibleSyncConfiguration) { + _realmLib.realm_config_set_schema_mode(configHandle._pointer, realm_schema_mode.RLM_SCHEMA_MODE_ADDITIVE_EXPLICIT); final syncConfigPtr = _realmLib.invokeGetPointer(() => _realmLib.realm_flx_sync_config_new(config.user.handle._pointer)); try { _realmLib.realm_sync_config_set_session_stop_policy(syncConfigPtr, config.sessionStopPolicy.index); @@ -529,14 +530,16 @@ class _RealmCore { _realmLib.invokeGetBool(() => _realmLib.realm_get_class(realm.handle._pointer, classKey, classInfo)); final name = classInfo.ref.name.cast().toDartString(); - final schema = _getSchemaForClassKey(realm, classKey, name, arena, expectedSize: classInfo.ref.num_properties + classInfo.ref.num_computed_properties); + final baseType = ObjectTypeNative.fromFlags(classInfo.ref.flags); + final schema = + _getSchemaForClassKey(realm, classKey, name, baseType, arena, expectedSize: classInfo.ref.num_properties + classInfo.ref.num_computed_properties); schemas.add(schema); } return RealmSchema(schemas); } - SchemaObject _getSchemaForClassKey(Realm realm, int classKey, String name, Arena arena, {int expectedSize = 10}) { + SchemaObject _getSchemaForClassKey(Realm realm, int classKey, String name, ObjectType baseType, Arena arena, {int expectedSize = 10}) { final actualCount = arena(); final propertiesPtr = arena(expectedSize); _realmLib.invokeGetBool(() => _realmLib.realm_get_class_properties(realm.handle._pointer, classKey, propertiesPtr, expectedSize, actualCount)); @@ -544,7 +547,7 @@ class _RealmCore { if (expectedSize < actualCount.value) { // The supplied array was too small - resize it arena.free(propertiesPtr); - return _getSchemaForClassKey(realm, classKey, name, arena, expectedSize: actualCount.value); + return _getSchemaForClassKey(realm, classKey, name, baseType, arena, expectedSize: actualCount.value); } final result = []; @@ -553,7 +556,19 @@ class _RealmCore { result.add(property); } - return SchemaObject(RealmObject, name, result); + late Type type; + switch (baseType) { + case ObjectType.topLevel: + type = RealmObject; + break; + case ObjectType.embedded: + type = EmbeddedObject; + break; + default: + throw RealmError('$baseType is not supported yet'); + } + + return SchemaObject(baseType, type, name, result); } void deleteRealmFiles(String path) { @@ -645,19 +660,19 @@ class _RealmCore { _realmLib.invokeGetBool(() => _realmLib.realm_refresh(realm.handle._pointer), "Could not refresh"); } - RealmObjectMetadata getObjectMetadata(Realm realm, String className, Type classType) { + RealmObjectMetadata getObjectMetadata(Realm realm, SchemaObject schema) { return using((Arena arena) { final found = arena(); final classInfo = arena(); - _realmLib.invokeGetBool(() => _realmLib.realm_find_class(realm.handle._pointer, className.toCharPtr(arena), found, classInfo), - "Error getting class $className from realm at ${realm.config.path}"); + _realmLib.invokeGetBool(() => _realmLib.realm_find_class(realm.handle._pointer, schema.name.toCharPtr(arena), found, classInfo), + "Error getting class ${schema.name} from realm at ${realm.config.path}"); if (!found.value) { - throwLastError("Class $className not found in ${realm.config.path}"); + throwLastError("Class ${schema.name} not found in ${realm.config.path}"); } final primaryKey = classInfo.ref.primary_key.cast().toRealmDartString(treatEmptyAsNull: true); - return RealmObjectMetadata(className, classType, primaryKey, classInfo.ref.key, _getPropertyMetadata(realm, classInfo.ref.key, primaryKey)); + return RealmObjectMetadata(schema, classInfo.ref.key, _getPropertyMetadata(realm, classInfo.ref.key, primaryKey)); }); } @@ -693,6 +708,11 @@ class _RealmCore { return RealmObjectHandle._(realmPtr, realm.handle); } + RealmObjectHandle createEmbeddedObject(RealmObjectBase obj, int propertyKey) { + final realmPtr = _realmLib.invokeGetPointer(() => _realmLib.realm_set_embedded(obj.handle._pointer, propertyKey)); + return RealmObjectHandle._(realmPtr, obj.realm.handle); + } + RealmObjectHandle getOrCreateRealmObjectWithPrimaryKey(Realm realm, int classKey, Object? primaryKey) { return using((Arena arena) { final realm_value = _toRealmValue(primaryKey, arena); @@ -715,7 +735,7 @@ class _RealmCore { }); } - Object? getProperty(RealmObject object, int propertyKey) { + Object? getProperty(RealmObjectBase object, int propertyKey) { return using((Arena arena) { final realm_value = arena(); _realmLib.invokeGetBool(() => _realmLib.realm_get_value(object.handle._pointer, propertyKey, realm_value)); @@ -723,14 +743,14 @@ class _RealmCore { }); } - void setProperty(RealmObject object, int propertyKey, Object? value, bool isDefault) { + void setProperty(RealmObjectBase object, int propertyKey, Object? value, bool isDefault) { return using((Arena arena) { final realm_value = _toRealmValue(value, arena); _realmLib.invokeGetBool(() => _realmLib.realm_set_value(object.handle._pointer, propertyKey, realm_value.ref, isDefault)); }); } - String objectToString(RealmObject object) { + String objectToString(RealmObjectBase object) { return _realmLib.realm_object_to_string(object.handle._pointer).cast().toRealmDartString(freeRealmMemory: true)!; } @@ -917,7 +937,7 @@ class _RealmCore { }); } - _RealmLinkHandle _getObjectAsLink(RealmObject object) { + _RealmLinkHandle _getObjectAsLink(RealmObjectBase object) { final realmLink = _realmLib.realm_object_as_link(object.handle._pointer); return _RealmLinkHandle._(realmLink); } @@ -927,7 +947,7 @@ class _RealmCore { return RealmObjectHandle._(pointer, realm.handle); } - RealmListHandle getListProperty(RealmObject object, int propertyKey) { + RealmListHandle getListProperty(RealmObjectBase object, int propertyKey) { final pointer = _realmLib.invokeGetPointer(() => _realmLib.realm_get_list(object.handle._pointer, propertyKey)); return RealmListHandle._(pointer, object.realm.handle); } @@ -949,23 +969,31 @@ class _RealmCore { } void listSetElementAt(RealmListHandle handle, int index, Object? value) { - return using((Arena arena) { + using((Arena arena) { final realm_value = _toRealmValue(value, arena); _realmLib.invokeGetBool(() => _realmLib.realm_list_set(handle._pointer, index, realm_value.ref)); }); } void listInsertElementAt(RealmListHandle handle, int index, Object? value) { - return using((Arena arena) { + using((Arena arena) { final realm_value = _toRealmValue(value, arena); _realmLib.invokeGetBool(() => _realmLib.realm_list_insert(handle._pointer, index, realm_value.ref)); }); } + RealmObjectHandle listSetEmbeddedObjectAt(Realm realm, RealmListHandle handle, int index) { + final ptr = _realmLib.invokeGetPointer(() => _realmLib.realm_list_set_embedded(handle._pointer, index)); + return RealmObjectHandle._(ptr, realm.handle); + } + + RealmObjectHandle listInsertEmbeddedObjectAt(Realm realm, RealmListHandle handle, int index) { + final ptr = _realmLib.invokeGetPointer(() => _realmLib.realm_list_insert_embedded(handle._pointer, index)); + return RealmObjectHandle._(ptr, realm.handle); + } + void listRemoveElementAt(RealmListHandle handle, int index) { - return using((Arena arena) { - _realmLib.invokeGetBool(() => _realmLib.realm_list_erase(handle._pointer, index)); - }); + _realmLib.invokeGetBool(() => _realmLib.realm_list_erase(handle._pointer, index)); } void listDeleteAll(RealmList list) { @@ -1001,7 +1029,7 @@ class _RealmCore { return _realmLib.realm_equals(first._pointer.cast(), second._pointer.cast()); } - bool objectEquals(RealmObject first, RealmObject second) => _equals(first.handle, second.handle); + bool objectEquals(RealmObjectBase first, RealmObjectBase second) => _equals(first.handle, second.handle); bool realmEquals(Realm first, Realm second) => _equals(first.handle, second.handle); bool userEquals(User first, User second) => _equals(first.handle, second.handle); bool subscriptionEquals(Subscription first, Subscription second) => _equals(first.handle, second.handle); @@ -1011,7 +1039,7 @@ class _RealmCore { return RealmResultsHandle._(resultsPointer, results.realm.handle); } - bool objectIsValid(RealmObject object) { + bool objectIsValid(RealmObjectBase object) { return _realmLib.realm_object_is_valid(object.handle._pointer); } @@ -1093,7 +1121,7 @@ class _RealmCore { return RealmNotificationTokenHandle._(pointer, list.realm.handle); } - RealmNotificationTokenHandle subscribeObjectNotifications(RealmObject object, NotificationsController controller) { + RealmNotificationTokenHandle subscribeObjectNotifications(RealmObjectBase object, NotificationsController controller) { final pointer = _realmLib.invokeGetPointer(() => _realmLib.realm_object_add_notification_callback( object.handle._pointer, controller.toWeakHandle(), @@ -1848,7 +1876,7 @@ class _RealmCore { return; } if (errorCode != nullptr) { - // Throw RealmException instead of RealmError to be recoverable by the user. + // Throw RealmException instead of RealmError to be recoverable by the user. completer.completeError(RealmException(errorCode.toSyncError().toString())); } else { completer.complete(); @@ -1935,7 +1963,7 @@ class _RealmCore { return RealmResultsHandle._(ptr, frozenRealm.handle); } - RealmObjectHandle? resolveObject(RealmObject object, Realm frozenRealm) { + RealmObjectHandle? resolveObject(RealmObjectBase object, Realm frozenRealm) { return using((Arena arena) { final resultPtr = arena>(); _realmLib.invokeGetBool(() => _realmLib.realm_object_resolve_in(object.handle._pointer, frozenRealm.handle._pointer, resultPtr)); @@ -2399,8 +2427,8 @@ void _intoRealmQueryArg(Object? value, Pointer realm_query_ar void _intoRealmValue(Object? value, Pointer realm_value, Allocator allocator) { if (value == null) { realm_value.ref.type = realm_value_type.RLM_TYPE_NULL; - } else if (value is RealmObject) { - //when converting a RealmObject to realm_value.link we assume the object is managed + } else if (value is RealmObjectBase) { + // when converting a RealmObjectBase to realm_value.link we assume the object is managed final link = realmCore._getObjectAsLink(value); realm_value.ref.values.link.target = link.targetKey; realm_value.ref.values.link.target_table = link.classKey; @@ -2474,8 +2502,7 @@ extension on Pointer { case realm_value_type.RLM_TYPE_LINK: final objectKey = ref.values.link.target; final classKey = ref.values.link.target_table; - RealmObjectHandle handle = realmCore._getObject(realm, classKey, objectKey); - return handle; + return realmCore._getObject(realm, classKey, objectKey); case realm_value_type.RLM_TYPE_BINARY: throw Exception("Not implemented"); case realm_value_type.RLM_TYPE_TIMESTAMP: @@ -2600,6 +2627,30 @@ extension on Completer { } } +extension ObjectTypeNative on ObjectType { + int toFlags() { + switch (this) { + case ObjectType.topLevel: + return realm_class_flags.RLM_CLASS_NORMAL; + case ObjectType.embedded: + return realm_class_flags.RLM_CLASS_EMBEDDED; + default: + throw RealmException('Invalid ObjectType: $this'); + } + } + + static ObjectType fromFlags(int flags) { + switch (flags) { + case realm_class_flags.RLM_CLASS_NORMAL: + return ObjectType.topLevel; + case realm_class_flags.RLM_CLASS_EMBEDDED: + return ObjectType.embedded; + default: + throw RealmException('Invalid ObjectType: $flags'); + } + } +} + enum _CustomErrorCode { noError(0), unknownHttp(998), diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index e57eda1b1..86b68be7e 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -50,6 +50,7 @@ export 'package:realm_common/realm_common.dart' GeneralSyncError, SyncErrorCategory, GeneralSyncErrorCode, + ObjectType, SyncClientErrorCode, SyncConnectionErrorCode, SyncSessionErrorCode, @@ -80,7 +81,8 @@ export "configuration.dart" export 'credentials.dart' show Credentials, AuthProviderType, EmailPasswordAuthProvider; export 'list.dart' show RealmList, RealmListOfObject, RealmListChanges; -export 'realm_object.dart' show RealmEntity, RealmException, UserCallbackException, RealmObject, RealmObjectChanges, DynamicRealmObject; +export 'realm_object.dart' + show RealmEntity, RealmException, UserCallbackException, RealmObject, RealmObjectBase, EmbeddedObject, RealmObjectChanges, DynamicRealmObject; export 'realm_property.dart'; export 'results.dart' show RealmResults, RealmResultsChanges; export 'subscription.dart' show Subscription, SubscriptionSet, SubscriptionSetState, MutableSubscriptionSet; @@ -165,7 +167,7 @@ class Realm implements Finalizable { void _populateMetadata() { schema = config.schemaObjects.isNotEmpty ? RealmSchema(config.schemaObjects) : realmCore.readSchema(this); - _metadata = RealmMetadata._(schema.map((c) => realmCore.getObjectMetadata(this, c.name, c.type))); + _metadata = RealmMetadata._(schema.map((c) => realmCore.getObjectMetadata(this, c))); } /// Deletes all files associated with a `Realm` located at given [path] @@ -225,7 +227,7 @@ class Realm implements Finalizable { return object; } - RealmObjectHandle _createObject(RealmObject object, RealmObjectMetadata metadata, bool update) { + RealmObjectHandle _createObject(RealmObjectBase object, RealmObjectMetadata metadata, bool update) { final key = metadata.classKey; final primaryKey = metadata.primaryKey; if (primaryKey == null) { @@ -561,7 +563,7 @@ extension RealmInternal on Realm { return Realm._(config, handle, isInMigration); } - RealmObject createObject(Type type, RealmObjectHandle handle, RealmObjectMetadata metadata) { + RealmObjectBase createObject(Type type, RealmObjectHandle handle, RealmObjectMetadata metadata) { final accessor = RealmCoreAccessor(metadata, _isInMigration); return RealmObjectInternal.create(type, this, handle, accessor); } @@ -584,7 +586,21 @@ extension RealmInternal on Realm { RealmMetadata get metadata => _metadata; - T? resolveObject(T object) { + void manageEmbedded(RealmObjectHandle handle, EmbeddedObject object, {bool update = false}) { + final metadata = _metadata.getByType(object.runtimeType); + + final accessor = RealmCoreAccessor(metadata, _isInMigration); + object.manage(this, handle, accessor, update); + } + + /// This should only be used for testing + RealmResults allEmbedded() { + final metadata = _metadata.getByType(T); + final handle = realmCore.findAll(this, metadata.classKey); + return RealmResultsInternal.create(handle, this, metadata); + } + + T? resolveObject(T object) { if (!object.isManaged) { throw RealmStateError("Can't resolve unmanaged objects"); } @@ -612,7 +628,7 @@ extension RealmInternal on Realm { return createList(handle, list.metadata); } - RealmResults resolveResults(RealmResults results) { + RealmResults resolveResults(RealmResults results) { final handle = realmCore.resolveResults(results, this); return RealmResultsInternal.create(handle, this, results.metadata); } @@ -708,10 +724,10 @@ class RealmMetadata { RealmMetadata._(Iterable objectMetadatas) { for (final metadata in objectMetadatas) { - if (metadata.type != RealmObject) { - _typeMap[metadata.type] = metadata; + if (!metadata.schema.isGenericRealmObject) { + _typeMap[metadata.schema.type] = metadata; } else { - _stringMap[metadata.name] = metadata; + _stringMap[metadata.schema.name] = metadata; } } } @@ -728,7 +744,7 @@ class RealmMetadata { RealmObjectMetadata getByName(String type) { var metadata = _stringMap[type]; if (metadata == null) { - metadata = _typeMap.values.firstWhereOrNull((v) => v.name == type); + metadata = _typeMap.values.firstWhereOrNull((v) => v.schema.name == type); if (metadata == null) { throw RealmError("Object type $type not configured in the current Realm's schema. Add type $type to your config before opening the Realm"); } @@ -768,7 +784,7 @@ class DynamicRealm { } final accessor = RealmCoreAccessor(metadata, _realm._isInMigration); - return RealmObjectInternal.create(RealmObject, _realm, handle, accessor); + return RealmObjectInternal.create(RealmObject, _realm, handle, accessor) as RealmObject; } } diff --git a/lib/src/realm_object.dart b/lib/src/realm_object.dart index e8129e2f9..ef13acaca 100644 --- a/lib/src/realm_object.dart +++ b/lib/src/realm_object.dart @@ -18,21 +18,23 @@ import 'dart:async'; import 'dart:ffi'; -import 'dart:io'; + +import 'package:collection/collection.dart'; import 'list.dart'; import 'native/realm_core.dart'; import 'realm_class.dart'; +import 'configuration.dart'; typedef DartDynamic = dynamic; abstract class RealmAccessor { - Object? get(RealmObject object, String name); - void set(RealmObject object, String name, Object? value, {bool isDefault = false, bool update = false}); + Object? get(RealmObjectBase object, String name); + void set(RealmObjectBase object, String name, Object? value, {bool isDefault = false, bool update = false}); static final Map> _defaultValues = >{}; - static void setDefaults(Map values) { + static void setDefaults(Map values) { _defaultValues[T] = values; } @@ -63,7 +65,7 @@ class RealmValuesAccessor implements RealmAccessor { final Map _values = {}; @override - Object? get(RealmObject object, String name) { + Object? get(RealmObjectBase object, String name) { if (!_values.containsKey(name)) { return RealmAccessor.getDefaultValue(object.runtimeType, name); } @@ -72,11 +74,11 @@ class RealmValuesAccessor implements RealmAccessor { } @override - void set(RealmObject object, String name, Object? value, {bool isDefault = false, bool update = false}) { + void set(RealmObjectBase object, String name, Object? value, {bool isDefault = false, bool update = false}) { _values[name] = value; } - void setAll(RealmObject object, RealmAccessor accessor, bool update) { + void setAll(RealmObjectBase object, RealmAccessor accessor, bool update) { final defaults = RealmAccessor.getDefaults(object.runtimeType); if (defaults != null) { @@ -96,15 +98,14 @@ class RealmValuesAccessor implements RealmAccessor { class RealmObjectMetadata { final int classKey; - final String name; - final Type type; - final String? primaryKey; + final SchemaObject schema; + late final String? primaryKey = schema.properties.firstWhereOrNull((element) => element.primaryKey)?.name; final Map _propertyKeys; - String get _realmObjectTypeName => type == RealmObject ? name : type.toString(); + String get _realmObjectTypeName => schema.isGenericRealmObject ? schema.name : schema.type.toString(); - RealmObjectMetadata(this.name, this.type, this.primaryKey, this.classKey, this._propertyKeys); + RealmObjectMetadata(this.schema, this.classKey, this._propertyKeys); RealmPropertyMetadata operator [](String propertyName) => _propertyKeys[propertyName] ?? (throw RealmException("Property $propertyName does not exist on class $_realmObjectTypeName")); @@ -137,7 +138,7 @@ class RealmCoreAccessor implements RealmAccessor { RealmCoreAccessor(this.metadata, this.isInMigration); @override - Object? get(RealmObject object, String name) { + Object? get(RealmObjectBase object, String name) { try { final propertyMeta = metadata[name]; if (propertyMeta.collectionType == RealmCollectionType.list) { @@ -148,7 +149,14 @@ class RealmCoreAccessor implements RealmAccessor { // called with a generic object arg - get we construct a list of // RealmObjects since we don't know the type of the object. if (listMetadata != null && _isTypeGenericObject()) { - return object.realm.createList(handle, listMetadata); + switch (listMetadata.schema.baseType) { + case ObjectType.topLevel: + return object.realm.createList(handle, listMetadata); + case ObjectType.embedded: + return object.realm.createList(handle, listMetadata); + default: + throw RealmError('List of ${listMetadata.schema.baseType} is not supported yet'); + } } return object.realm.createList(handle, listMetadata); @@ -162,7 +170,7 @@ class RealmCoreAccessor implements RealmAccessor { // If we have an object but the user called the API without providing a generic // arg, we construct a RealmObject since we don't know the type of the object. if (_isTypeGenericObject()) { - return object.realm.createObject(RealmObject, value, targetMetadata); + return object.realm.createObject(RealmObjectBase, value, targetMetadata); } return object.realm.createObject(T, value, targetMetadata); @@ -175,7 +183,7 @@ class RealmCoreAccessor implements RealmAccessor { } @override - void set(RealmObject object, String name, Object? value, {bool isDefault = false, bool update = false}) { + void set(RealmObjectBase object, String name, Object? value, {bool isDefault = false, bool update = false}) { final propertyMeta = metadata[name]; try { if (value is RealmList) { @@ -187,6 +195,16 @@ class RealmCoreAccessor implements RealmAccessor { return; } + if (value is EmbeddedObject) { + if (value.isManaged) { + throw RealmError("Can't set an embedded object that is already managed"); + } + + final handle = realmCore.createEmbeddedObject(object, propertyMeta.key); + object.realm.manageEmbedded(handle, value, update: update); + return; + } + if (value is RealmObject && !value.isManaged) { object.realm.add(value, update: update); } @@ -230,28 +248,30 @@ extension RealmEntityInternal on RealmEntity { /// /// [RealmObject] should not be used directly as it is part of the generated class hierarchy. ex: `MyClass extends _MyClass with RealmObject`. /// {@category Realm} -mixin RealmObject on RealmEntity implements Finalizable { +mixin RealmObjectBase on RealmEntity implements Finalizable { RealmObjectHandle? _handle; RealmAccessor _accessor = RealmValuesAccessor(); - static final Map _factories = { + static final Map _factories = { // Register default factories for `RealmObject` and `RealmObject?`. Whenever the user // asks for these types, we'll use the ConcreteRealmObject implementation. RealmObject: () => _ConcreteRealmObject(), _typeOf(): () => _ConcreteRealmObject(), + EmbeddedObject: () => _ConcreteEmbeddedObject(), + _typeOf(): () => _ConcreteEmbeddedObject(), }; /// @nodoc - static Object? get(RealmObject object, String name) { + static Object? get(RealmObjectBase object, String name) { return object._accessor.get(object, name); } /// @nodoc - static void set(RealmObject object, String name, T? value, {bool update = false}) { + static void set(RealmObjectBase object, String name, T? value, {bool update = false}) { object._accessor.set(object, name, value, update: update); } /// @nodoc - static void registerFactory(T Function() factory) { + static void registerFactory(T Function() factory) { // We register a factory for both the type itself, but also the nullable // version of the type. _factories.putIfAbsent(T, () => factory); @@ -259,11 +279,24 @@ mixin RealmObject on RealmEntity implements Finalizable { } /// @nodoc - static T create() { - if (!_factories.containsKey(T)) { - throw RealmException("Factory for Realm object type $T not found"); + static RealmObjectBase createObject(Type type, RealmObjectMetadata metadata) { + final factory = _factories[type]; + if (factory == null) { + if (type == RealmObjectBase) { + switch (metadata.schema.baseType) { + case ObjectType.topLevel: + return _ConcreteRealmObject(); + case ObjectType.embedded: + return _ConcreteEmbeddedObject(); + default: + throw RealmException("ObjectType ${metadata.schema.baseType} not supported"); + } + } + + throw RealmException("Factory for Realm object type $type not found"); } - return _factories[T]!() as T; + + return factory(); } /// @nodoc @@ -273,7 +306,7 @@ mixin RealmObject on RealmEntity implements Finalizable { } /// @nodoc - static T freezeObject(T object) { + static T freezeObject(T object) { if (!object.isManaged) { throw RealmStateError("Can't freeze unmanaged objects."); } @@ -311,10 +344,10 @@ mixin RealmObject on RealmEntity implements Finalizable { /// Returns a [Stream] of [RealmObjectChanges] that can be listened to. /// /// If the object is not managed a [RealmStateError] is thrown. - Stream> get changes => throw RealmError("Invalid usage. Use the generated inheritors of RealmObject"); + Stream> get changes => throw RealmError("Invalid usage. Use the generated inheritors of RealmObject"); /// @nodoc - static Stream> getChanges(T object) { + static Stream> getChanges(T object) { if (!object.isManaged) { throw RealmStateError("Object is not managed"); } @@ -364,12 +397,18 @@ mixin RealmObject on RealmEntity implements Finalizable { late final DynamicRealmObject dynamic = DynamicRealmObject._(this); /// Creates a frozen snapshot of this [RealmObject]. - RealmObject freeze() => freezeObject(this); + RealmObjectBase freeze() => freezeObject(this); } +/// @nodoc +mixin RealmObject on RealmObjectBase {} + +/// @nodoc +mixin EmbeddedObject on RealmObjectBase {} + /// @nodoc //RealmObject package internal members -extension RealmObjectInternal on RealmObject { +extension RealmObjectInternal on RealmObjectBase { @pragma('vm:never-inline') void keepAlive() { _realm?.keepAlive(); @@ -392,12 +431,8 @@ extension RealmObjectInternal on RealmObject { _accessor = accessor; } - static RealmObject create(Type type, Realm realm, RealmObjectHandle handle, RealmCoreAccessor accessor) { - if (!RealmObject._factories.containsKey(type)) { - throw Exception("Factory for object type $type not found."); - } - - final object = RealmObject._factories[type]!(); + static RealmObjectBase create(Type type, Realm realm, RealmObjectHandle handle, RealmCoreAccessor accessor) { + final object = RealmObjectBase.createObject(type, accessor.metadata); object._handle = handle; object._accessor = accessor; object._realm = realm; @@ -439,7 +474,7 @@ class UserCallbackException extends RealmException { } /// Describes the changes in on a single RealmObject since the last time the notification callback was invoked. -class RealmObjectChanges implements Finalizable { +class RealmObjectChanges implements Finalizable { // ignore: unused_field final RealmObjectChangesHandle _handle; @@ -467,7 +502,7 @@ extension RealmObjectChangesInternal on RealmObjectChange } /// @nodoc -class RealmObjectNotificationsController extends NotificationsController { +class RealmObjectNotificationsController extends NotificationsController { T realmObject; late final StreamController> streamController; @@ -500,7 +535,10 @@ class RealmObjectNotificationsController extends Notifica } /// @nodoc -class _ConcreteRealmObject with RealmEntity, RealmObject {} +class _ConcreteRealmObject with RealmEntity, RealmObjectBase, RealmObject {} + +/// @nodoc +class _ConcreteEmbeddedObject with RealmEntity, RealmObjectBase, EmbeddedObject {} // This is necessary whenever we need to pass T? as the type. Type _typeOf() => T; @@ -512,7 +550,7 @@ bool _isTypeGenericObject() => T == Object || T == _typeOf(); /// /// {@category Realm} class DynamicRealmObject { - final RealmObject _obj; + final RealmObjectBase _obj; DynamicRealmObject._(this._obj); @@ -521,7 +559,7 @@ class DynamicRealmObject { /// in [Object]. T get(String name) { _validatePropertyType(name, RealmCollectionType.none); - return RealmObject.get(_obj, name) as T; + return RealmObjectBase.get(_obj, name) as T; } /// Gets a list by the property name. If a generic type is specified, the property @@ -529,7 +567,7 @@ class DynamicRealmObject { /// returned. RealmList getList(String name) { _validatePropertyType(name, RealmCollectionType.list); - return RealmObject.get(_obj, name) as RealmList; + return RealmObjectBase.get(_obj, name) as RealmList; } RealmPropertyMetadata? _validatePropertyType(String name, RealmCollectionType expectedCollectionType) { @@ -537,25 +575,25 @@ class DynamicRealmObject { if (accessor is RealmCoreAccessor) { final prop = accessor.metadata._propertyKeys[name]; if (prop == null) { - throw RealmException("Property '$name' does not exist on class '${accessor.metadata.name}'"); + throw RealmException("Property '$name' does not exist on class '${accessor.metadata.schema.name}'"); } if (prop.collectionType != expectedCollectionType) { throw RealmException( - "Property '$name' on class '${accessor.metadata.name}' is '${prop.collectionType}' but the method used to access it expected '$expectedCollectionType'."); + "Property '$name' on class '${accessor.metadata.schema.name}' is '${prop.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 (T != _typeOf() && prop.isNullable != null is T) { throw RealmException( - "Property '$name' on class '${accessor.metadata.name}' is ${prop.isNullable ? 'nullable' : 'required'} but the generic argument passed to get is $T."); + "Property '$name' on class '${accessor.metadata.schema.name}' is ${prop.isNullable ? 'nullable' : 'required'} but the generic argument passed to get is $T."); } final targetType = _getPropertyType(); if (targetType != null && targetType != prop.propertyType) { throw RealmException( - "Property '$name' on class '${accessor.metadata.name}' is not the correct type. Expected '$targetType', got '${prop.propertyType}'."); + "Property '$name' on class '${accessor.metadata.schema.name}' is not the correct type. Expected '$targetType', got '${prop.propertyType}'."); } return prop; diff --git a/lib/src/results.dart b/lib/src/results.dart index c58e464a6..76565eceb 100644 --- a/lib/src/results.dart +++ b/lib/src/results.dart @@ -29,11 +29,11 @@ import 'realm_object.dart'; /// added to or deleted from the Realm that match the underlying query. /// /// {@category Realm} -class RealmResults extends collection.IterableBase with RealmEntity implements Finalizable { +class RealmResults extends collection.IterableBase with RealmEntity implements Finalizable { final RealmObjectMetadata? _metadata; final RealmResultsHandle _handle; - final _supportsSnapshot = [] is List; + final _supportsSnapshot = [] is List; RealmResults._(this._handle, Realm realm, this._metadata) { setRealm(realm); @@ -112,13 +112,13 @@ extension RealmResultsInternal on RealmResults { RealmObjectMetadata? get metadata => _metadata; - static RealmResults create(RealmResultsHandle handle, Realm realm, RealmObjectMetadata? metadata) { + static RealmResults create(RealmResultsHandle handle, Realm realm, RealmObjectMetadata? metadata) { return RealmResults._(handle, realm, metadata); } } /// Describes the changes in a Realm results collection since the last time the notification callback was invoked. -class RealmResultsChanges extends RealmCollectionChanges { +class RealmResultsChanges extends RealmCollectionChanges { /// The results collection being monitored for changes. final RealmResults results; @@ -126,7 +126,7 @@ class RealmResultsChanges extends RealmCollectionChanges } /// @nodoc -class ResultsNotificationsController extends NotificationsController { +class ResultsNotificationsController extends NotificationsController { final RealmResults results; late final StreamController> streamController; @@ -158,7 +158,7 @@ class ResultsNotificationsController extends Notification } } -class _RealmResultsIterator implements Iterator { +class _RealmResultsIterator implements Iterator { final RealmResults _results; int _index; T? _current; diff --git a/test/dynamic_realm_test.dart b/test/dynamic_realm_test.dart index 2948b2450..e7ba708d7 100644 --- a/test/dynamic_realm_test.dart +++ b/test/dynamic_realm_test.dart @@ -44,19 +44,36 @@ Future main([List? args]) async { } test('schema is read from disk', () { - final config = Configuration.local([Car.schema, Dog.schema, Person.schema, AllTypes.schema, LinksClass.schema]); + final config = Configuration.local([ + Car.schema, + Dog.schema, + Person.schema, + AllTypes.schema, + LinksClass.schema, + ObjectWithEmbedded.schema, + AllTypesEmbedded.schema, + RecursiveEmbedded1.schema, + RecursiveEmbedded2.schema, + RecursiveEmbedded3.schema + ]); + getRealm(config).close(); final dynamicConfig = Configuration.local([]); final realm = getRealm(dynamicConfig); - expect(realm.schema.length, 5); + expect(realm.schema.length, 10); _assertSchemaExists(realm, Car.schema); _assertSchemaExists(realm, Dog.schema); _assertSchemaExists(realm, Person.schema); _assertSchemaExists(realm, AllTypes.schema); _assertSchemaExists(realm, LinksClass.schema); + _assertSchemaExists(realm, ObjectWithEmbedded.schema); + _assertSchemaExists(realm, AllTypesEmbedded.schema); + _assertSchemaExists(realm, RecursiveEmbedded1.schema); + _assertSchemaExists(realm, RecursiveEmbedded2.schema); + _assertSchemaExists(realm, RecursiveEmbedded3.schema); }); test('dynamic is always the same', () { diff --git a/test/embedded_test.dart b/test/embedded_test.dart new file mode 100644 index 000000000..8eec5cb26 --- /dev/null +++ b/test/embedded_test.dart @@ -0,0 +1,537 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Copyright 2022 Realm Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// + +// ignore_for_file: unused_local_variable + +import 'package:test/test.dart' hide test, throws; + +import '../lib/realm.dart'; +import '../lib/src/realm_object.dart'; +import 'test.dart'; + +// This is required to be able to use the API for querying embedded objects. +import '../lib/src/realm_class.dart' show RealmInternal; + +Future main([List? args]) async { + await setupTests(args); + + Realm getLocalRealm() { + final config = Configuration.local( + [AllTypesEmbedded.schema, ObjectWithEmbedded.schema, RecursiveEmbedded1.schema, RecursiveEmbedded2.schema, RecursiveEmbedded3.schema]); + return getRealm(config); + } + + Future getSyncRealm(AppConfiguration config) async { + final app = App(config); + final user = await getAnonymousUser(app); + final realmConfig = Configuration.flexibleSync( + user, [AllTypesEmbedded.schema, ObjectWithEmbedded.schema, RecursiveEmbedded1.schema, RecursiveEmbedded2.schema, RecursiveEmbedded3.schema]); + return getRealm(realmConfig); + } + + test('local Realm with orphan embedded schemas works', () { + final config = Configuration.local([AllTypesEmbedded.schema]); + final realm = getRealm(config); + realm.close(); + + final dynamicRealm = getRealm(Configuration.local([], path: config.path)); + + final schema = dynamicRealm.schema; + expect(schema.single.baseType, ObjectType.embedded); + }); + + baasTest('synchronized Realm with orphan embedded schemas throws', (configuration) async { + final app = App(configuration); + final user = await getIntegrationUser(app); + final config = Configuration.flexibleSync(user, [AllTypesEmbedded.schema]); + + expect(() => getRealm(config), throws("Embedded object 'AllTypesEmbedded' is unreachable by any link path from top level objects")); + }); + + test('Embedded object roundtrip', () { + final realm = getLocalRealm(); + + final now = DateTime.now(); + final oid = ObjectId(); + final uuid = Uuid.v4(); + realm.write(() { + realm.add(ObjectWithEmbedded('abc', singleObject: AllTypesEmbedded('str', true, now, 1.23, oid, uuid, 99))); + }); + + final obj = realm.all().single; + final json = obj.toJson(); + + expect(json, contains('"stringProp":"str"')); + expect(json, contains('"boolProp":true')); + expect(json, contains('"dateProp":"${now.toNormalizedDateString()}"')); + expect(json, contains('"doubleProp":1.23')); + expect(json, contains('"objectIdProp":"$oid"')); + expect(json, contains('"uuidProp":"$uuid"')); + expect(json, contains('"intProp":99')); + + expect(json, contains('"nullableStringProp":null')); + expect(json, contains('"nullableBoolProp":null')); + expect(json, contains('"nullableDateProp":null')); + expect(json, contains('"nullableDoubleProp":null')); + expect(json, contains('"nullableObjectIdProp":null')); + expect(json, contains('"nullableUuidProp":null')); + expect(json, contains('"nullableIntProp":null')); + }); + + test('Embedded object get/set properties', () { + final config = Configuration.local( + [AllTypesEmbedded.schema, ObjectWithEmbedded.schema, RecursiveEmbedded1.schema, RecursiveEmbedded2.schema, RecursiveEmbedded3.schema]); + final realm = getRealm(config); + + final graph = ObjectWithEmbedded('TopLevel1', + recursiveObject: RecursiveEmbedded1('Child1', child: RecursiveEmbedded2('Child2'), children: [ + RecursiveEmbedded2('List1', child: RecursiveEmbedded3('Child3'), topLevel: ObjectWithEmbedded('TopLeve2')), + RecursiveEmbedded2('List2'), + ])); + + // Make a cycle + graph.recursiveObject!.child!.topLevel = graph; + realm.write(() { + realm.add(graph); + }); + + expect(graph.isManaged, true); + expect(graph.recursiveObject!.isManaged, true); + + final refetched = realm.all().first; + + expect(refetched, graph); + expect(refetched.id, 'TopLevel1'); + + final child1 = refetched.recursiveObject; + expect(child1, isNotNull); + expect(child1!.isManaged, true); + expect(child1.value, 'Child1'); + expect(child1.topLevel, isNull); + + final child2 = child1.child; + expect(child2, isNotNull); + expect(child2!.isManaged, true); + expect(child2.value, 'Child2'); + expect(child2.topLevel, refetched); + expect(child2.child, isNull); + expect(child2.children, isEmpty); + + final listChild1 = child1.children[0]; + expect(listChild1.isManaged, true); + expect(listChild1.value, 'List1'); + expect(listChild1.child!.value, 'Child3'); + expect(listChild1.topLevel!.id, 'TopLeve2'); + + final listChild2 = child1.children[1]; + expect(listChild2.isManaged, true); + expect(listChild2.value, 'List2'); + expect(listChild2.child, isNull); + }); + + test('Embedded object when replaced invalidates old object', () { + final realm = getLocalRealm(); + final topLevel = realm.write(() { + return realm.add(ObjectWithEmbedded('', recursiveObject: RecursiveEmbedded1('first'))); + }); + + final firstEmbeded = topLevel.recursiveObject!; + expect(firstEmbeded.isManaged, true); + expect(firstEmbeded.isValid, true); + expect(firstEmbeded.value, 'first'); + + realm.write(() { + topLevel.recursiveObject = RecursiveEmbedded1('second'); + }); + + // We replaced firstEmbedded with another one, so we expect it to be invalid + expect(firstEmbeded.isManaged, true); + expect(firstEmbeded.isValid, false); + + final secondEmbedded = topLevel.recursiveObject!; + expect(secondEmbedded.value, 'second'); + + realm.write(() { + topLevel.recursiveObject = null; + }); + + // We replaced secondEmbedded with null, so we expect it to be invalid + expect(secondEmbedded.isManaged, true); + expect(secondEmbedded.isValid, false); + + expect(topLevel.recursiveObject, isNull); + }); + + test('Embedded object in list when replaced invalidates old object', () { + final realm = getLocalRealm(); + final topLevel = realm.write(() { + return realm.add(ObjectWithEmbedded('', recursiveList: [RecursiveEmbedded1('first'), RecursiveEmbedded1('second'), RecursiveEmbedded1('third')])); + }); + + final list = topLevel.recursiveList; + + final first = list[0]; + + realm.write(() { + list.remove(first); + }); + + expect(list.length, 2); + expect(first.isManaged, true); + expect(first.isValid, false); + + final second = list[0]; + realm.write(() { + list.removeAt(0); + }); + + expect(list.length, 1); + expect(second.isManaged, true); + expect(second.isValid, false); + + final third = list.first; + realm.write(() { + list[0] = RecursiveEmbedded1('fourth'); + }); + + expect(list.length, 1); + expect(third.isManaged, true); + expect(third.isValid, false); + + final fourth = list.first; + + realm.write(() { + list.clear(); + }); + + expect(list.length, 0); + expect(fourth.isManaged, true); + expect(fourth.isValid, false); + }); + + test('Embedded list .add', () { + final realm = getLocalRealm(); + + final obj = realm.write(() { + return realm.add(ObjectWithEmbedded('')); + }); + + realm.write(() { + obj.recursiveList.add(RecursiveEmbedded1('1')); + obj.recursiveList.add(RecursiveEmbedded1('2')); + }); + + expect(obj.recursiveList.length, 2); + expect(obj.recursiveList[0].value, '1'); + expect(obj.recursiveList[1].value, '2'); + }); + + test('Embedded list .insert', () { + final realm = getLocalRealm(); + + final obj = realm.write(() { + return realm.add(ObjectWithEmbedded('')); + }); + + realm.write(() { + obj.recursiveList.insert(0, RecursiveEmbedded1('1')); + obj.recursiveList.insert(0, RecursiveEmbedded1('2')); + }); + + expect(obj.recursiveList.length, 2); + expect(obj.recursiveList[0].value, '2'); + expect(obj.recursiveList[1].value, '1'); + }, skip: 'Needs https://github.com/realm/realm-dart/pull/894'); + + test('Embedded list .set', () { + final realm = getLocalRealm(); + + final obj = realm.write(() { + return realm.add(ObjectWithEmbedded('')); + }); + + realm.write(() { + obj.recursiveList[0] = RecursiveEmbedded1('1'); + }); + + expect(obj.recursiveList.length, 1); + expect(obj.recursiveList[0].value, '1'); + + realm.write(() { + obj.recursiveList[0] = RecursiveEmbedded1('5'); + }); + + expect(obj.recursiveList.length, 1); + expect(obj.recursiveList[0].value, '5'); + }); + + test('Embedded property when set to a managed object fails', () { + final realm = getLocalRealm(); + final parent = realm.write(() { + return realm.add(ObjectWithEmbedded('1', recursiveObject: RecursiveEmbedded1('1'))); + }); + + realm.write(() { + expect(() => realm.add(ObjectWithEmbedded('2', recursiveObject: parent.recursiveObject)), throws()); + }); + + realm.write(() { + expect(() => parent.recursiveObject = parent.recursiveObject, throws()); + }); + }); + + test('Embedded list when adding managed object fails', () { + final realm = getLocalRealm(); + final parent = realm.write(() { + return realm.add(ObjectWithEmbedded('', recursiveList: [RecursiveEmbedded1('1')], recursiveObject: RecursiveEmbedded1('2'))); + }); + + final list = parent.recursiveList; + + realm.write(() { + expect(() => list.add(list[0]), throws()); + expect(() => list.add(parent.recursiveObject!), throws()); + expect(() => list[0] = list[0], throws()); + expect(() => list[0] = parent.recursiveObject!, throws()); + expect(() => list.insert(0, list[0]), throws()); + expect(() => list.insert(0, parent.recursiveObject!), throws()); + }); + }); + + test('Parent with embedded addOrUpdate invalidates old embedded', () { + final realm = getLocalRealm(); + + final parent = realm.write(() { + return realm.add(ObjectWithEmbedded('123', recursiveList: [RecursiveEmbedded1('list')], recursiveObject: RecursiveEmbedded1('link'))); + }); + + final listObj = parent.recursiveList[0]; + final obj = parent.recursiveObject!; + + expect(listObj.isValid, true); + expect(obj.isValid, true); + + realm.write(() { + realm.add(ObjectWithEmbedded('123', recursiveList: [RecursiveEmbedded1('list 2')], recursiveObject: RecursiveEmbedded1('link 2')), update: true); + }); + + expect(listObj.isValid, false); + expect(obj.isValid, false); + + expect(parent.recursiveList[0].value, 'list 2'); + expect(parent.recursiveObject!.value, 'link 2'); + }); + + test('Parent with embedded addOrUpdate correctly propagates graph', () { + final realm = getLocalRealm(); + + final parent = realm.write(() { + return realm.add(ObjectWithEmbedded('123', recursiveList: [RecursiveEmbedded1('list')], recursiveObject: RecursiveEmbedded1('link'))); + }); + + final listObj = parent.recursiveList[0]; + final obj = parent.recursiveObject!; + + expect(listObj.isValid, true); + expect(obj.isValid, true); + + final newGraph = ObjectWithEmbedded('456', + recursiveObject: RecursiveEmbedded1('embedded 456', topLevel: ObjectWithEmbedded('123', recursiveList: [RecursiveEmbedded1('value')]))); + + realm.write(() { + realm.add(newGraph, update: true); + }); + + expect(realm.all().length, 2); + + expect(listObj.isValid, false); + expect(obj.isValid, false); + + expect(parent.recursiveList[0].value, 'value'); + expect(parent.recursiveObject, isNull); + }); + + test('Parent when deleted cleans up embedded graph', () { + final realm = getLocalRealm(); + + final parent = ObjectWithEmbedded('123', + recursiveList: [ + RecursiveEmbedded1('1.1', child: RecursiveEmbedded2('2.1', child: RecursiveEmbedded3('3.1'), children: [RecursiveEmbedded3('3.2')])), + RecursiveEmbedded1('1.2', child: RecursiveEmbedded2('2.2', children: [RecursiveEmbedded3('3.3'), RecursiveEmbedded3('3.4')])), + ], + recursiveObject: RecursiveEmbedded1('1.3', child: RecursiveEmbedded2('2.3'))); + + realm.write(() { + realm.add(parent); + }); + + final embedded1s = realm.allEmbedded(); + final embedded2s = realm.allEmbedded(); + final embedded3s = realm.allEmbedded(); + + expect(embedded1s.length, 3); + expect(embedded2s.length, 3); + expect(embedded3s.length, 4); + + realm.write(() { + realm.delete(parent); + }); + + expect(embedded1s.length, 0); + expect(embedded2s.length, 0); + expect(embedded3s.length, 0); + }); + + baasTest('Embedded objects synchronization', (config) async { + final realm1 = await getSyncRealm(config); + + final differentiator = Uuid.v4(); + realm1.subscriptions.update((mutableSubscriptions) { + mutableSubscriptions.add(realm1.query(r'differentiator = $0', [differentiator])); + }); + + final obj1 = realm1.write(() { + return realm1.add(ObjectWithEmbedded(Uuid.v4().toString(), + differentiator: differentiator, + recursiveObject: RecursiveEmbedded1('1.1', child: RecursiveEmbedded2('2.1'), children: [RecursiveEmbedded2('2.2')]), + recursiveList: [RecursiveEmbedded1('1.2')])); + }); + + await realm1.subscriptions.waitForSynchronization(); + await realm1.syncSession.waitForUpload(); + + final realm2 = await getSyncRealm(config); + realm2.subscriptions.update((mutableSubscriptions) { + mutableSubscriptions.add(realm2.query(r'differentiator = $0', [differentiator])); + }); + + await realm2.subscriptions.waitForSynchronization(); + await realm2.syncSession.waitForDownload(); + + final obj2 = realm2.all().single; + + expect(obj2.recursiveObject!.value, '1.1'); + expect(obj2.recursiveObject!.child!.value, '2.1'); + expect(obj2.recursiveObject!.children.length, 1); + expect(obj2.recursiveObject!.children[0].value, '2.2'); + expect(obj2.recursiveList.length, 1); + expect(obj2.recursiveList[0].value, '1.2'); + expect(obj2.recursiveList[0].child, isNull); + expect(obj2.recursiveList[0].children, isEmpty); + + realm2.write(() { + obj2.recursiveObject = null; + }); + + await realm2.syncSession.waitForUpload(); + await realm1.syncSession.waitForDownload(); + + expect(obj1.recursiveObject, isNull); + + expect(realm1.allEmbedded().length, 1); + expect(realm1.allEmbedded().length, 0); + }); + + for (final isDynamic in [true, false]) { + Realm _getDynamicRealm(Realm original) { + if (isDynamic) { + original.close(); + return getRealm(Configuration.local([])); + } + + return original; + } + + test('Dynamic embedded object can read properties when isDynamic=$isDynamic', () { + final realm = getLocalRealm(); + + realm.write(() { + realm.add(ObjectWithEmbedded('123', + recursiveObject: + RecursiveEmbedded1('1.1', child: RecursiveEmbedded2('2.1', child: RecursiveEmbedded3('3.1')), children: [RecursiveEmbedded2('2.2')]), + recursiveList: [RecursiveEmbedded1('1.2')])); + }); + + final dynamicRealm = _getDynamicRealm(realm); + final parent = dynamicRealm.dynamic.find('ObjectWithEmbedded', '123')!; + + // String API with casting + final child11 = parent.dynamic.get('recursiveObject')!; + expect(child11.dynamic.get('value'), '1.1'); + expect(child11.instanceSchema.name, 'RecursiveEmbedded1'); + expect(child11.instanceSchema.baseType, ObjectType.embedded); + + final list1 = parent.dynamic.getList('recursiveList'); + expect(list1.length, 1); + expect(list1[0].dynamic.get('value'), '1.2'); + expect(list1[0].instanceSchema.name, 'RecursiveEmbedded1'); + expect(list1[0].instanceSchema.baseType, ObjectType.embedded); + + final child21 = child11.dynamic.get('child')!; + expect(child21.dynamic.get('value'), '2.1'); + expect(child21.instanceSchema.name, 'RecursiveEmbedded2'); + expect(child21.instanceSchema.baseType, ObjectType.embedded); + + final list2 = child11.dynamic.getList('children'); + expect(list2.length, 1); + expect(list2[0].dynamic.get('value'), '2.2'); + expect(list2[0].instanceSchema.name, 'RecursiveEmbedded2'); + expect(list2[0].instanceSchema.baseType, ObjectType.embedded); + + final child31 = child21.dynamic.get('child')!; + expect(child31.dynamic.get('value'), '3.1'); + expect(child31.instanceSchema.name, 'RecursiveEmbedded3'); + expect(child31.instanceSchema.baseType, ObjectType.embedded); + + // String API without casting + final genericChild11 = parent.dynamic.get('recursiveObject'); + expect(genericChild11 is EmbeddedObject, true); + final castChild11 = genericChild11 as EmbeddedObject; + expect(castChild11.dynamic.get('value'), '1.1'); + + final genericList1 = parent.dynamic.getList('recursiveList'); + expect(genericList1 is List, true); + + final castList1 = genericList1 as List; + expect(castList1.length, 1); + + // Dynamic API + dynamic dynamicParent = parent; + dynamic dynamicChild11 = dynamicParent.recursiveObject; + expect(dynamicChild11.value, '1.1'); + + dynamic dynamicList1 = dynamicParent.recursiveList; + expect(dynamicList1.length, 1); + expect(dynamicList1[0].value, '1.2'); + + dynamic dynamicChild21 = dynamicChild11.child; + expect(dynamicChild21.value, '2.1'); + + dynamic dynamicList2 = dynamicChild11.children; + expect(dynamicList2.length, 1); + expect(dynamicList2[0].value, '2.2'); + + dynamic dynamicChild31 = dynamicChild21.child; + expect(dynamicChild31.value, '3.1'); + }); + } +} + +extension on RealmObjectBase { + SchemaObject get instanceSchema => (accessor as RealmCoreAccessor).metadata.schema; +} diff --git a/test/migration_test.g.dart b/test/migration_test.g.dart index 7c13f76ba..5bc2b65ac 100644 --- a/test/migration_test.g.dart +++ b/test/migration_test.g.dart @@ -6,109 +6,115 @@ part of 'migration_test.dart'; // RealmObjectGenerator // ************************************************************************** -class PersonIntName extends _PersonIntName with RealmEntity, RealmObject { +class PersonIntName extends _PersonIntName + with RealmEntity, RealmObjectBase, RealmObject { PersonIntName( int name, ) { - RealmObject.set(this, 'name', name); + RealmObjectBase.set(this, 'name', name); } PersonIntName._(); @override - int get name => RealmObject.get(this, 'name') as int; + int get name => RealmObjectBase.get(this, 'name') as int; @override - set name(int value) => RealmObject.set(this, 'name', value); + set name(int value) => RealmObjectBase.set(this, 'name', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - PersonIntName freeze() => RealmObject.freezeObject(this); + PersonIntName freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(PersonIntName._); - return const SchemaObject(PersonIntName, 'Person', [ + RealmObjectBase.registerFactory(PersonIntName._); + return const SchemaObject(ObjectType.topLevel, PersonIntName, 'Person', [ SchemaProperty('name', RealmPropertyType.int), ]); } } -class StudentV1 extends _StudentV1 with RealmEntity, RealmObject { +class StudentV1 extends _StudentV1 + with RealmEntity, RealmObjectBase, RealmObject { StudentV1( String name, { int? yearOfBirth, }) { - RealmObject.set(this, 'name', name); - RealmObject.set(this, 'yearOfBirth', yearOfBirth); + RealmObjectBase.set(this, 'name', name); + RealmObjectBase.set(this, 'yearOfBirth', yearOfBirth); } StudentV1._(); @override - String get name => RealmObject.get(this, 'name') as String; + String get name => RealmObjectBase.get(this, 'name') as String; @override - set name(String value) => RealmObject.set(this, 'name', value); + set name(String value) => RealmObjectBase.set(this, 'name', value); @override - int? get yearOfBirth => RealmObject.get(this, 'yearOfBirth') as int?; + int? get yearOfBirth => RealmObjectBase.get(this, 'yearOfBirth') as int?; @override - set yearOfBirth(int? value) => RealmObject.set(this, 'yearOfBirth', value); + set yearOfBirth(int? value) => + RealmObjectBase.set(this, 'yearOfBirth', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - StudentV1 freeze() => RealmObject.freezeObject(this); + StudentV1 freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(StudentV1._); - return const SchemaObject(StudentV1, 'Student', [ + RealmObjectBase.registerFactory(StudentV1._); + return const SchemaObject(ObjectType.topLevel, StudentV1, 'Student', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('yearOfBirth', RealmPropertyType.int, optional: true), ]); } } -class MyObjectWithTypo extends _MyObjectWithTypo with RealmEntity, RealmObject { +class MyObjectWithTypo extends _MyObjectWithTypo + with RealmEntity, RealmObjectBase, RealmObject { MyObjectWithTypo( String nmae, int vlaue, ) { - RealmObject.set(this, 'nmae', nmae); - RealmObject.set(this, 'vlaue', vlaue); + RealmObjectBase.set(this, 'nmae', nmae); + RealmObjectBase.set(this, 'vlaue', vlaue); } MyObjectWithTypo._(); @override - String get nmae => RealmObject.get(this, 'nmae') as String; + String get nmae => RealmObjectBase.get(this, 'nmae') as String; @override - set nmae(String value) => RealmObject.set(this, 'nmae', value); + set nmae(String value) => RealmObjectBase.set(this, 'nmae', value); @override - int get vlaue => RealmObject.get(this, 'vlaue') as int; + int get vlaue => RealmObjectBase.get(this, 'vlaue') as int; @override - set vlaue(int value) => RealmObject.set(this, 'vlaue', value); + set vlaue(int value) => RealmObjectBase.set(this, 'vlaue', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - MyObjectWithTypo freeze() => RealmObject.freezeObject(this); + MyObjectWithTypo freeze() => + RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(MyObjectWithTypo._); - return const SchemaObject(MyObjectWithTypo, 'MyObject', [ + RealmObjectBase.registerFactory(MyObjectWithTypo._); + return const SchemaObject( + ObjectType.topLevel, MyObjectWithTypo, 'MyObject', [ SchemaProperty('nmae', RealmPropertyType.string), SchemaProperty('vlaue', RealmPropertyType.int), ]); @@ -116,40 +122,41 @@ class MyObjectWithTypo extends _MyObjectWithTypo with RealmEntity, RealmObject { } class MyObjectWithoutTypo extends _MyObjectWithoutTypo - with RealmEntity, RealmObject { + with RealmEntity, RealmObjectBase, RealmObject { MyObjectWithoutTypo( String name, int value, ) { - RealmObject.set(this, 'name', name); - RealmObject.set(this, 'value', value); + RealmObjectBase.set(this, 'name', name); + RealmObjectBase.set(this, 'value', value); } MyObjectWithoutTypo._(); @override - String get name => RealmObject.get(this, 'name') as String; + String get name => RealmObjectBase.get(this, 'name') as String; @override - set name(String value) => RealmObject.set(this, 'name', value); + set name(String value) => RealmObjectBase.set(this, 'name', value); @override - int get value => RealmObject.get(this, 'value') as int; + int get value => RealmObjectBase.get(this, 'value') as int; @override - set value(int value) => RealmObject.set(this, 'value', value); + set value(int value) => RealmObjectBase.set(this, 'value', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override MyObjectWithoutTypo freeze() => - RealmObject.freezeObject(this); + RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(MyObjectWithoutTypo._); - return const SchemaObject(MyObjectWithoutTypo, 'MyObject', [ + RealmObjectBase.registerFactory(MyObjectWithoutTypo._); + return const SchemaObject( + ObjectType.topLevel, MyObjectWithoutTypo, 'MyObject', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('value', RealmPropertyType.int), ]); @@ -157,33 +164,34 @@ class MyObjectWithoutTypo extends _MyObjectWithoutTypo } class MyObjectWithoutValue extends _MyObjectWithoutValue - with RealmEntity, RealmObject { + with RealmEntity, RealmObjectBase, RealmObject { MyObjectWithoutValue( String name, ) { - RealmObject.set(this, 'name', name); + RealmObjectBase.set(this, 'name', name); } MyObjectWithoutValue._(); @override - String get name => RealmObject.get(this, 'name') as String; + String get name => RealmObjectBase.get(this, 'name') as String; @override - set name(String value) => RealmObject.set(this, 'name', value); + set name(String value) => RealmObjectBase.set(this, 'name', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override MyObjectWithoutValue freeze() => - RealmObject.freezeObject(this); + RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(MyObjectWithoutValue._); - return const SchemaObject(MyObjectWithoutValue, 'MyObject', [ + RealmObjectBase.registerFactory(MyObjectWithoutValue._); + return const SchemaObject( + ObjectType.topLevel, MyObjectWithoutValue, 'MyObject', [ SchemaProperty('name', RealmPropertyType.string), ]); } diff --git a/test/realm_object_test.dart b/test/realm_object_test.dart index 3f92f17b7..4f5cd4517 100644 --- a/test/realm_object_test.dart +++ b/test/realm_object_test.dart @@ -89,20 +89,6 @@ class _BoolValue { late bool value; } -extension on DateTime { - String toNormalizedDateString() { - final utc = toUtc(); - // This is kind of silly, but Core serializes negative dates as -003-01-01 12:34:56 - final utcYear = utc.year < 0 ? '-${utc.year.abs().toString().padLeft(3, '0')}' : utc.year.toString().padLeft(4, '0'); - - // For some reason Core always rounds up to the next second for negative dates, so we need to do the same - final seconds = utc.microsecondsSinceEpoch < 0 && utc.microsecondsSinceEpoch % 1000000 != 0 ? utc.second + 1 : utc.second; - return '$utcYear-${_format(utc.month)}-${_format(utc.day)} ${_format(utc.hour)}:${_format(utc.minute)}:${_format(seconds)}'; - } - - static String _format(int value) => value.toString().padLeft(2, '0'); -} - Future main([List? args]) async { await setupTests(args); @@ -324,7 +310,7 @@ Future main([List? args]) async { final foundObj = realm.find(key); expect(foundObj, obj); - final propValue = RealmObject.get(obj, pkProp.name); + final propValue = RealmObjectBase.get(obj, pkProp.name); expect(propValue, key); realm.close(); diff --git a/test/realm_object_test.g.dart b/test/realm_object_test.g.dart index 6a9c6fe8f..8451a143e 100644 --- a/test/realm_object_test.g.dart +++ b/test/realm_object_test.g.dart @@ -7,264 +7,273 @@ part of 'realm_object_test.dart'; // ************************************************************************** class ObjectIdPrimaryKey extends _ObjectIdPrimaryKey - with RealmEntity, RealmObject { + with RealmEntity, RealmObjectBase, RealmObject { ObjectIdPrimaryKey( ObjectId id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } ObjectIdPrimaryKey._(); @override - ObjectId get id => RealmObject.get(this, 'id') as ObjectId; + ObjectId get id => RealmObjectBase.get(this, 'id') as ObjectId; @override - set id(ObjectId value) => RealmObject.set(this, 'id', value); + set id(ObjectId value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override ObjectIdPrimaryKey freeze() => - RealmObject.freezeObject(this); + RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(ObjectIdPrimaryKey._); - return const SchemaObject(ObjectIdPrimaryKey, 'ObjectIdPrimaryKey', [ + RealmObjectBase.registerFactory(ObjectIdPrimaryKey._); + return const SchemaObject( + ObjectType.topLevel, ObjectIdPrimaryKey, 'ObjectIdPrimaryKey', [ SchemaProperty('id', RealmPropertyType.objectid, primaryKey: true), ]); } } class NullableObjectIdPrimaryKey extends _NullableObjectIdPrimaryKey - with RealmEntity, RealmObject { + with RealmEntity, RealmObjectBase, RealmObject { NullableObjectIdPrimaryKey( ObjectId? id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } NullableObjectIdPrimaryKey._(); @override - ObjectId? get id => RealmObject.get(this, 'id') as ObjectId?; + ObjectId? get id => RealmObjectBase.get(this, 'id') as ObjectId?; @override - set id(ObjectId? value) => RealmObject.set(this, 'id', value); + set id(ObjectId? value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override NullableObjectIdPrimaryKey freeze() => - RealmObject.freezeObject(this); + RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(NullableObjectIdPrimaryKey._); - return const SchemaObject( - NullableObjectIdPrimaryKey, 'NullableObjectIdPrimaryKey', [ + RealmObjectBase.registerFactory(NullableObjectIdPrimaryKey._); + return const SchemaObject(ObjectType.topLevel, NullableObjectIdPrimaryKey, + 'NullableObjectIdPrimaryKey', [ SchemaProperty('id', RealmPropertyType.objectid, optional: true, primaryKey: true), ]); } } -class IntPrimaryKey extends _IntPrimaryKey with RealmEntity, RealmObject { +class IntPrimaryKey extends _IntPrimaryKey + with RealmEntity, RealmObjectBase, RealmObject { IntPrimaryKey( int id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } IntPrimaryKey._(); @override - int get id => RealmObject.get(this, 'id') as int; + int get id => RealmObjectBase.get(this, 'id') as int; @override - set id(int value) => RealmObject.set(this, 'id', value); + set id(int value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - IntPrimaryKey freeze() => RealmObject.freezeObject(this); + IntPrimaryKey freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(IntPrimaryKey._); - return const SchemaObject(IntPrimaryKey, 'IntPrimaryKey', [ + RealmObjectBase.registerFactory(IntPrimaryKey._); + return const SchemaObject( + ObjectType.topLevel, IntPrimaryKey, 'IntPrimaryKey', [ SchemaProperty('id', RealmPropertyType.int, primaryKey: true), ]); } } class NullableIntPrimaryKey extends _NullableIntPrimaryKey - with RealmEntity, RealmObject { + with RealmEntity, RealmObjectBase, RealmObject { NullableIntPrimaryKey( int? id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } NullableIntPrimaryKey._(); @override - int? get id => RealmObject.get(this, 'id') as int?; + int? get id => RealmObjectBase.get(this, 'id') as int?; @override - set id(int? value) => RealmObject.set(this, 'id', value); + set id(int? value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override NullableIntPrimaryKey freeze() => - RealmObject.freezeObject(this); + RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(NullableIntPrimaryKey._); - return const SchemaObject(NullableIntPrimaryKey, 'NullableIntPrimaryKey', [ + RealmObjectBase.registerFactory(NullableIntPrimaryKey._); + return const SchemaObject( + ObjectType.topLevel, NullableIntPrimaryKey, 'NullableIntPrimaryKey', [ SchemaProperty('id', RealmPropertyType.int, optional: true, primaryKey: true), ]); } } -class StringPrimaryKey extends _StringPrimaryKey with RealmEntity, RealmObject { +class StringPrimaryKey extends _StringPrimaryKey + with RealmEntity, RealmObjectBase, RealmObject { StringPrimaryKey( String id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } StringPrimaryKey._(); @override - String get id => RealmObject.get(this, 'id') as String; + String get id => RealmObjectBase.get(this, 'id') as String; @override - set id(String value) => RealmObject.set(this, 'id', value); + set id(String value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - StringPrimaryKey freeze() => RealmObject.freezeObject(this); + StringPrimaryKey freeze() => + RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(StringPrimaryKey._); - return const SchemaObject(StringPrimaryKey, 'StringPrimaryKey', [ + RealmObjectBase.registerFactory(StringPrimaryKey._); + return const SchemaObject( + ObjectType.topLevel, StringPrimaryKey, 'StringPrimaryKey', [ SchemaProperty('id', RealmPropertyType.string, primaryKey: true), ]); } } class NullableStringPrimaryKey extends _NullableStringPrimaryKey - with RealmEntity, RealmObject { + with RealmEntity, RealmObjectBase, RealmObject { NullableStringPrimaryKey( String? id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } NullableStringPrimaryKey._(); @override - String? get id => RealmObject.get(this, 'id') as String?; + String? get id => RealmObjectBase.get(this, 'id') as String?; @override - set id(String? value) => RealmObject.set(this, 'id', value); + set id(String? value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override NullableStringPrimaryKey freeze() => - RealmObject.freezeObject(this); + RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(NullableStringPrimaryKey._); - return const SchemaObject( - NullableStringPrimaryKey, 'NullableStringPrimaryKey', [ + RealmObjectBase.registerFactory(NullableStringPrimaryKey._); + return const SchemaObject(ObjectType.topLevel, NullableStringPrimaryKey, + 'NullableStringPrimaryKey', [ SchemaProperty('id', RealmPropertyType.string, optional: true, primaryKey: true), ]); } } -class UuidPrimaryKey extends _UuidPrimaryKey with RealmEntity, RealmObject { +class UuidPrimaryKey extends _UuidPrimaryKey + with RealmEntity, RealmObjectBase, RealmObject { UuidPrimaryKey( Uuid id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } UuidPrimaryKey._(); @override - Uuid get id => RealmObject.get(this, 'id') as Uuid; + Uuid get id => RealmObjectBase.get(this, 'id') as Uuid; @override - set id(Uuid value) => RealmObject.set(this, 'id', value); + set id(Uuid value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - UuidPrimaryKey freeze() => RealmObject.freezeObject(this); + UuidPrimaryKey freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(UuidPrimaryKey._); - return const SchemaObject(UuidPrimaryKey, 'UuidPrimaryKey', [ + RealmObjectBase.registerFactory(UuidPrimaryKey._); + return const SchemaObject( + ObjectType.topLevel, UuidPrimaryKey, 'UuidPrimaryKey', [ SchemaProperty('id', RealmPropertyType.uuid, primaryKey: true), ]); } } class NullableUuidPrimaryKey extends _NullableUuidPrimaryKey - with RealmEntity, RealmObject { + with RealmEntity, RealmObjectBase, RealmObject { NullableUuidPrimaryKey( Uuid? id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } NullableUuidPrimaryKey._(); @override - Uuid? get id => RealmObject.get(this, 'id') as Uuid?; + Uuid? get id => RealmObjectBase.get(this, 'id') as Uuid?; @override - set id(Uuid? value) => RealmObject.set(this, 'id', value); + set id(Uuid? value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override NullableUuidPrimaryKey freeze() => - RealmObject.freezeObject(this); + RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(NullableUuidPrimaryKey._); + RealmObjectBase.registerFactory(NullableUuidPrimaryKey._); return const SchemaObject( - NullableUuidPrimaryKey, 'NullableUuidPrimaryKey', [ + ObjectType.topLevel, NullableUuidPrimaryKey, 'NullableUuidPrimaryKey', [ SchemaProperty('id', RealmPropertyType.uuid, optional: true, primaryKey: true), ]); @@ -272,36 +281,37 @@ class NullableUuidPrimaryKey extends _NullableUuidPrimaryKey } class RemappedFromAnotherFile extends _RemappedFromAnotherFile - with RealmEntity, RealmObject { + with RealmEntity, RealmObjectBase, RealmObject { RemappedFromAnotherFile({ RemappedClass? linkToAnotherClass, }) { - RealmObject.set(this, 'property with spaces', linkToAnotherClass); + RealmObjectBase.set(this, 'property with spaces', linkToAnotherClass); } RemappedFromAnotherFile._(); @override RemappedClass? get linkToAnotherClass => - RealmObject.get(this, 'property with spaces') + RealmObjectBase.get(this, 'property with spaces') as RemappedClass?; @override set linkToAnotherClass(covariant RemappedClass? value) => - RealmObject.set(this, 'property with spaces', value); + RealmObjectBase.set(this, 'property with spaces', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override RemappedFromAnotherFile freeze() => - RealmObject.freezeObject(this); + RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(RemappedFromAnotherFile._); - return const SchemaObject(RemappedFromAnotherFile, 'class with spaces', [ + RealmObjectBase.registerFactory(RemappedFromAnotherFile._); + return const SchemaObject( + ObjectType.topLevel, RemappedFromAnotherFile, 'class with spaces', [ SchemaProperty('property with spaces', RealmPropertyType.object, mapTo: 'property with spaces', optional: true, @@ -310,39 +320,40 @@ class RemappedFromAnotherFile extends _RemappedFromAnotherFile } } -class BoolValue extends _BoolValue with RealmEntity, RealmObject { +class BoolValue extends _BoolValue + with RealmEntity, RealmObjectBase, RealmObject { BoolValue( int key, bool value, ) { - RealmObject.set(this, 'key', key); - RealmObject.set(this, 'value', value); + RealmObjectBase.set(this, 'key', key); + RealmObjectBase.set(this, 'value', value); } BoolValue._(); @override - int get key => RealmObject.get(this, 'key') as int; + int get key => RealmObjectBase.get(this, 'key') as int; @override - set key(int value) => RealmObject.set(this, 'key', value); + set key(int value) => RealmObjectBase.set(this, 'key', value); @override - bool get value => RealmObject.get(this, 'value') as bool; + bool get value => RealmObjectBase.get(this, 'value') as bool; @override - set value(bool value) => RealmObject.set(this, 'value', value); + set value(bool value) => RealmObjectBase.set(this, 'value', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - BoolValue freeze() => RealmObject.freezeObject(this); + BoolValue freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(BoolValue._); - return const SchemaObject(BoolValue, 'BoolValue', [ + RealmObjectBase.registerFactory(BoolValue._); + return const SchemaObject(ObjectType.topLevel, BoolValue, 'BoolValue', [ SchemaProperty('key', RealmPropertyType.int, primaryKey: true), SchemaProperty('value', RealmPropertyType.bool), ]); diff --git a/test/test.dart b/test/test.dart index 1961eb272..7e3e72908 100644 --- a/test/test.dart +++ b/test/test.dart @@ -230,6 +230,75 @@ class _Game { int get rounds => winnerByRound.length; } +@RealmModel(ObjectType.embedded) +class _AllTypesEmbedded { + late String stringProp; + late bool boolProp; + late DateTime dateProp; + late double doubleProp; + late ObjectId objectIdProp; + late Uuid uuidProp; + late int intProp; + + late String? nullableStringProp; + late bool? nullableBoolProp; + late DateTime? nullableDateProp; + late double? nullableDoubleProp; + late ObjectId? nullableObjectIdProp; + late Uuid? nullableUuidProp; + late int? nullableIntProp; + + late List strings; + late List bools; + late List dates; + late List doubles; + late List objectIds; + late List uuids; + late List ints; +} + +@RealmModel() +class _ObjectWithEmbedded { + @PrimaryKey() + @MapTo('_id') + late String id; + + late Uuid? differentiator; + + late _AllTypesEmbedded? singleObject; + + late List<_AllTypesEmbedded> list; + + late _RecursiveEmbedded1? recursiveObject; + + late List<_RecursiveEmbedded1> recursiveList; +} + +@RealmModel(ObjectType.embedded) +class _RecursiveEmbedded1 { + late String value; + + late _RecursiveEmbedded2? child; + late List<_RecursiveEmbedded2> children; + + late _ObjectWithEmbedded? topLevel; +} + +@RealmModel(ObjectType.embedded) +class _RecursiveEmbedded2 { + late String value; + + late _RecursiveEmbedded3? child; + late List<_RecursiveEmbedded3> children; + + late _ObjectWithEmbedded? topLevel; +} + +@RealmModel(ObjectType.embedded) +class _RecursiveEmbedded3 { + late String value; +} + String? testName; Map arguments = {}; final baasApps = {}; @@ -515,6 +584,10 @@ Future getIntegrationUser(App app) async { return await loginWithRetry(app, Credentials.emailPassword(email, password)); } +Future getAnonymousUser(App app) { + return app.logIn(Credentials.anonymous(reuseCredentials: false)); +} + Future createServerApiKey(App app, String name, {bool enabled = true}) async { final baasApp = baasApps.values.firstWhere((ba) => ba.clientAppId == app.id); final client = _baasClient ?? (throw StateError("No BAAS client")); @@ -568,10 +641,24 @@ Future waitForCondition( ]); } -extension RealmObjectTest on RealmObject { +extension RealmObjectTest on RealmObjectBase { String toJson() => realmCore.objectToString(this); } +extension DateTimeTest on DateTime { + String toNormalizedDateString() { + final utc = toUtc(); + // This is kind of silly, but Core serializes negative dates as -003-01-01 12:34:56 + final utcYear = utc.year < 0 ? '-${utc.year.abs().toString().padLeft(3, '0')}' : utc.year.toString().padLeft(4, '0'); + + // For some reason Core always rounds up to the next second for negative dates, so we need to do the same + final seconds = utc.microsecondsSinceEpoch < 0 && utc.microsecondsSinceEpoch % 1000000 != 0 ? utc.second + 1 : utc.second; + return '$utcYear-${_format(utc.month)}-${_format(utc.day)} ${_format(utc.hour)}:${_format(utc.minute)}:${_format(seconds)}'; + } + + static String _format(int value) => value.toString().padLeft(2, '0'); +} + void clearCachedApps() => realmCore.clearCachedApps(); Future _printPlatformInfo() async { diff --git a/test/test.g.dart b/test/test.g.dart index 7b9e3d540..6bbea5855 100644 --- a/test/test.g.dart +++ b/test/test.g.dart @@ -6,108 +6,109 @@ part of 'test.dart'; // RealmObjectGenerator // ************************************************************************** -class Car extends _Car with RealmEntity, RealmObject { +class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { Car( String make, ) { - RealmObject.set(this, 'make', make); + RealmObjectBase.set(this, 'make', make); } Car._(); @override - String get make => RealmObject.get(this, 'make') as String; + String get make => RealmObjectBase.get(this, 'make') as String; @override - set make(String value) => RealmObject.set(this, 'make', value); + set make(String value) => RealmObjectBase.set(this, 'make', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Car freeze() => RealmObject.freezeObject(this); + Car freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Car._); - return const SchemaObject(Car, 'Car', [ + RealmObjectBase.registerFactory(Car._); + return const SchemaObject(ObjectType.topLevel, Car, 'Car', [ SchemaProperty('make', RealmPropertyType.string, primaryKey: true), ]); } } -class Person extends _Person with RealmEntity, RealmObject { +class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { Person( String name, ) { - RealmObject.set(this, 'name', name); + RealmObjectBase.set(this, 'name', name); } Person._(); @override - String get name => RealmObject.get(this, 'name') as String; + String get name => RealmObjectBase.get(this, 'name') as String; @override - set name(String value) => RealmObject.set(this, 'name', value); + set name(String value) => RealmObjectBase.set(this, 'name', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Person freeze() => RealmObject.freezeObject(this); + Person freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Person._); - return const SchemaObject(Person, 'Person', [ + RealmObjectBase.registerFactory(Person._); + return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string), ]); } } -class Dog extends _Dog with RealmEntity, RealmObject { +class Dog extends _Dog with RealmEntity, RealmObjectBase, RealmObject { Dog( String name, { int? age, Person? owner, }) { - RealmObject.set(this, 'name', name); - RealmObject.set(this, 'age', age); - RealmObject.set(this, 'owner', owner); + RealmObjectBase.set(this, 'name', name); + RealmObjectBase.set(this, 'age', age); + RealmObjectBase.set(this, 'owner', owner); } Dog._(); @override - String get name => RealmObject.get(this, 'name') as String; + String get name => RealmObjectBase.get(this, 'name') as String; @override - set name(String value) => RealmObject.set(this, 'name', value); + set name(String value) => RealmObjectBase.set(this, 'name', value); @override - int? get age => RealmObject.get(this, 'age') as int?; + int? get age => RealmObjectBase.get(this, 'age') as int?; @override - set age(int? value) => RealmObject.set(this, 'age', value); + set age(int? value) => RealmObjectBase.set(this, 'age', value); @override - Person? get owner => RealmObject.get(this, 'owner') as Person?; + Person? get owner => RealmObjectBase.get(this, 'owner') as Person?; @override - set owner(covariant Person? value) => RealmObject.set(this, 'owner', value); + set owner(covariant Person? value) => + RealmObjectBase.set(this, 'owner', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Dog freeze() => RealmObject.freezeObject(this); + Dog freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Dog._); - return const SchemaObject(Dog, 'Dog', [ + RealmObjectBase.registerFactory(Dog._); + return const SchemaObject(ObjectType.topLevel, Dog, 'Dog', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('age', RealmPropertyType.int, optional: true), SchemaProperty('owner', RealmPropertyType.object, @@ -116,51 +117,51 @@ class Dog extends _Dog with RealmEntity, RealmObject { } } -class Team extends _Team with RealmEntity, RealmObject { +class Team extends _Team with RealmEntity, RealmObjectBase, RealmObject { Team( String name, { Iterable players = const [], Iterable scores = const [], }) { - RealmObject.set(this, 'name', name); - RealmObject.set>( + RealmObjectBase.set(this, 'name', name); + RealmObjectBase.set>( this, 'players', RealmList(players)); - RealmObject.set>(this, 'scores', RealmList(scores)); + RealmObjectBase.set>(this, 'scores', RealmList(scores)); } Team._(); @override - String get name => RealmObject.get(this, 'name') as String; + String get name => RealmObjectBase.get(this, 'name') as String; @override - set name(String value) => RealmObject.set(this, 'name', value); + set name(String value) => RealmObjectBase.set(this, 'name', value); @override RealmList get players => - RealmObject.get(this, 'players') as RealmList; + RealmObjectBase.get(this, 'players') as RealmList; @override set players(covariant RealmList value) => throw RealmUnsupportedSetError(); @override RealmList get scores => - RealmObject.get(this, 'scores') as RealmList; + RealmObjectBase.get(this, 'scores') as RealmList; @override set scores(covariant RealmList value) => throw RealmUnsupportedSetError(); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Team freeze() => RealmObject.freezeObject(this); + Team freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Team._); - return const SchemaObject(Team, 'Team', [ + RealmObjectBase.registerFactory(Team._); + return const SchemaObject(ObjectType.topLevel, Team, 'Team', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('players', RealmPropertyType.object, linkTarget: 'Person', collectionType: RealmCollectionType.list), @@ -170,53 +171,55 @@ class Team extends _Team with RealmEntity, RealmObject { } } -class Student extends _Student with RealmEntity, RealmObject { +class Student extends _Student with RealmEntity, RealmObjectBase, RealmObject { Student( int number, { String? name, int? yearOfBirth, School? school, }) { - RealmObject.set(this, 'number', number); - RealmObject.set(this, 'name', name); - RealmObject.set(this, 'yearOfBirth', yearOfBirth); - RealmObject.set(this, 'school', school); + RealmObjectBase.set(this, 'number', number); + RealmObjectBase.set(this, 'name', name); + RealmObjectBase.set(this, 'yearOfBirth', yearOfBirth); + RealmObjectBase.set(this, 'school', school); } Student._(); @override - int get number => RealmObject.get(this, 'number') as int; + int get number => RealmObjectBase.get(this, 'number') as int; @override - set number(int value) => RealmObject.set(this, 'number', value); + set number(int value) => RealmObjectBase.set(this, 'number', value); @override - String? get name => RealmObject.get(this, 'name') as String?; + String? get name => RealmObjectBase.get(this, 'name') as String?; @override - set name(String? value) => RealmObject.set(this, 'name', value); + set name(String? value) => RealmObjectBase.set(this, 'name', value); @override - int? get yearOfBirth => RealmObject.get(this, 'yearOfBirth') as int?; + int? get yearOfBirth => RealmObjectBase.get(this, 'yearOfBirth') as int?; @override - set yearOfBirth(int? value) => RealmObject.set(this, 'yearOfBirth', value); + set yearOfBirth(int? value) => + RealmObjectBase.set(this, 'yearOfBirth', value); @override - School? get school => RealmObject.get(this, 'school') as School?; + School? get school => RealmObjectBase.get(this, 'school') as School?; @override - set school(covariant School? value) => RealmObject.set(this, 'school', value); + set school(covariant School? value) => + RealmObjectBase.set(this, 'school', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Student freeze() => RealmObject.freezeObject(this); + Student freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Student._); - return const SchemaObject(Student, 'Student', [ + RealmObjectBase.registerFactory(Student._); + return const SchemaObject(ObjectType.topLevel, Student, 'Student', [ SchemaProperty('number', RealmPropertyType.int, primaryKey: true), SchemaProperty('name', RealmPropertyType.string, optional: true), SchemaProperty('yearOfBirth', RealmPropertyType.int, optional: true), @@ -226,7 +229,7 @@ class Student extends _Student with RealmEntity, RealmObject { } } -class School extends _School with RealmEntity, RealmObject { +class School extends _School with RealmEntity, RealmObjectBase, RealmObject { School( String name, { String? city, @@ -234,60 +237,60 @@ class School extends _School with RealmEntity, RealmObject { Iterable students = const [], Iterable branches = const [], }) { - RealmObject.set(this, 'name', name); - RealmObject.set(this, 'city', city); - RealmObject.set(this, 'branchOfSchool', branchOfSchool); - RealmObject.set>( + RealmObjectBase.set(this, 'name', name); + RealmObjectBase.set(this, 'city', city); + RealmObjectBase.set(this, 'branchOfSchool', branchOfSchool); + RealmObjectBase.set>( this, 'students', RealmList(students)); - RealmObject.set>( + RealmObjectBase.set>( this, 'branches', RealmList(branches)); } School._(); @override - String get name => RealmObject.get(this, 'name') as String; + String get name => RealmObjectBase.get(this, 'name') as String; @override - set name(String value) => RealmObject.set(this, 'name', value); + set name(String value) => RealmObjectBase.set(this, 'name', value); @override - String? get city => RealmObject.get(this, 'city') as String?; + String? get city => RealmObjectBase.get(this, 'city') as String?; @override - set city(String? value) => RealmObject.set(this, 'city', value); + set city(String? value) => RealmObjectBase.set(this, 'city', value); @override RealmList get students => - RealmObject.get(this, 'students') as RealmList; + RealmObjectBase.get(this, 'students') as RealmList; @override set students(covariant RealmList value) => throw RealmUnsupportedSetError(); @override School? get branchOfSchool => - RealmObject.get(this, 'branchOfSchool') as School?; + RealmObjectBase.get(this, 'branchOfSchool') as School?; @override set branchOfSchool(covariant School? value) => - RealmObject.set(this, 'branchOfSchool', value); + RealmObjectBase.set(this, 'branchOfSchool', value); @override RealmList get branches => - RealmObject.get(this, 'branches') as RealmList; + RealmObjectBase.get(this, 'branches') as RealmList; @override set branches(covariant RealmList value) => throw RealmUnsupportedSetError(); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - School freeze() => RealmObject.freezeObject(this); + School freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(School._); - return const SchemaObject(School, 'School', [ + RealmObjectBase.registerFactory(School._); + return const SchemaObject(ObjectType.topLevel, School, 'School', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('city', RealmPropertyType.string, optional: true), SchemaProperty('students', RealmPropertyType.object, @@ -300,13 +303,14 @@ class School extends _School with RealmEntity, RealmObject { } } -class RemappedClass extends $RemappedClass with RealmEntity, RealmObject { +class RemappedClass extends $RemappedClass + with RealmEntity, RealmObjectBase, RealmObject { RemappedClass( String remappedProperty, { Iterable listProperty = const [], }) { - RealmObject.set(this, 'primitive_property', remappedProperty); - RealmObject.set>( + RealmObjectBase.set(this, 'primitive_property', remappedProperty); + RealmObjectBase.set>( this, 'list-with-dashes', RealmList(listProperty)); } @@ -314,14 +318,14 @@ class RemappedClass extends $RemappedClass with RealmEntity, RealmObject { @override String get remappedProperty => - RealmObject.get(this, 'primitive_property') as String; + RealmObjectBase.get(this, 'primitive_property') as String; @override set remappedProperty(String value) => - RealmObject.set(this, 'primitive_property', value); + RealmObjectBase.set(this, 'primitive_property', value); @override RealmList get listProperty => - RealmObject.get(this, 'list-with-dashes') + RealmObjectBase.get(this, 'list-with-dashes') as RealmList; @override set listProperty(covariant RealmList value) => @@ -329,16 +333,17 @@ class RemappedClass extends $RemappedClass with RealmEntity, RealmObject { @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - RemappedClass freeze() => RealmObject.freezeObject(this); + RemappedClass freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(RemappedClass._); - return const SchemaObject(RemappedClass, 'myRemappedClass', [ + RealmObjectBase.registerFactory(RemappedClass._); + return const SchemaObject( + ObjectType.topLevel, RemappedClass, 'myRemappedClass', [ SchemaProperty('primitive_property', RealmPropertyType.string, mapTo: 'primitive_property'), SchemaProperty('list-with-dashes', RealmPropertyType.object, @@ -349,72 +354,73 @@ class RemappedClass extends $RemappedClass with RealmEntity, RealmObject { } } -class Task extends _Task with RealmEntity, RealmObject { +class Task extends _Task with RealmEntity, RealmObjectBase, RealmObject { Task( ObjectId id, ) { - RealmObject.set(this, '_id', id); + RealmObjectBase.set(this, '_id', id); } Task._(); @override - ObjectId get id => RealmObject.get(this, '_id') as ObjectId; + ObjectId get id => RealmObjectBase.get(this, '_id') as ObjectId; @override - set id(ObjectId value) => RealmObject.set(this, '_id', value); + set id(ObjectId value) => RealmObjectBase.set(this, '_id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Task freeze() => RealmObject.freezeObject(this); + Task freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Task._); - return const SchemaObject(Task, 'Task', [ + RealmObjectBase.registerFactory(Task._); + return const SchemaObject(ObjectType.topLevel, Task, 'Task', [ SchemaProperty('_id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), ]); } } -class Product extends _Product with RealmEntity, RealmObject { +class Product extends _Product with RealmEntity, RealmObjectBase, RealmObject { Product( ObjectId id, String name, ) { - RealmObject.set(this, '_id', id); - RealmObject.set(this, 'stringQueryField', name); + RealmObjectBase.set(this, '_id', id); + RealmObjectBase.set(this, 'stringQueryField', name); } Product._(); @override - ObjectId get id => RealmObject.get(this, '_id') as ObjectId; + ObjectId get id => RealmObjectBase.get(this, '_id') as ObjectId; @override - set id(ObjectId value) => RealmObject.set(this, '_id', value); + set id(ObjectId value) => RealmObjectBase.set(this, '_id', value); @override String get name => - RealmObject.get(this, 'stringQueryField') as String; + RealmObjectBase.get(this, 'stringQueryField') as String; @override - set name(String value) => RealmObject.set(this, 'stringQueryField', value); + set name(String value) => + RealmObjectBase.set(this, 'stringQueryField', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Product freeze() => RealmObject.freezeObject(this); + Product freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Product._); - return const SchemaObject(Product, 'Product', [ + RealmObjectBase.registerFactory(Product._); + return const SchemaObject(ObjectType.topLevel, Product, 'Product', [ SchemaProperty('_id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('stringQueryField', RealmPropertyType.string, @@ -423,41 +429,42 @@ class Product extends _Product with RealmEntity, RealmObject { } } -class Schedule extends _Schedule with RealmEntity, RealmObject { +class Schedule extends _Schedule + with RealmEntity, RealmObjectBase, RealmObject { Schedule( ObjectId id, { Iterable tasks = const [], }) { - RealmObject.set(this, '_id', id); - RealmObject.set>(this, 'tasks', RealmList(tasks)); + RealmObjectBase.set(this, '_id', id); + RealmObjectBase.set>(this, 'tasks', RealmList(tasks)); } Schedule._(); @override - ObjectId get id => RealmObject.get(this, '_id') as ObjectId; + ObjectId get id => RealmObjectBase.get(this, '_id') as ObjectId; @override - set id(ObjectId value) => RealmObject.set(this, '_id', value); + set id(ObjectId value) => RealmObjectBase.set(this, '_id', value); @override RealmList get tasks => - RealmObject.get(this, 'tasks') as RealmList; + RealmObjectBase.get(this, 'tasks') as RealmList; @override set tasks(covariant RealmList value) => throw RealmUnsupportedSetError(); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Schedule freeze() => RealmObject.freezeObject(this); + Schedule freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Schedule._); - return const SchemaObject(Schedule, 'Schedule', [ + RealmObjectBase.registerFactory(Schedule._); + return const SchemaObject(ObjectType.topLevel, Schedule, 'Schedule', [ SchemaProperty('_id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('tasks', RealmPropertyType.object, @@ -466,7 +473,8 @@ class Schedule extends _Schedule with RealmEntity, RealmObject { } } -class AllTypes extends _AllTypes with RealmEntity, RealmObject { +class AllTypes extends _AllTypes + with RealmEntity, RealmObjectBase, RealmObject { AllTypes( String stringProp, bool boolProp, @@ -483,125 +491,127 @@ class AllTypes extends _AllTypes with RealmEntity, RealmObject { Uuid? nullableUuidProp, int? nullableIntProp, }) { - RealmObject.set(this, 'stringProp', stringProp); - RealmObject.set(this, 'boolProp', boolProp); - RealmObject.set(this, 'dateProp', dateProp); - RealmObject.set(this, 'doubleProp', doubleProp); - RealmObject.set(this, 'objectIdProp', objectIdProp); - RealmObject.set(this, 'uuidProp', uuidProp); - RealmObject.set(this, 'intProp', intProp); - RealmObject.set(this, 'nullableStringProp', nullableStringProp); - RealmObject.set(this, 'nullableBoolProp', nullableBoolProp); - RealmObject.set(this, 'nullableDateProp', nullableDateProp); - RealmObject.set(this, 'nullableDoubleProp', nullableDoubleProp); - RealmObject.set(this, 'nullableObjectIdProp', nullableObjectIdProp); - RealmObject.set(this, 'nullableUuidProp', nullableUuidProp); - RealmObject.set(this, 'nullableIntProp', nullableIntProp); + RealmObjectBase.set(this, 'stringProp', stringProp); + RealmObjectBase.set(this, 'boolProp', boolProp); + RealmObjectBase.set(this, 'dateProp', dateProp); + RealmObjectBase.set(this, 'doubleProp', doubleProp); + RealmObjectBase.set(this, 'objectIdProp', objectIdProp); + RealmObjectBase.set(this, 'uuidProp', uuidProp); + RealmObjectBase.set(this, 'intProp', intProp); + RealmObjectBase.set(this, 'nullableStringProp', nullableStringProp); + RealmObjectBase.set(this, 'nullableBoolProp', nullableBoolProp); + RealmObjectBase.set(this, 'nullableDateProp', nullableDateProp); + RealmObjectBase.set(this, 'nullableDoubleProp', nullableDoubleProp); + RealmObjectBase.set(this, 'nullableObjectIdProp', nullableObjectIdProp); + RealmObjectBase.set(this, 'nullableUuidProp', nullableUuidProp); + RealmObjectBase.set(this, 'nullableIntProp', nullableIntProp); } AllTypes._(); @override String get stringProp => - RealmObject.get(this, 'stringProp') as String; + RealmObjectBase.get(this, 'stringProp') as String; @override - set stringProp(String value) => RealmObject.set(this, 'stringProp', value); + set stringProp(String value) => + RealmObjectBase.set(this, 'stringProp', value); @override - bool get boolProp => RealmObject.get(this, 'boolProp') as bool; + bool get boolProp => RealmObjectBase.get(this, 'boolProp') as bool; @override - set boolProp(bool value) => RealmObject.set(this, 'boolProp', value); + set boolProp(bool value) => RealmObjectBase.set(this, 'boolProp', value); @override DateTime get dateProp => - RealmObject.get(this, 'dateProp') as DateTime; + RealmObjectBase.get(this, 'dateProp') as DateTime; @override - set dateProp(DateTime value) => RealmObject.set(this, 'dateProp', value); + set dateProp(DateTime value) => RealmObjectBase.set(this, 'dateProp', value); @override double get doubleProp => - RealmObject.get(this, 'doubleProp') as double; + RealmObjectBase.get(this, 'doubleProp') as double; @override - set doubleProp(double value) => RealmObject.set(this, 'doubleProp', value); + set doubleProp(double value) => + RealmObjectBase.set(this, 'doubleProp', value); @override ObjectId get objectIdProp => - RealmObject.get(this, 'objectIdProp') as ObjectId; + RealmObjectBase.get(this, 'objectIdProp') as ObjectId; @override set objectIdProp(ObjectId value) => - RealmObject.set(this, 'objectIdProp', value); + RealmObjectBase.set(this, 'objectIdProp', value); @override - Uuid get uuidProp => RealmObject.get(this, 'uuidProp') as Uuid; + Uuid get uuidProp => RealmObjectBase.get(this, 'uuidProp') as Uuid; @override - set uuidProp(Uuid value) => RealmObject.set(this, 'uuidProp', value); + set uuidProp(Uuid value) => RealmObjectBase.set(this, 'uuidProp', value); @override - int get intProp => RealmObject.get(this, 'intProp') as int; + int get intProp => RealmObjectBase.get(this, 'intProp') as int; @override - set intProp(int value) => RealmObject.set(this, 'intProp', value); + set intProp(int value) => RealmObjectBase.set(this, 'intProp', value); @override String? get nullableStringProp => - RealmObject.get(this, 'nullableStringProp') as String?; + RealmObjectBase.get(this, 'nullableStringProp') as String?; @override set nullableStringProp(String? value) => - RealmObject.set(this, 'nullableStringProp', value); + RealmObjectBase.set(this, 'nullableStringProp', value); @override bool? get nullableBoolProp => - RealmObject.get(this, 'nullableBoolProp') as bool?; + RealmObjectBase.get(this, 'nullableBoolProp') as bool?; @override set nullableBoolProp(bool? value) => - RealmObject.set(this, 'nullableBoolProp', value); + RealmObjectBase.set(this, 'nullableBoolProp', value); @override DateTime? get nullableDateProp => - RealmObject.get(this, 'nullableDateProp') as DateTime?; + RealmObjectBase.get(this, 'nullableDateProp') as DateTime?; @override set nullableDateProp(DateTime? value) => - RealmObject.set(this, 'nullableDateProp', value); + RealmObjectBase.set(this, 'nullableDateProp', value); @override double? get nullableDoubleProp => - RealmObject.get(this, 'nullableDoubleProp') as double?; + RealmObjectBase.get(this, 'nullableDoubleProp') as double?; @override set nullableDoubleProp(double? value) => - RealmObject.set(this, 'nullableDoubleProp', value); + RealmObjectBase.set(this, 'nullableDoubleProp', value); @override ObjectId? get nullableObjectIdProp => - RealmObject.get(this, 'nullableObjectIdProp') as ObjectId?; + RealmObjectBase.get(this, 'nullableObjectIdProp') as ObjectId?; @override set nullableObjectIdProp(ObjectId? value) => - RealmObject.set(this, 'nullableObjectIdProp', value); + RealmObjectBase.set(this, 'nullableObjectIdProp', value); @override Uuid? get nullableUuidProp => - RealmObject.get(this, 'nullableUuidProp') as Uuid?; + RealmObjectBase.get(this, 'nullableUuidProp') as Uuid?; @override set nullableUuidProp(Uuid? value) => - RealmObject.set(this, 'nullableUuidProp', value); + RealmObjectBase.set(this, 'nullableUuidProp', value); @override int? get nullableIntProp => - RealmObject.get(this, 'nullableIntProp') as int?; + RealmObjectBase.get(this, 'nullableIntProp') as int?; @override set nullableIntProp(int? value) => - RealmObject.set(this, 'nullableIntProp', value); + RealmObjectBase.set(this, 'nullableIntProp', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - AllTypes freeze() => RealmObject.freezeObject(this); + AllTypes freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(AllTypes._); - return const SchemaObject(AllTypes, 'AllTypes', [ + RealmObjectBase.registerFactory(AllTypes._); + return const SchemaObject(ObjectType.topLevel, AllTypes, 'AllTypes', [ SchemaProperty('stringProp', RealmPropertyType.string), SchemaProperty('boolProp', RealmPropertyType.bool), SchemaProperty('dateProp', RealmPropertyType.timestamp), @@ -626,50 +636,52 @@ class AllTypes extends _AllTypes with RealmEntity, RealmObject { } } -class LinksClass extends _LinksClass with RealmEntity, RealmObject { +class LinksClass extends _LinksClass + with RealmEntity, RealmObjectBase, RealmObject { LinksClass( Uuid id, { LinksClass? link, Iterable list = const [], }) { - RealmObject.set(this, 'id', id); - RealmObject.set(this, 'link', link); - RealmObject.set>( + RealmObjectBase.set(this, 'id', id); + RealmObjectBase.set(this, 'link', link); + RealmObjectBase.set>( this, 'list', RealmList(list)); } LinksClass._(); @override - Uuid get id => RealmObject.get(this, 'id') as Uuid; + Uuid get id => RealmObjectBase.get(this, 'id') as Uuid; @override - set id(Uuid value) => RealmObject.set(this, 'id', value); + set id(Uuid value) => RealmObjectBase.set(this, 'id', value); @override LinksClass? get link => - RealmObject.get(this, 'link') as LinksClass?; + RealmObjectBase.get(this, 'link') as LinksClass?; @override - set link(covariant LinksClass? value) => RealmObject.set(this, 'link', value); + set link(covariant LinksClass? value) => + RealmObjectBase.set(this, 'link', value); @override RealmList get list => - RealmObject.get(this, 'list') as RealmList; + RealmObjectBase.get(this, 'list') as RealmList; @override set list(covariant RealmList value) => throw RealmUnsupportedSetError(); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - LinksClass freeze() => RealmObject.freezeObject(this); + LinksClass freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(LinksClass._); - return const SchemaObject(LinksClass, 'LinksClass', [ + RealmObjectBase.registerFactory(LinksClass._); + return const SchemaObject(ObjectType.topLevel, LinksClass, 'LinksClass', [ SchemaProperty('id', RealmPropertyType.uuid, primaryKey: true), SchemaProperty('link', RealmPropertyType.object, optional: true, linkTarget: 'LinksClass'), @@ -679,7 +691,8 @@ class LinksClass extends _LinksClass with RealmEntity, RealmObject { } } -class AllCollections extends _AllCollections with RealmEntity, RealmObject { +class AllCollections extends _AllCollections + with RealmEntity, RealmObjectBase, RealmObject { AllCollections({ Iterable strings = const [], Iterable bools = const [], @@ -696,30 +709,30 @@ class AllCollections extends _AllCollections with RealmEntity, RealmObject { Iterable nullableUuids = const [], Iterable nullableInts = const [], }) { - RealmObject.set>( + RealmObjectBase.set>( this, 'strings', RealmList(strings)); - RealmObject.set>(this, 'bools', RealmList(bools)); - RealmObject.set>( + RealmObjectBase.set>(this, 'bools', RealmList(bools)); + RealmObjectBase.set>( this, 'dates', RealmList(dates)); - RealmObject.set>( + RealmObjectBase.set>( this, 'doubles', RealmList(doubles)); - RealmObject.set>( + RealmObjectBase.set>( this, 'objectIds', RealmList(objectIds)); - RealmObject.set>(this, 'uuids', RealmList(uuids)); - RealmObject.set>(this, 'ints', RealmList(ints)); - RealmObject.set>( + RealmObjectBase.set>(this, 'uuids', RealmList(uuids)); + RealmObjectBase.set>(this, 'ints', RealmList(ints)); + RealmObjectBase.set>( this, 'nullableStrings', RealmList(nullableStrings)); - RealmObject.set>( + RealmObjectBase.set>( this, 'nullableBools', RealmList(nullableBools)); - RealmObject.set>( + RealmObjectBase.set>( this, 'nullableDates', RealmList(nullableDates)); - RealmObject.set>( + RealmObjectBase.set>( this, 'nullableDoubles', RealmList(nullableDoubles)); - RealmObject.set>( + RealmObjectBase.set>( this, 'nullableObjectIds', RealmList(nullableObjectIds)); - RealmObject.set>( + RealmObjectBase.set>( this, 'nullableUuids', RealmList(nullableUuids)); - RealmObject.set>( + RealmObjectBase.set>( this, 'nullableInts', RealmList(nullableInts)); } @@ -727,83 +740,86 @@ class AllCollections extends _AllCollections with RealmEntity, RealmObject { @override RealmList get strings => - RealmObject.get(this, 'strings') as RealmList; + RealmObjectBase.get(this, 'strings') as RealmList; @override set strings(covariant RealmList value) => throw RealmUnsupportedSetError(); @override RealmList get bools => - RealmObject.get(this, 'bools') as RealmList; + RealmObjectBase.get(this, 'bools') as RealmList; @override set bools(covariant RealmList value) => throw RealmUnsupportedSetError(); @override RealmList get dates => - RealmObject.get(this, 'dates') as RealmList; + RealmObjectBase.get(this, 'dates') as RealmList; @override set dates(covariant RealmList value) => throw RealmUnsupportedSetError(); @override RealmList get doubles => - RealmObject.get(this, 'doubles') as RealmList; + RealmObjectBase.get(this, 'doubles') as RealmList; @override set doubles(covariant RealmList value) => throw RealmUnsupportedSetError(); @override RealmList get objectIds => - RealmObject.get(this, 'objectIds') as RealmList; + RealmObjectBase.get(this, 'objectIds') as RealmList; @override set objectIds(covariant RealmList value) => throw RealmUnsupportedSetError(); @override RealmList get uuids => - RealmObject.get(this, 'uuids') as RealmList; + RealmObjectBase.get(this, 'uuids') as RealmList; @override set uuids(covariant RealmList value) => throw RealmUnsupportedSetError(); @override RealmList get ints => - RealmObject.get(this, 'ints') as RealmList; + RealmObjectBase.get(this, 'ints') as RealmList; @override set ints(covariant RealmList value) => throw RealmUnsupportedSetError(); @override RealmList get nullableStrings => - RealmObject.get(this, 'nullableStrings') as RealmList; + RealmObjectBase.get(this, 'nullableStrings') + as RealmList; @override set nullableStrings(covariant RealmList value) => throw RealmUnsupportedSetError(); @override RealmList get nullableBools => - RealmObject.get(this, 'nullableBools') as RealmList; + RealmObjectBase.get(this, 'nullableBools') as RealmList; @override set nullableBools(covariant RealmList value) => throw RealmUnsupportedSetError(); @override RealmList get nullableDates => - RealmObject.get(this, 'nullableDates') as RealmList; + RealmObjectBase.get(this, 'nullableDates') + as RealmList; @override set nullableDates(covariant RealmList value) => throw RealmUnsupportedSetError(); @override RealmList get nullableDoubles => - RealmObject.get(this, 'nullableDoubles') as RealmList; + RealmObjectBase.get(this, 'nullableDoubles') + as RealmList; @override set nullableDoubles(covariant RealmList value) => throw RealmUnsupportedSetError(); @override RealmList get nullableObjectIds => - RealmObject.get(this, 'nullableObjectIds') + RealmObjectBase.get(this, 'nullableObjectIds') as RealmList; @override set nullableObjectIds(covariant RealmList value) => @@ -811,30 +827,31 @@ class AllCollections extends _AllCollections with RealmEntity, RealmObject { @override RealmList get nullableUuids => - RealmObject.get(this, 'nullableUuids') as RealmList; + RealmObjectBase.get(this, 'nullableUuids') as RealmList; @override set nullableUuids(covariant RealmList value) => throw RealmUnsupportedSetError(); @override RealmList get nullableInts => - RealmObject.get(this, 'nullableInts') as RealmList; + RealmObjectBase.get(this, 'nullableInts') as RealmList; @override set nullableInts(covariant RealmList value) => throw RealmUnsupportedSetError(); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - AllCollections freeze() => RealmObject.freezeObject(this); + AllCollections freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(AllCollections._); - return const SchemaObject(AllCollections, 'AllCollections', [ + RealmObjectBase.registerFactory(AllCollections._); + return const SchemaObject( + ObjectType.topLevel, AllCollections, 'AllCollections', [ SchemaProperty('strings', RealmPropertyType.string, collectionType: RealmCollectionType.list), SchemaProperty('bools', RealmPropertyType.bool, @@ -867,7 +884,8 @@ class AllCollections extends _AllCollections with RealmEntity, RealmObject { } } -class NullableTypes extends _NullableTypes with RealmEntity, RealmObject { +class NullableTypes extends _NullableTypes + with RealmEntity, RealmObjectBase, RealmObject { NullableTypes( ObjectId id, ObjectId differentiator, { @@ -879,83 +897,86 @@ class NullableTypes extends _NullableTypes with RealmEntity, RealmObject { Uuid? uuidProp, int? intProp, }) { - RealmObject.set(this, '_id', id); - RealmObject.set(this, 'differentiator', differentiator); - RealmObject.set(this, 'stringProp', stringProp); - RealmObject.set(this, 'boolProp', boolProp); - RealmObject.set(this, 'dateProp', dateProp); - RealmObject.set(this, 'doubleProp', doubleProp); - RealmObject.set(this, 'objectIdProp', objectIdProp); - RealmObject.set(this, 'uuidProp', uuidProp); - RealmObject.set(this, 'intProp', intProp); + RealmObjectBase.set(this, '_id', id); + RealmObjectBase.set(this, 'differentiator', differentiator); + RealmObjectBase.set(this, 'stringProp', stringProp); + RealmObjectBase.set(this, 'boolProp', boolProp); + RealmObjectBase.set(this, 'dateProp', dateProp); + RealmObjectBase.set(this, 'doubleProp', doubleProp); + RealmObjectBase.set(this, 'objectIdProp', objectIdProp); + RealmObjectBase.set(this, 'uuidProp', uuidProp); + RealmObjectBase.set(this, 'intProp', intProp); } NullableTypes._(); @override - ObjectId get id => RealmObject.get(this, '_id') as ObjectId; + ObjectId get id => RealmObjectBase.get(this, '_id') as ObjectId; @override - set id(ObjectId value) => RealmObject.set(this, '_id', value); + set id(ObjectId value) => RealmObjectBase.set(this, '_id', value); @override ObjectId get differentiator => - RealmObject.get(this, 'differentiator') as ObjectId; + RealmObjectBase.get(this, 'differentiator') as ObjectId; @override set differentiator(ObjectId value) => - RealmObject.set(this, 'differentiator', value); + RealmObjectBase.set(this, 'differentiator', value); @override String? get stringProp => - RealmObject.get(this, 'stringProp') as String?; + RealmObjectBase.get(this, 'stringProp') as String?; @override - set stringProp(String? value) => RealmObject.set(this, 'stringProp', value); + set stringProp(String? value) => + RealmObjectBase.set(this, 'stringProp', value); @override - bool? get boolProp => RealmObject.get(this, 'boolProp') as bool?; + bool? get boolProp => RealmObjectBase.get(this, 'boolProp') as bool?; @override - set boolProp(bool? value) => RealmObject.set(this, 'boolProp', value); + set boolProp(bool? value) => RealmObjectBase.set(this, 'boolProp', value); @override DateTime? get dateProp => - RealmObject.get(this, 'dateProp') as DateTime?; + RealmObjectBase.get(this, 'dateProp') as DateTime?; @override - set dateProp(DateTime? value) => RealmObject.set(this, 'dateProp', value); + set dateProp(DateTime? value) => RealmObjectBase.set(this, 'dateProp', value); @override double? get doubleProp => - RealmObject.get(this, 'doubleProp') as double?; + RealmObjectBase.get(this, 'doubleProp') as double?; @override - set doubleProp(double? value) => RealmObject.set(this, 'doubleProp', value); + set doubleProp(double? value) => + RealmObjectBase.set(this, 'doubleProp', value); @override ObjectId? get objectIdProp => - RealmObject.get(this, 'objectIdProp') as ObjectId?; + RealmObjectBase.get(this, 'objectIdProp') as ObjectId?; @override set objectIdProp(ObjectId? value) => - RealmObject.set(this, 'objectIdProp', value); + RealmObjectBase.set(this, 'objectIdProp', value); @override - Uuid? get uuidProp => RealmObject.get(this, 'uuidProp') as Uuid?; + Uuid? get uuidProp => RealmObjectBase.get(this, 'uuidProp') as Uuid?; @override - set uuidProp(Uuid? value) => RealmObject.set(this, 'uuidProp', value); + set uuidProp(Uuid? value) => RealmObjectBase.set(this, 'uuidProp', value); @override - int? get intProp => RealmObject.get(this, 'intProp') as int?; + int? get intProp => RealmObjectBase.get(this, 'intProp') as int?; @override - set intProp(int? value) => RealmObject.set(this, 'intProp', value); + set intProp(int? value) => RealmObjectBase.set(this, 'intProp', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - NullableTypes freeze() => RealmObject.freezeObject(this); + NullableTypes freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(NullableTypes._); - return const SchemaObject(NullableTypes, 'NullableTypes', [ + RealmObjectBase.registerFactory(NullableTypes._); + return const SchemaObject( + ObjectType.topLevel, NullableTypes, 'NullableTypes', [ SchemaProperty('_id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('differentiator', RealmPropertyType.objectid), @@ -971,7 +992,7 @@ class NullableTypes extends _NullableTypes with RealmEntity, RealmObject { } } -class Event extends _Event with RealmEntity, RealmObject { +class Event extends _Event with RealmEntity, RealmObjectBase, RealmObject { Event( ObjectId id, { String? name, @@ -979,58 +1000,60 @@ class Event extends _Event with RealmEntity, RealmObject { int? durationInMinutes, String? assignedTo, }) { - RealmObject.set(this, '_id', id); - RealmObject.set(this, 'stringQueryField', name); - RealmObject.set(this, 'boolQueryField', isCompleted); - RealmObject.set(this, 'intQueryField', durationInMinutes); - RealmObject.set(this, 'assignedTo', assignedTo); + RealmObjectBase.set(this, '_id', id); + RealmObjectBase.set(this, 'stringQueryField', name); + RealmObjectBase.set(this, 'boolQueryField', isCompleted); + RealmObjectBase.set(this, 'intQueryField', durationInMinutes); + RealmObjectBase.set(this, 'assignedTo', assignedTo); } Event._(); @override - ObjectId get id => RealmObject.get(this, '_id') as ObjectId; + ObjectId get id => RealmObjectBase.get(this, '_id') as ObjectId; @override - set id(ObjectId value) => RealmObject.set(this, '_id', value); + set id(ObjectId value) => RealmObjectBase.set(this, '_id', value); @override String? get name => - RealmObject.get(this, 'stringQueryField') as String?; + RealmObjectBase.get(this, 'stringQueryField') as String?; @override - set name(String? value) => RealmObject.set(this, 'stringQueryField', value); + set name(String? value) => + RealmObjectBase.set(this, 'stringQueryField', value); @override bool? get isCompleted => - RealmObject.get(this, 'boolQueryField') as bool?; + RealmObjectBase.get(this, 'boolQueryField') as bool?; @override set isCompleted(bool? value) => - RealmObject.set(this, 'boolQueryField', value); + RealmObjectBase.set(this, 'boolQueryField', value); @override int? get durationInMinutes => - RealmObject.get(this, 'intQueryField') as int?; + RealmObjectBase.get(this, 'intQueryField') as int?; @override set durationInMinutes(int? value) => - RealmObject.set(this, 'intQueryField', value); + RealmObjectBase.set(this, 'intQueryField', value); @override String? get assignedTo => - RealmObject.get(this, 'assignedTo') as String?; + RealmObjectBase.get(this, 'assignedTo') as String?; @override - set assignedTo(String? value) => RealmObject.set(this, 'assignedTo', value); + set assignedTo(String? value) => + RealmObjectBase.set(this, 'assignedTo', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Event freeze() => RealmObject.freezeObject(this); + Event freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Event._); - return const SchemaObject(Event, 'Event', [ + RealmObjectBase.registerFactory(Event._); + return const SchemaObject(ObjectType.topLevel, Event, 'Event', [ SchemaProperty('_id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('stringQueryField', RealmPropertyType.string, @@ -1044,57 +1067,57 @@ class Event extends _Event with RealmEntity, RealmObject { } } -class Party extends _Party with RealmEntity, RealmObject { +class Party extends _Party with RealmEntity, RealmObjectBase, RealmObject { Party( int year, { Friend? host, Party? previous, Iterable guests = const [], }) { - RealmObject.set(this, 'host', host); - RealmObject.set(this, 'year', year); - RealmObject.set(this, 'previous', previous); - RealmObject.set>( + RealmObjectBase.set(this, 'host', host); + RealmObjectBase.set(this, 'year', year); + RealmObjectBase.set(this, 'previous', previous); + RealmObjectBase.set>( this, 'guests', RealmList(guests)); } Party._(); @override - Friend? get host => RealmObject.get(this, 'host') as Friend?; + Friend? get host => RealmObjectBase.get(this, 'host') as Friend?; @override - set host(covariant Friend? value) => RealmObject.set(this, 'host', value); + set host(covariant Friend? value) => RealmObjectBase.set(this, 'host', value); @override - int get year => RealmObject.get(this, 'year') as int; + int get year => RealmObjectBase.get(this, 'year') as int; @override - set year(int value) => RealmObject.set(this, 'year', value); + set year(int value) => RealmObjectBase.set(this, 'year', value); @override RealmList get guests => - RealmObject.get(this, 'guests') as RealmList; + RealmObjectBase.get(this, 'guests') as RealmList; @override set guests(covariant RealmList value) => throw RealmUnsupportedSetError(); @override - Party? get previous => RealmObject.get(this, 'previous') as Party?; + Party? get previous => RealmObjectBase.get(this, 'previous') as Party?; @override set previous(covariant Party? value) => - RealmObject.set(this, 'previous', value); + RealmObjectBase.set(this, 'previous', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Party freeze() => RealmObject.freezeObject(this); + Party freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Party._); - return const SchemaObject(Party, 'Party', [ + RealmObjectBase.registerFactory(Party._); + return const SchemaObject(ObjectType.topLevel, Party, 'Party', [ SchemaProperty('host', RealmPropertyType.object, optional: true, linkTarget: 'Friend'), SchemaProperty('year', RealmPropertyType.int), @@ -1106,7 +1129,7 @@ class Party extends _Party with RealmEntity, RealmObject { } } -class Friend extends _Friend with RealmEntity, RealmObject { +class Friend extends _Friend with RealmEntity, RealmObjectBase, RealmObject { static var _defaultsSet = false; Friend( @@ -1116,55 +1139,55 @@ class Friend extends _Friend with RealmEntity, RealmObject { Iterable friends = const [], }) { if (!_defaultsSet) { - _defaultsSet = RealmObject.setDefaults({ + _defaultsSet = RealmObjectBase.setDefaults({ 'age': 42, }); } - RealmObject.set(this, 'name', name); - RealmObject.set(this, 'age', age); - RealmObject.set(this, 'bestFriend', bestFriend); - RealmObject.set>( + RealmObjectBase.set(this, 'name', name); + RealmObjectBase.set(this, 'age', age); + RealmObjectBase.set(this, 'bestFriend', bestFriend); + RealmObjectBase.set>( this, 'friends', RealmList(friends)); } Friend._(); @override - String get name => RealmObject.get(this, 'name') as String; + String get name => RealmObjectBase.get(this, 'name') as String; @override - set name(String value) => RealmObject.set(this, 'name', value); + set name(String value) => RealmObjectBase.set(this, 'name', value); @override - int get age => RealmObject.get(this, 'age') as int; + int get age => RealmObjectBase.get(this, 'age') as int; @override - set age(int value) => RealmObject.set(this, 'age', value); + set age(int value) => RealmObjectBase.set(this, 'age', value); @override Friend? get bestFriend => - RealmObject.get(this, 'bestFriend') as Friend?; + RealmObjectBase.get(this, 'bestFriend') as Friend?; @override set bestFriend(covariant Friend? value) => - RealmObject.set(this, 'bestFriend', value); + RealmObjectBase.set(this, 'bestFriend', value); @override RealmList get friends => - RealmObject.get(this, 'friends') as RealmList; + RealmObjectBase.get(this, 'friends') as RealmList; @override set friends(covariant RealmList value) => throw RealmUnsupportedSetError(); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Friend freeze() => RealmObject.freezeObject(this); + Friend freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Friend._); - return const SchemaObject(Friend, 'Friend', [ + RealmObjectBase.registerFactory(Friend._); + return const SchemaObject(ObjectType.topLevel, Friend, 'Friend', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('age', RealmPropertyType.int), SchemaProperty('bestFriend', RealmPropertyType.object, @@ -1175,92 +1198,92 @@ class Friend extends _Friend with RealmEntity, RealmObject { } } -class When extends _When with RealmEntity, RealmObject { +class When extends _When with RealmEntity, RealmObjectBase, RealmObject { When( DateTime dateTimeUtc, String locationName, ) { - RealmObject.set(this, 'dateTimeUtc', dateTimeUtc); - RealmObject.set(this, 'locationName', locationName); + RealmObjectBase.set(this, 'dateTimeUtc', dateTimeUtc); + RealmObjectBase.set(this, 'locationName', locationName); } When._(); @override DateTime get dateTimeUtc => - RealmObject.get(this, 'dateTimeUtc') as DateTime; + RealmObjectBase.get(this, 'dateTimeUtc') as DateTime; @override set dateTimeUtc(DateTime value) => - RealmObject.set(this, 'dateTimeUtc', value); + RealmObjectBase.set(this, 'dateTimeUtc', value); @override String get locationName => - RealmObject.get(this, 'locationName') as String; + RealmObjectBase.get(this, 'locationName') as String; @override set locationName(String value) => - RealmObject.set(this, 'locationName', value); + RealmObjectBase.set(this, 'locationName', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - When freeze() => RealmObject.freezeObject(this); + When freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(When._); - return const SchemaObject(When, 'When', [ + RealmObjectBase.registerFactory(When._); + return const SchemaObject(ObjectType.topLevel, When, 'When', [ SchemaProperty('dateTimeUtc', RealmPropertyType.timestamp), SchemaProperty('locationName', RealmPropertyType.string), ]); } } -class Player extends _Player with RealmEntity, RealmObject { +class Player extends _Player with RealmEntity, RealmObjectBase, RealmObject { Player( String name, { Game? game, Iterable scoresByRound = const [], }) { - RealmObject.set(this, 'name', name); - RealmObject.set(this, 'game', game); - RealmObject.set>( + RealmObjectBase.set(this, 'name', name); + RealmObjectBase.set(this, 'game', game); + RealmObjectBase.set>( this, 'scoresByRound', RealmList(scoresByRound)); } Player._(); @override - String get name => RealmObject.get(this, 'name') as String; + String get name => RealmObjectBase.get(this, 'name') as String; @override - set name(String value) => RealmObject.set(this, 'name', value); + set name(String value) => RealmObjectBase.set(this, 'name', value); @override - Game? get game => RealmObject.get(this, 'game') as Game?; + Game? get game => RealmObjectBase.get(this, 'game') as Game?; @override - set game(covariant Game? value) => RealmObject.set(this, 'game', value); + set game(covariant Game? value) => RealmObjectBase.set(this, 'game', value); @override RealmList get scoresByRound => - RealmObject.get(this, 'scoresByRound') as RealmList; + RealmObjectBase.get(this, 'scoresByRound') as RealmList; @override set scoresByRound(covariant RealmList value) => throw RealmUnsupportedSetError(); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Player freeze() => RealmObject.freezeObject(this); + Player freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Player._); - return const SchemaObject(Player, 'Player', [ + RealmObjectBase.registerFactory(Player._); + return const SchemaObject(ObjectType.topLevel, Player, 'Player', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('game', RealmPropertyType.object, optional: true, linkTarget: 'Game'), @@ -1270,11 +1293,11 @@ class Player extends _Player with RealmEntity, RealmObject { } } -class Game extends _Game with RealmEntity, RealmObject { +class Game extends _Game with RealmEntity, RealmObjectBase, RealmObject { Game({ Iterable winnerByRound = const [], }) { - RealmObject.set>( + RealmObjectBase.set>( this, 'winnerByRound', RealmList(winnerByRound)); } @@ -1282,25 +1305,545 @@ class Game extends _Game with RealmEntity, RealmObject { @override RealmList get winnerByRound => - RealmObject.get(this, 'winnerByRound') as RealmList; + RealmObjectBase.get(this, 'winnerByRound') as RealmList; @override set winnerByRound(covariant RealmList value) => throw RealmUnsupportedSetError(); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Game freeze() => RealmObject.freezeObject(this); + Game freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Game._); - return const SchemaObject(Game, 'Game', [ + RealmObjectBase.registerFactory(Game._); + return const SchemaObject(ObjectType.topLevel, Game, 'Game', [ SchemaProperty('winnerByRound', RealmPropertyType.object, linkTarget: 'Player', collectionType: RealmCollectionType.list), ]); } } + +class AllTypesEmbedded extends _AllTypesEmbedded + with RealmEntity, RealmObjectBase, EmbeddedObject { + AllTypesEmbedded( + String stringProp, + bool boolProp, + DateTime dateProp, + double doubleProp, + ObjectId objectIdProp, + Uuid uuidProp, + int intProp, { + String? nullableStringProp, + bool? nullableBoolProp, + DateTime? nullableDateProp, + double? nullableDoubleProp, + ObjectId? nullableObjectIdProp, + Uuid? nullableUuidProp, + int? nullableIntProp, + Iterable strings = const [], + Iterable bools = const [], + Iterable dates = const [], + Iterable doubles = const [], + Iterable objectIds = const [], + Iterable uuids = const [], + Iterable ints = const [], + }) { + RealmObjectBase.set(this, 'stringProp', stringProp); + RealmObjectBase.set(this, 'boolProp', boolProp); + RealmObjectBase.set(this, 'dateProp', dateProp); + RealmObjectBase.set(this, 'doubleProp', doubleProp); + RealmObjectBase.set(this, 'objectIdProp', objectIdProp); + RealmObjectBase.set(this, 'uuidProp', uuidProp); + RealmObjectBase.set(this, 'intProp', intProp); + RealmObjectBase.set(this, 'nullableStringProp', nullableStringProp); + RealmObjectBase.set(this, 'nullableBoolProp', nullableBoolProp); + RealmObjectBase.set(this, 'nullableDateProp', nullableDateProp); + RealmObjectBase.set(this, 'nullableDoubleProp', nullableDoubleProp); + RealmObjectBase.set(this, 'nullableObjectIdProp', nullableObjectIdProp); + RealmObjectBase.set(this, 'nullableUuidProp', nullableUuidProp); + RealmObjectBase.set(this, 'nullableIntProp', nullableIntProp); + RealmObjectBase.set>( + this, 'strings', RealmList(strings)); + RealmObjectBase.set>(this, 'bools', RealmList(bools)); + RealmObjectBase.set>( + this, 'dates', RealmList(dates)); + RealmObjectBase.set>( + this, 'doubles', RealmList(doubles)); + RealmObjectBase.set>( + this, 'objectIds', RealmList(objectIds)); + RealmObjectBase.set>(this, 'uuids', RealmList(uuids)); + RealmObjectBase.set>(this, 'ints', RealmList(ints)); + } + + AllTypesEmbedded._(); + + @override + String get stringProp => + RealmObjectBase.get(this, 'stringProp') as String; + @override + set stringProp(String value) => + RealmObjectBase.set(this, 'stringProp', value); + + @override + bool get boolProp => RealmObjectBase.get(this, 'boolProp') as bool; + @override + set boolProp(bool value) => RealmObjectBase.set(this, 'boolProp', value); + + @override + DateTime get dateProp => + RealmObjectBase.get(this, 'dateProp') as DateTime; + @override + set dateProp(DateTime value) => RealmObjectBase.set(this, 'dateProp', value); + + @override + double get doubleProp => + RealmObjectBase.get(this, 'doubleProp') as double; + @override + set doubleProp(double value) => + RealmObjectBase.set(this, 'doubleProp', value); + + @override + ObjectId get objectIdProp => + RealmObjectBase.get(this, 'objectIdProp') as ObjectId; + @override + set objectIdProp(ObjectId value) => + RealmObjectBase.set(this, 'objectIdProp', value); + + @override + Uuid get uuidProp => RealmObjectBase.get(this, 'uuidProp') as Uuid; + @override + set uuidProp(Uuid value) => RealmObjectBase.set(this, 'uuidProp', value); + + @override + int get intProp => RealmObjectBase.get(this, 'intProp') as int; + @override + set intProp(int value) => RealmObjectBase.set(this, 'intProp', value); + + @override + String? get nullableStringProp => + RealmObjectBase.get(this, 'nullableStringProp') as String?; + @override + set nullableStringProp(String? value) => + RealmObjectBase.set(this, 'nullableStringProp', value); + + @override + bool? get nullableBoolProp => + RealmObjectBase.get(this, 'nullableBoolProp') as bool?; + @override + set nullableBoolProp(bool? value) => + RealmObjectBase.set(this, 'nullableBoolProp', value); + + @override + DateTime? get nullableDateProp => + RealmObjectBase.get(this, 'nullableDateProp') as DateTime?; + @override + set nullableDateProp(DateTime? value) => + RealmObjectBase.set(this, 'nullableDateProp', value); + + @override + double? get nullableDoubleProp => + RealmObjectBase.get(this, 'nullableDoubleProp') as double?; + @override + set nullableDoubleProp(double? value) => + RealmObjectBase.set(this, 'nullableDoubleProp', value); + + @override + ObjectId? get nullableObjectIdProp => + RealmObjectBase.get(this, 'nullableObjectIdProp') as ObjectId?; + @override + set nullableObjectIdProp(ObjectId? value) => + RealmObjectBase.set(this, 'nullableObjectIdProp', value); + + @override + Uuid? get nullableUuidProp => + RealmObjectBase.get(this, 'nullableUuidProp') as Uuid?; + @override + set nullableUuidProp(Uuid? value) => + RealmObjectBase.set(this, 'nullableUuidProp', value); + + @override + int? get nullableIntProp => + RealmObjectBase.get(this, 'nullableIntProp') as int?; + @override + set nullableIntProp(int? value) => + RealmObjectBase.set(this, 'nullableIntProp', value); + + @override + RealmList get strings => + RealmObjectBase.get(this, 'strings') as RealmList; + @override + set strings(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + RealmList get bools => + RealmObjectBase.get(this, 'bools') as RealmList; + @override + set bools(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + RealmList get dates => + RealmObjectBase.get(this, 'dates') as RealmList; + @override + set dates(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + RealmList get doubles => + RealmObjectBase.get(this, 'doubles') as RealmList; + @override + set doubles(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + RealmList get objectIds => + RealmObjectBase.get(this, 'objectIds') as RealmList; + @override + set objectIds(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + RealmList get uuids => + RealmObjectBase.get(this, 'uuids') as RealmList; + @override + set uuids(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + RealmList get ints => + RealmObjectBase.get(this, 'ints') as RealmList; + @override + set ints(covariant RealmList value) => throw RealmUnsupportedSetError(); + + @override + Stream> get changes => + RealmObjectBase.getChanges(this); + + @override + AllTypesEmbedded freeze() => + RealmObjectBase.freezeObject(this); + + static SchemaObject get schema => _schema ??= _initSchema(); + static SchemaObject? _schema; + static SchemaObject _initSchema() { + RealmObjectBase.registerFactory(AllTypesEmbedded._); + return const SchemaObject( + ObjectType.embedded, AllTypesEmbedded, 'AllTypesEmbedded', [ + SchemaProperty('stringProp', RealmPropertyType.string), + SchemaProperty('boolProp', RealmPropertyType.bool), + SchemaProperty('dateProp', RealmPropertyType.timestamp), + SchemaProperty('doubleProp', RealmPropertyType.double), + SchemaProperty('objectIdProp', RealmPropertyType.objectid), + SchemaProperty('uuidProp', RealmPropertyType.uuid), + SchemaProperty('intProp', RealmPropertyType.int), + SchemaProperty('nullableStringProp', RealmPropertyType.string, + optional: true), + SchemaProperty('nullableBoolProp', RealmPropertyType.bool, + optional: true), + SchemaProperty('nullableDateProp', RealmPropertyType.timestamp, + optional: true), + SchemaProperty('nullableDoubleProp', RealmPropertyType.double, + optional: true), + SchemaProperty('nullableObjectIdProp', RealmPropertyType.objectid, + optional: true), + SchemaProperty('nullableUuidProp', RealmPropertyType.uuid, + optional: true), + SchemaProperty('nullableIntProp', RealmPropertyType.int, optional: true), + SchemaProperty('strings', RealmPropertyType.string, + collectionType: RealmCollectionType.list), + SchemaProperty('bools', RealmPropertyType.bool, + collectionType: RealmCollectionType.list), + SchemaProperty('dates', RealmPropertyType.timestamp, + collectionType: RealmCollectionType.list), + SchemaProperty('doubles', RealmPropertyType.double, + collectionType: RealmCollectionType.list), + SchemaProperty('objectIds', RealmPropertyType.objectid, + collectionType: RealmCollectionType.list), + SchemaProperty('uuids', RealmPropertyType.uuid, + collectionType: RealmCollectionType.list), + SchemaProperty('ints', RealmPropertyType.int, + collectionType: RealmCollectionType.list), + ]); + } +} + +class ObjectWithEmbedded extends _ObjectWithEmbedded + with RealmEntity, RealmObjectBase, RealmObject { + ObjectWithEmbedded( + String id, { + Uuid? differentiator, + AllTypesEmbedded? singleObject, + RecursiveEmbedded1? recursiveObject, + Iterable list = const [], + Iterable recursiveList = const [], + }) { + RealmObjectBase.set(this, '_id', id); + RealmObjectBase.set(this, 'differentiator', differentiator); + RealmObjectBase.set(this, 'singleObject', singleObject); + RealmObjectBase.set(this, 'recursiveObject', recursiveObject); + RealmObjectBase.set>( + this, 'list', RealmList(list)); + RealmObjectBase.set>( + this, 'recursiveList', RealmList(recursiveList)); + } + + ObjectWithEmbedded._(); + + @override + String get id => RealmObjectBase.get(this, '_id') as String; + @override + set id(String value) => RealmObjectBase.set(this, '_id', value); + + @override + Uuid? get differentiator => + RealmObjectBase.get(this, 'differentiator') as Uuid?; + @override + set differentiator(Uuid? value) => + RealmObjectBase.set(this, 'differentiator', value); + + @override + AllTypesEmbedded? get singleObject => + RealmObjectBase.get(this, 'singleObject') + as AllTypesEmbedded?; + @override + set singleObject(covariant AllTypesEmbedded? value) => + RealmObjectBase.set(this, 'singleObject', value); + + @override + RealmList get list => + RealmObjectBase.get(this, 'list') + as RealmList; + @override + set list(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + RecursiveEmbedded1? get recursiveObject => + RealmObjectBase.get(this, 'recursiveObject') + as RecursiveEmbedded1?; + @override + set recursiveObject(covariant RecursiveEmbedded1? value) => + RealmObjectBase.set(this, 'recursiveObject', value); + + @override + RealmList get recursiveList => + RealmObjectBase.get(this, 'recursiveList') + as RealmList; + @override + set recursiveList(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + Stream> get changes => + RealmObjectBase.getChanges(this); + + @override + ObjectWithEmbedded freeze() => + RealmObjectBase.freezeObject(this); + + static SchemaObject get schema => _schema ??= _initSchema(); + static SchemaObject? _schema; + static SchemaObject _initSchema() { + RealmObjectBase.registerFactory(ObjectWithEmbedded._); + return const SchemaObject( + ObjectType.topLevel, ObjectWithEmbedded, 'ObjectWithEmbedded', [ + SchemaProperty('_id', RealmPropertyType.string, + mapTo: '_id', primaryKey: true), + SchemaProperty('differentiator', RealmPropertyType.uuid, optional: true), + SchemaProperty('singleObject', RealmPropertyType.object, + optional: true, linkTarget: 'AllTypesEmbedded'), + SchemaProperty('list', RealmPropertyType.object, + linkTarget: 'AllTypesEmbedded', + collectionType: RealmCollectionType.list), + SchemaProperty('recursiveObject', RealmPropertyType.object, + optional: true, linkTarget: 'RecursiveEmbedded1'), + SchemaProperty('recursiveList', RealmPropertyType.object, + linkTarget: 'RecursiveEmbedded1', + collectionType: RealmCollectionType.list), + ]); + } +} + +class RecursiveEmbedded1 extends _RecursiveEmbedded1 + with RealmEntity, RealmObjectBase, EmbeddedObject { + RecursiveEmbedded1( + String value, { + RecursiveEmbedded2? child, + ObjectWithEmbedded? topLevel, + Iterable children = const [], + }) { + RealmObjectBase.set(this, 'value', value); + RealmObjectBase.set(this, 'child', child); + RealmObjectBase.set(this, 'topLevel', topLevel); + RealmObjectBase.set>( + this, 'children', RealmList(children)); + } + + RecursiveEmbedded1._(); + + @override + String get value => RealmObjectBase.get(this, 'value') as String; + @override + set value(String value) => RealmObjectBase.set(this, 'value', value); + + @override + RecursiveEmbedded2? get child => + RealmObjectBase.get(this, 'child') + as RecursiveEmbedded2?; + @override + set child(covariant RecursiveEmbedded2? value) => + RealmObjectBase.set(this, 'child', value); + + @override + RealmList get children => + RealmObjectBase.get(this, 'children') + as RealmList; + @override + set children(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + ObjectWithEmbedded? get topLevel => + RealmObjectBase.get(this, 'topLevel') + as ObjectWithEmbedded?; + @override + set topLevel(covariant ObjectWithEmbedded? value) => + RealmObjectBase.set(this, 'topLevel', value); + + @override + Stream> get changes => + RealmObjectBase.getChanges(this); + + @override + RecursiveEmbedded1 freeze() => + RealmObjectBase.freezeObject(this); + + static SchemaObject get schema => _schema ??= _initSchema(); + static SchemaObject? _schema; + static SchemaObject _initSchema() { + RealmObjectBase.registerFactory(RecursiveEmbedded1._); + return const SchemaObject( + ObjectType.embedded, RecursiveEmbedded1, 'RecursiveEmbedded1', [ + SchemaProperty('value', RealmPropertyType.string), + SchemaProperty('child', RealmPropertyType.object, + optional: true, linkTarget: 'RecursiveEmbedded2'), + SchemaProperty('children', RealmPropertyType.object, + linkTarget: 'RecursiveEmbedded2', + collectionType: RealmCollectionType.list), + SchemaProperty('topLevel', RealmPropertyType.object, + optional: true, linkTarget: 'ObjectWithEmbedded'), + ]); + } +} + +class RecursiveEmbedded2 extends _RecursiveEmbedded2 + with RealmEntity, RealmObjectBase, EmbeddedObject { + RecursiveEmbedded2( + String value, { + RecursiveEmbedded3? child, + ObjectWithEmbedded? topLevel, + Iterable children = const [], + }) { + RealmObjectBase.set(this, 'value', value); + RealmObjectBase.set(this, 'child', child); + RealmObjectBase.set(this, 'topLevel', topLevel); + RealmObjectBase.set>( + this, 'children', RealmList(children)); + } + + RecursiveEmbedded2._(); + + @override + String get value => RealmObjectBase.get(this, 'value') as String; + @override + set value(String value) => RealmObjectBase.set(this, 'value', value); + + @override + RecursiveEmbedded3? get child => + RealmObjectBase.get(this, 'child') + as RecursiveEmbedded3?; + @override + set child(covariant RecursiveEmbedded3? value) => + RealmObjectBase.set(this, 'child', value); + + @override + RealmList get children => + RealmObjectBase.get(this, 'children') + as RealmList; + @override + set children(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + ObjectWithEmbedded? get topLevel => + RealmObjectBase.get(this, 'topLevel') + as ObjectWithEmbedded?; + @override + set topLevel(covariant ObjectWithEmbedded? value) => + RealmObjectBase.set(this, 'topLevel', value); + + @override + Stream> get changes => + RealmObjectBase.getChanges(this); + + @override + RecursiveEmbedded2 freeze() => + RealmObjectBase.freezeObject(this); + + static SchemaObject get schema => _schema ??= _initSchema(); + static SchemaObject? _schema; + static SchemaObject _initSchema() { + RealmObjectBase.registerFactory(RecursiveEmbedded2._); + return const SchemaObject( + ObjectType.embedded, RecursiveEmbedded2, 'RecursiveEmbedded2', [ + SchemaProperty('value', RealmPropertyType.string), + SchemaProperty('child', RealmPropertyType.object, + optional: true, linkTarget: 'RecursiveEmbedded3'), + SchemaProperty('children', RealmPropertyType.object, + linkTarget: 'RecursiveEmbedded3', + collectionType: RealmCollectionType.list), + SchemaProperty('topLevel', RealmPropertyType.object, + optional: true, linkTarget: 'ObjectWithEmbedded'), + ]); + } +} + +class RecursiveEmbedded3 extends _RecursiveEmbedded3 + with RealmEntity, RealmObjectBase, EmbeddedObject { + RecursiveEmbedded3( + String value, + ) { + RealmObjectBase.set(this, 'value', value); + } + + RecursiveEmbedded3._(); + + @override + String get value => RealmObjectBase.get(this, 'value') as String; + @override + set value(String value) => RealmObjectBase.set(this, 'value', value); + + @override + Stream> get changes => + RealmObjectBase.getChanges(this); + + @override + RecursiveEmbedded3 freeze() => + RealmObjectBase.freezeObject(this); + + static SchemaObject get schema => _schema ??= _initSchema(); + static SchemaObject? _schema; + static SchemaObject _initSchema() { + RealmObjectBase.registerFactory(RecursiveEmbedded3._); + return const SchemaObject( + ObjectType.embedded, RecursiveEmbedded3, 'RecursiveEmbedded3', [ + SchemaProperty('value', RealmPropertyType.string), + ]); + } +} From 29a8e0ee7afe5654423e22ff15bd1e865a7e1fcb Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Wed, 19 Oct 2022 22:35:57 +0200 Subject: [PATCH 02/13] Fix generator tests --- .../embedded_object_primary_key.dart | 9 + .../embedded_object_primary_key.expected | 13 + generator/test/good_test.dart | 22 +- .../test/good_test_data/all_types.expected | 137 ++++----- .../test/good_test_data/embedded_objects.dart | 38 +++ .../good_test_data/embedded_objects.expected | 275 ++++++++++++++++++ .../list_initialization.expected | 24 +- generator/test/good_test_data/mapto.expected | 30 +- .../good_test_data/optional_argument.expected | 22 +- .../test/good_test_data/pinhole.expected | 20 +- .../test/good_test_data/primary_key.expected | 143 ++++----- .../good_test_data/required_argument.expected | 18 +- ...uired_argument_with_default_value.expected | 20 +- .../user_defined_getter.expected | 18 +- 14 files changed, 583 insertions(+), 206 deletions(-) create mode 100644 generator/test/error_test_data/embedded_object_primary_key.dart create mode 100644 generator/test/error_test_data/embedded_object_primary_key.expected create mode 100644 generator/test/good_test_data/embedded_objects.dart create mode 100644 generator/test/good_test_data/embedded_objects.expected diff --git a/generator/test/error_test_data/embedded_object_primary_key.dart b/generator/test/error_test_data/embedded_object_primary_key.dart new file mode 100644 index 000000000..f0b5f6183 --- /dev/null +++ b/generator/test/error_test_data/embedded_object_primary_key.dart @@ -0,0 +1,9 @@ +import 'package:realm_common/realm_common.dart'; + +//part 'embedded_object_primary_key.g.dart'; + +@RealmModel(ObjectType.embedded) +class _Bad { + @PrimaryKey() + late int id; +} diff --git a/generator/test/error_test_data/embedded_object_primary_key.expected b/generator/test/error_test_data/embedded_object_primary_key.expected new file mode 100644 index 000000000..fa7e93a53 --- /dev/null +++ b/generator/test/error_test_data/embedded_object_primary_key.expected @@ -0,0 +1,13 @@ +Primary key not allowed on embedded objects + +in: asset:pkg/test/error_test_data/embedded_object_primary_key.dart:8:12 + ╷ +5 │ @RealmModel(ObjectType.embedded) +6 │ class _Bad { + │ ━━━━ +7 │ @PrimaryKey() +8 │ late int id; + │ ^^ Bad is marked as embedded but has primary key defined + ╵ +Remove the @PrimaryKey annotation from the field or set the object type to topLevel. + diff --git a/generator/test/good_test.dart b/generator/test/good_test.dart index d390deb71..fbbfc6618 100644 --- a/generator/test/good_test.dart +++ b/generator/test/good_test.dart @@ -36,13 +36,14 @@ class _MappedToo { // RealmObjectGenerator // ************************************************************************** -class MappedToo extends _MappedToo with RealmEntity, RealmObject { +class MappedToo extends _MappedToo + with RealmEntity, RealmObjectBase, RealmObject { MappedToo({ Original? singleLink, Iterable listLink = const [], }) { - RealmObject.set(this, 'singleLink', singleLink); - RealmObject.set>( + RealmObjectBase.set(this, 'singleLink', singleLink); + RealmObjectBase.set>( this, 'listLink', RealmList(listLink)); } @@ -50,30 +51,31 @@ class MappedToo extends _MappedToo with RealmEntity, RealmObject { @override Original? get singleLink => - RealmObject.get(this, 'singleLink') as Original?; + RealmObjectBase.get(this, 'singleLink') as Original?; @override set singleLink(covariant Original? value) => - RealmObject.set(this, 'singleLink', value); + RealmObjectBase.set(this, 'singleLink', value); @override RealmList get listLink => - RealmObject.get(this, 'listLink') as RealmList; + RealmObjectBase.get(this, 'listLink') as RealmList; @override set listLink(covariant RealmList value) => throw RealmUnsupportedSetError(); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - MappedToo freeze() => RealmObject.freezeObject(this); + MappedToo freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(MappedToo._); - return const SchemaObject(MappedToo, 'this is also mapped', [ + RealmObjectBase.registerFactory(MappedToo._); + return const SchemaObject( + ObjectType.topLevel, MappedToo, 'this is also mapped', [ SchemaProperty('singleLink', RealmPropertyType.object, optional: true, linkTarget: 'another type'), SchemaProperty('listLink', RealmPropertyType.object, diff --git a/generator/test/good_test_data/all_types.expected b/generator/test/good_test_data/all_types.expected index f9ee58042..49fc2eff1 100644 --- a/generator/test/good_test_data/all_types.expected +++ b/generator/test/good_test_data/all_types.expected @@ -2,45 +2,45 @@ // RealmObjectGenerator // ************************************************************************** -class Foo extends _Foo with RealmEntity, RealmObject { +class Foo extends _Foo with RealmEntity, RealmObjectBase, RealmObject { static var _defaultsSet = false; Foo({ int x = 0, }) { if (!_defaultsSet) { - _defaultsSet = RealmObject.setDefaults({ + _defaultsSet = RealmObjectBase.setDefaults({ 'x': 0, }); } - RealmObject.set(this, 'x', x); + RealmObjectBase.set(this, 'x', x); } Foo._(); @override - int get x => RealmObject.get(this, 'x') as int; + int get x => RealmObjectBase.get(this, 'x') as int; @override - set x(int value) => RealmObject.set(this, 'x', value); + set x(int value) => RealmObjectBase.set(this, 'x', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Foo freeze() => RealmObject.freezeObject(this); + Foo freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Foo._); - return const SchemaObject(Foo, 'MyFoo', [ + RealmObjectBase.registerFactory(Foo._); + return const SchemaObject(ObjectType.topLevel, Foo, 'MyFoo', [ SchemaProperty('x', RealmPropertyType.int), ]); } } -class Bar extends _Bar with RealmEntity, RealmObject { +class Bar extends _Bar with RealmEntity, RealmObjectBase, RealmObject { static var _defaultsSet = false; Bar( @@ -56,93 +56,95 @@ class Bar extends _Bar with RealmEntity, RealmObject { Iterable list = const [], }) { if (!_defaultsSet) { - _defaultsSet = RealmObject.setDefaults({ + _defaultsSet = RealmObjectBase.setDefaults({ 'data': Uint8List(16), 'tidspunkt': DateTime.now(), 'aDouble': 0.0, }); } - RealmObject.set(this, 'id', id); - RealmObject.set(this, 'aBool', aBool); - RealmObject.set(this, 'another', another); - RealmObject.set(this, 'data', data); - RealmObject.set(this, 'tidspunkt', timestamp); - RealmObject.set(this, 'aDouble', aDouble); - RealmObject.set(this, 'foo', foo); - RealmObject.set(this, 'anOptionalString', anOptionalString); - RealmObject.set(this, 'objectId', objectId); - RealmObject.set>(this, 'list', RealmList(list)); + RealmObjectBase.set(this, 'id', id); + RealmObjectBase.set(this, 'aBool', aBool); + RealmObjectBase.set(this, 'another', another); + RealmObjectBase.set(this, 'data', data); + RealmObjectBase.set(this, 'tidspunkt', timestamp); + RealmObjectBase.set(this, 'aDouble', aDouble); + RealmObjectBase.set(this, 'foo', foo); + RealmObjectBase.set(this, 'anOptionalString', anOptionalString); + RealmObjectBase.set(this, 'objectId', objectId); + RealmObjectBase.set>(this, 'list', RealmList(list)); } Bar._(); @override - String get id => RealmObject.get(this, 'id') as String; + String get id => RealmObjectBase.get(this, 'id') as String; @override - set id(String value) => RealmObject.set(this, 'id', value); + set id(String value) => RealmObjectBase.set(this, 'id', value); @override - bool get aBool => RealmObject.get(this, 'aBool') as bool; + bool get aBool => RealmObjectBase.get(this, 'aBool') as bool; @override - set aBool(bool value) => RealmObject.set(this, 'aBool', value); + set aBool(bool value) => RealmObjectBase.set(this, 'aBool', value); @override - bool get another => RealmObject.get(this, 'another') as bool; + bool get another => RealmObjectBase.get(this, 'another') as bool; @override - set another(bool value) => RealmObject.set(this, 'another', value); + set another(bool value) => RealmObjectBase.set(this, 'another', value); @override - Uint8List get data => RealmObject.get(this, 'data') as Uint8List; + Uint8List get data => + RealmObjectBase.get(this, 'data') as Uint8List; @override - set data(Uint8List value) => RealmObject.set(this, 'data', value); + set data(Uint8List value) => RealmObjectBase.set(this, 'data', value); @override DateTime get timestamp => - RealmObject.get(this, 'tidspunkt') as DateTime; + RealmObjectBase.get(this, 'tidspunkt') as DateTime; @override - set timestamp(DateTime value) => RealmObject.set(this, 'tidspunkt', value); + set timestamp(DateTime value) => + RealmObjectBase.set(this, 'tidspunkt', value); @override - double get aDouble => RealmObject.get(this, 'aDouble') as double; + double get aDouble => RealmObjectBase.get(this, 'aDouble') as double; @override - set aDouble(double value) => RealmObject.set(this, 'aDouble', value); + set aDouble(double value) => RealmObjectBase.set(this, 'aDouble', value); @override - Foo? get foo => RealmObject.get(this, 'foo') as Foo?; + Foo? get foo => RealmObjectBase.get(this, 'foo') as Foo?; @override - set foo(covariant Foo? value) => RealmObject.set(this, 'foo', value); + set foo(covariant Foo? value) => RealmObjectBase.set(this, 'foo', value); @override RealmList get list => - RealmObject.get(this, 'list') as RealmList; + RealmObjectBase.get(this, 'list') as RealmList; @override set list(covariant RealmList value) => throw RealmUnsupportedSetError(); @override String? get anOptionalString => - RealmObject.get(this, 'anOptionalString') as String?; + RealmObjectBase.get(this, 'anOptionalString') as String?; @override set anOptionalString(String? value) => - RealmObject.set(this, 'anOptionalString', value); + RealmObjectBase.set(this, 'anOptionalString', value); @override ObjectId get objectId => - RealmObject.get(this, 'objectId') as ObjectId; + RealmObjectBase.get(this, 'objectId') as ObjectId; @override - set objectId(ObjectId value) => RealmObject.set(this, 'objectId', value); + set objectId(ObjectId value) => RealmObjectBase.set(this, 'objectId', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Bar freeze() => RealmObject.freezeObject(this); + Bar freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Bar._); - return const SchemaObject(Bar, 'Bar', [ + RealmObjectBase.registerFactory(Bar._); + return const SchemaObject(ObjectType.topLevel, Bar, 'Bar', [ SchemaProperty('id', RealmPropertyType.string, primaryKey: true), SchemaProperty('aBool', RealmPropertyType.bool), SchemaProperty('another', RealmPropertyType.bool), @@ -161,7 +163,8 @@ class Bar extends _Bar with RealmEntity, RealmObject { } } -class PrimitiveTypes extends _PrimitiveTypes with RealmEntity, RealmObject { +class PrimitiveTypes extends _PrimitiveTypes + with RealmEntity, RealmObjectBase, RealmObject { PrimitiveTypes( String stringProp, bool boolProp, @@ -169,57 +172,60 @@ class PrimitiveTypes extends _PrimitiveTypes with RealmEntity, RealmObject { double doubleProp, ObjectId objectIdProp, ) { - RealmObject.set(this, 'stringProp', stringProp); - RealmObject.set(this, 'boolProp', boolProp); - RealmObject.set(this, 'dateProp', dateProp); - RealmObject.set(this, 'doubleProp', doubleProp); - RealmObject.set(this, 'objectIdProp', objectIdProp); + RealmObjectBase.set(this, 'stringProp', stringProp); + RealmObjectBase.set(this, 'boolProp', boolProp); + RealmObjectBase.set(this, 'dateProp', dateProp); + RealmObjectBase.set(this, 'doubleProp', doubleProp); + RealmObjectBase.set(this, 'objectIdProp', objectIdProp); } PrimitiveTypes._(); @override String get stringProp => - RealmObject.get(this, 'stringProp') as String; + RealmObjectBase.get(this, 'stringProp') as String; @override - set stringProp(String value) => RealmObject.set(this, 'stringProp', value); + set stringProp(String value) => + RealmObjectBase.set(this, 'stringProp', value); @override - bool get boolProp => RealmObject.get(this, 'boolProp') as bool; + bool get boolProp => RealmObjectBase.get(this, 'boolProp') as bool; @override - set boolProp(bool value) => RealmObject.set(this, 'boolProp', value); + set boolProp(bool value) => RealmObjectBase.set(this, 'boolProp', value); @override DateTime get dateProp => - RealmObject.get(this, 'dateProp') as DateTime; + RealmObjectBase.get(this, 'dateProp') as DateTime; @override - set dateProp(DateTime value) => RealmObject.set(this, 'dateProp', value); + set dateProp(DateTime value) => RealmObjectBase.set(this, 'dateProp', value); @override double get doubleProp => - RealmObject.get(this, 'doubleProp') as double; + RealmObjectBase.get(this, 'doubleProp') as double; @override - set doubleProp(double value) => RealmObject.set(this, 'doubleProp', value); + set doubleProp(double value) => + RealmObjectBase.set(this, 'doubleProp', value); @override ObjectId get objectIdProp => - RealmObject.get(this, 'objectIdProp') as ObjectId; + RealmObjectBase.get(this, 'objectIdProp') as ObjectId; @override set objectIdProp(ObjectId value) => - RealmObject.set(this, 'objectIdProp', value); + RealmObjectBase.set(this, 'objectIdProp', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - PrimitiveTypes freeze() => RealmObject.freezeObject(this); + PrimitiveTypes freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(PrimitiveTypes._); - return const SchemaObject(PrimitiveTypes, 'PrimitiveTypes', [ + RealmObjectBase.registerFactory(PrimitiveTypes._); + return const SchemaObject( + ObjectType.topLevel, PrimitiveTypes, 'PrimitiveTypes', [ SchemaProperty('stringProp', RealmPropertyType.string), SchemaProperty('boolProp', RealmPropertyType.bool), SchemaProperty('dateProp', RealmPropertyType.timestamp), @@ -227,4 +233,5 @@ class PrimitiveTypes extends _PrimitiveTypes with RealmEntity, RealmObject { SchemaProperty('objectIdProp', RealmPropertyType.objectid), ]); } -} \ No newline at end of file +} + diff --git a/generator/test/good_test_data/embedded_objects.dart b/generator/test/good_test_data/embedded_objects.dart new file mode 100644 index 000000000..169d5dd0d --- /dev/null +++ b/generator/test/good_test_data/embedded_objects.dart @@ -0,0 +1,38 @@ +import 'package:realm_common/realm_common.dart'; + +@RealmModel() +class _Parent { + late _Child1? child; + + late List<_Child1> children; +} + +@RealmModel(ObjectType.embedded) +class _Child1 { + late String value; + + late _Child2? child; + + late List<_Child2> children; + + late _Parent? linkToParent; +} + +@RealmModel(ObjectType.embedded) +class _Child2 { + late bool boolProp; + late int intProp; + late double doubleProp; + late String stringProp; + late DateTime dateProp; + late ObjectId objectIdProp; + late Uuid uuidProp; + + late bool? nullableBoolProp; + late int? nullableIntProp; + late double? nullableDoubleProp; + late String? nullableStringProp; + late DateTime? nullableDateProp; + late ObjectId? nullableObjectIdProp; + late Uuid? nullableUuidProp; +} diff --git a/generator/test/good_test_data/embedded_objects.expected b/generator/test/good_test_data/embedded_objects.expected new file mode 100644 index 000000000..ba0f92bad --- /dev/null +++ b/generator/test/good_test_data/embedded_objects.expected @@ -0,0 +1,275 @@ +// ************************************************************************** +// RealmObjectGenerator +// ************************************************************************** + +class Parent extends _Parent with RealmEntity, RealmObjectBase, RealmObject { + Parent({ + Child1? child, + Iterable children = const [], + }) { + RealmObjectBase.set(this, 'child', child); + RealmObjectBase.set>( + this, 'children', RealmList(children)); + } + + Parent._(); + + @override + Child1? get child => RealmObjectBase.get(this, 'child') as Child1?; + @override + set child(covariant Child1? value) => + RealmObjectBase.set(this, 'child', value); + + @override + RealmList get children => + RealmObjectBase.get(this, 'children') as RealmList; + @override + set children(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + Stream> get changes => + RealmObjectBase.getChanges(this); + + @override + Parent freeze() => RealmObjectBase.freezeObject(this); + + static SchemaObject get schema => _schema ??= _initSchema(); + static SchemaObject? _schema; + static SchemaObject _initSchema() { + RealmObjectBase.registerFactory(Parent._); + return const SchemaObject(ObjectType.topLevel, Parent, 'Parent', [ + SchemaProperty('child', RealmPropertyType.object, + optional: true, linkTarget: 'Child1'), + SchemaProperty('children', RealmPropertyType.object, + linkTarget: 'Child1', collectionType: RealmCollectionType.list), + ]); + } +} + +class Child1 extends _Child1 with RealmEntity, RealmObjectBase, EmbeddedObject { + Child1( + String value, { + Child2? child, + Parent? linkToParent, + Iterable children = const [], + }) { + RealmObjectBase.set(this, 'value', value); + RealmObjectBase.set(this, 'child', child); + RealmObjectBase.set(this, 'linkToParent', linkToParent); + RealmObjectBase.set>( + this, 'children', RealmList(children)); + } + + Child1._(); + + @override + String get value => RealmObjectBase.get(this, 'value') as String; + @override + set value(String value) => RealmObjectBase.set(this, 'value', value); + + @override + Child2? get child => RealmObjectBase.get(this, 'child') as Child2?; + @override + set child(covariant Child2? value) => + RealmObjectBase.set(this, 'child', value); + + @override + RealmList get children => + RealmObjectBase.get(this, 'children') as RealmList; + @override + set children(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + Parent? get linkToParent => + RealmObjectBase.get(this, 'linkToParent') as Parent?; + @override + set linkToParent(covariant Parent? value) => + RealmObjectBase.set(this, 'linkToParent', value); + + @override + Stream> get changes => + RealmObjectBase.getChanges(this); + + @override + Child1 freeze() => RealmObjectBase.freezeObject(this); + + static SchemaObject get schema => _schema ??= _initSchema(); + static SchemaObject? _schema; + static SchemaObject _initSchema() { + RealmObjectBase.registerFactory(Child1._); + return const SchemaObject(ObjectType.embedded, Child1, 'Child1', [ + SchemaProperty('value', RealmPropertyType.string), + SchemaProperty('child', RealmPropertyType.object, + optional: true, linkTarget: 'Child2'), + SchemaProperty('children', RealmPropertyType.object, + linkTarget: 'Child2', collectionType: RealmCollectionType.list), + SchemaProperty('linkToParent', RealmPropertyType.object, + optional: true, linkTarget: 'Parent'), + ]); + } +} + +class Child2 extends _Child2 with RealmEntity, RealmObjectBase, EmbeddedObject { + Child2( + bool boolProp, + int intProp, + double doubleProp, + String stringProp, + DateTime dateProp, + ObjectId objectIdProp, + Uuid uuidProp, { + bool? nullableBoolProp, + int? nullableIntProp, + double? nullableDoubleProp, + String? nullableStringProp, + DateTime? nullableDateProp, + ObjectId? nullableObjectIdProp, + Uuid? nullableUuidProp, + }) { + RealmObjectBase.set(this, 'boolProp', boolProp); + RealmObjectBase.set(this, 'intProp', intProp); + RealmObjectBase.set(this, 'doubleProp', doubleProp); + RealmObjectBase.set(this, 'stringProp', stringProp); + RealmObjectBase.set(this, 'dateProp', dateProp); + RealmObjectBase.set(this, 'objectIdProp', objectIdProp); + RealmObjectBase.set(this, 'uuidProp', uuidProp); + RealmObjectBase.set(this, 'nullableBoolProp', nullableBoolProp); + RealmObjectBase.set(this, 'nullableIntProp', nullableIntProp); + RealmObjectBase.set(this, 'nullableDoubleProp', nullableDoubleProp); + RealmObjectBase.set(this, 'nullableStringProp', nullableStringProp); + RealmObjectBase.set(this, 'nullableDateProp', nullableDateProp); + RealmObjectBase.set(this, 'nullableObjectIdProp', nullableObjectIdProp); + RealmObjectBase.set(this, 'nullableUuidProp', nullableUuidProp); + } + + Child2._(); + + @override + bool get boolProp => RealmObjectBase.get(this, 'boolProp') as bool; + @override + set boolProp(bool value) => RealmObjectBase.set(this, 'boolProp', value); + + @override + int get intProp => RealmObjectBase.get(this, 'intProp') as int; + @override + set intProp(int value) => RealmObjectBase.set(this, 'intProp', value); + + @override + double get doubleProp => + RealmObjectBase.get(this, 'doubleProp') as double; + @override + set doubleProp(double value) => + RealmObjectBase.set(this, 'doubleProp', value); + + @override + String get stringProp => + RealmObjectBase.get(this, 'stringProp') as String; + @override + set stringProp(String value) => + RealmObjectBase.set(this, 'stringProp', value); + + @override + DateTime get dateProp => + RealmObjectBase.get(this, 'dateProp') as DateTime; + @override + set dateProp(DateTime value) => RealmObjectBase.set(this, 'dateProp', value); + + @override + ObjectId get objectIdProp => + RealmObjectBase.get(this, 'objectIdProp') as ObjectId; + @override + set objectIdProp(ObjectId value) => + RealmObjectBase.set(this, 'objectIdProp', value); + + @override + Uuid get uuidProp => RealmObjectBase.get(this, 'uuidProp') as Uuid; + @override + set uuidProp(Uuid value) => RealmObjectBase.set(this, 'uuidProp', value); + + @override + bool? get nullableBoolProp => + RealmObjectBase.get(this, 'nullableBoolProp') as bool?; + @override + set nullableBoolProp(bool? value) => + RealmObjectBase.set(this, 'nullableBoolProp', value); + + @override + int? get nullableIntProp => + RealmObjectBase.get(this, 'nullableIntProp') as int?; + @override + set nullableIntProp(int? value) => + RealmObjectBase.set(this, 'nullableIntProp', value); + + @override + double? get nullableDoubleProp => + RealmObjectBase.get(this, 'nullableDoubleProp') as double?; + @override + set nullableDoubleProp(double? value) => + RealmObjectBase.set(this, 'nullableDoubleProp', value); + + @override + String? get nullableStringProp => + RealmObjectBase.get(this, 'nullableStringProp') as String?; + @override + set nullableStringProp(String? value) => + RealmObjectBase.set(this, 'nullableStringProp', value); + + @override + DateTime? get nullableDateProp => + RealmObjectBase.get(this, 'nullableDateProp') as DateTime?; + @override + set nullableDateProp(DateTime? value) => + RealmObjectBase.set(this, 'nullableDateProp', value); + + @override + ObjectId? get nullableObjectIdProp => + RealmObjectBase.get(this, 'nullableObjectIdProp') as ObjectId?; + @override + set nullableObjectIdProp(ObjectId? value) => + RealmObjectBase.set(this, 'nullableObjectIdProp', value); + + @override + Uuid? get nullableUuidProp => + RealmObjectBase.get(this, 'nullableUuidProp') as Uuid?; + @override + set nullableUuidProp(Uuid? value) => + RealmObjectBase.set(this, 'nullableUuidProp', value); + + @override + Stream> get changes => + RealmObjectBase.getChanges(this); + + @override + Child2 freeze() => RealmObjectBase.freezeObject(this); + + static SchemaObject get schema => _schema ??= _initSchema(); + static SchemaObject? _schema; + static SchemaObject _initSchema() { + RealmObjectBase.registerFactory(Child2._); + return const SchemaObject(ObjectType.embedded, Child2, 'Child2', [ + SchemaProperty('boolProp', RealmPropertyType.bool), + SchemaProperty('intProp', RealmPropertyType.int), + SchemaProperty('doubleProp', RealmPropertyType.double), + SchemaProperty('stringProp', RealmPropertyType.string), + SchemaProperty('dateProp', RealmPropertyType.timestamp), + SchemaProperty('objectIdProp', RealmPropertyType.objectid), + SchemaProperty('uuidProp', RealmPropertyType.uuid), + SchemaProperty('nullableBoolProp', RealmPropertyType.bool, + optional: true), + SchemaProperty('nullableIntProp', RealmPropertyType.int, optional: true), + SchemaProperty('nullableDoubleProp', RealmPropertyType.double, + optional: true), + SchemaProperty('nullableStringProp', RealmPropertyType.string, + optional: true), + SchemaProperty('nullableDateProp', RealmPropertyType.timestamp, + optional: true), + SchemaProperty('nullableObjectIdProp', RealmPropertyType.objectid, + optional: true), + SchemaProperty('nullableUuidProp', RealmPropertyType.uuid, + optional: true), + ]); + } +} + diff --git a/generator/test/good_test_data/list_initialization.expected b/generator/test/good_test_data/list_initialization.expected index 574697ad6..8f7ac45f8 100644 --- a/generator/test/good_test_data/list_initialization.expected +++ b/generator/test/good_test_data/list_initialization.expected @@ -2,32 +2,38 @@ // RealmObjectGenerator // ************************************************************************** -class Person extends _Person with RealmEntity, RealmObject { +class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { Person({ Iterable children = const [], }) { - RealmObject.set>(this, 'children', RealmList(children)); + RealmObjectBase.set>( + this, 'children', RealmList(children)); } Person._(); @override - RealmList get children => RealmObject.get(this, 'children') as RealmList; + RealmList get children => + RealmObjectBase.get(this, 'children') as RealmList; @override - set children(covariant RealmList value) => throw RealmUnsupportedSetError(); + set children(covariant RealmList value) => + throw RealmUnsupportedSetError(); @override - Stream> get changes => RealmObject.getChanges(this); + Stream> get changes => + RealmObjectBase.getChanges(this); @override - Person freeze() => RealmObject.freezeObject(this); + Person freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Person._); - return const SchemaObject(Person, 'Person', [ - SchemaProperty('children', RealmPropertyType.object, linkTarget: 'Person', collectionType: RealmCollectionType.list), + RealmObjectBase.registerFactory(Person._); + return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ + SchemaProperty('children', RealmPropertyType.object, + linkTarget: 'Person', collectionType: RealmCollectionType.list), ]); } } + diff --git a/generator/test/good_test_data/mapto.expected b/generator/test/good_test_data/mapto.expected index 9b2e6d121..05b5fb468 100644 --- a/generator/test/good_test_data/mapto.expected +++ b/generator/test/good_test_data/mapto.expected @@ -2,7 +2,8 @@ // RealmObjectGenerator // ************************************************************************** -class Original extends $Original with RealmEntity, RealmObject { +class Original extends $Original + with RealmEntity, RealmObjectBase, RealmObject { static var _defaultsSet = false; Original({ @@ -11,13 +12,13 @@ class Original extends $Original with RealmEntity, RealmObject { Iterable listProperty = const [], }) { if (!_defaultsSet) { - _defaultsSet = RealmObject.setDefaults({ + _defaultsSet = RealmObjectBase.setDefaults({ 'remapped primitive': 0, }); } - RealmObject.set(this, 'remapped primitive', primitiveProperty); - RealmObject.set(this, 'remapped object', objectProperty); - RealmObject.set>( + RealmObjectBase.set(this, 'remapped primitive', primitiveProperty); + RealmObjectBase.set(this, 'remapped object', objectProperty); + RealmObjectBase.set>( this, 'remapped list', RealmList(listProperty)); } @@ -25,37 +26,38 @@ class Original extends $Original with RealmEntity, RealmObject { @override int get primitiveProperty => - RealmObject.get(this, 'remapped primitive') as int; + RealmObjectBase.get(this, 'remapped primitive') as int; @override set primitiveProperty(int value) => - RealmObject.set(this, 'remapped primitive', value); + RealmObjectBase.set(this, 'remapped primitive', value); @override Original? get objectProperty => - RealmObject.get(this, 'remapped object') as Original?; + RealmObjectBase.get(this, 'remapped object') as Original?; @override set objectProperty(covariant Original? value) => - RealmObject.set(this, 'remapped object', value); + RealmObjectBase.set(this, 'remapped object', value); @override RealmList get listProperty => - RealmObject.get(this, 'remapped list') as RealmList; + RealmObjectBase.get(this, 'remapped list') + as RealmList; @override set listProperty(covariant RealmList value) => throw RealmUnsupportedSetError(); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Original freeze() => RealmObject.freezeObject(this); + Original freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Original._); - return const SchemaObject(Original, 'another type', [ + RealmObjectBase.registerFactory(Original._); + return const SchemaObject(ObjectType.topLevel, Original, 'another type', [ SchemaProperty('remapped primitive', RealmPropertyType.int, mapTo: 'remapped primitive'), SchemaProperty('remapped object', RealmPropertyType.object, diff --git a/generator/test/good_test_data/optional_argument.expected b/generator/test/good_test_data/optional_argument.expected index 305b960bf..33dab79ad 100644 --- a/generator/test/good_test_data/optional_argument.expected +++ b/generator/test/good_test_data/optional_argument.expected @@ -2,32 +2,36 @@ // RealmObjectGenerator // ************************************************************************** -class Person extends _Person with RealmEntity, RealmObject { +class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { Person({ Person? spouse, }) { - RealmObject.set(this, 'spouse', spouse); + RealmObjectBase.set(this, 'spouse', spouse); } Person._(); @override - Person? get spouse => RealmObject.get(this, 'spouse') as Person?; + Person? get spouse => RealmObjectBase.get(this, 'spouse') as Person?; @override - set spouse(covariant Person? value) => RealmObject.set(this, 'spouse', value); + set spouse(covariant Person? value) => + RealmObjectBase.set(this, 'spouse', value); @override - Stream> get changes => RealmObject.getChanges(this); + Stream> get changes => + RealmObjectBase.getChanges(this); @override - Person freeze() => RealmObject.freezeObject(this); + Person freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Person._); - return const SchemaObject(Person, 'Person', [ - SchemaProperty('spouse', RealmPropertyType.object, optional: true, linkTarget: 'Person'), + RealmObjectBase.registerFactory(Person._); + return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ + SchemaProperty('spouse', RealmPropertyType.object, + optional: true, linkTarget: 'Person'), ]); } } + diff --git a/generator/test/good_test_data/pinhole.expected b/generator/test/good_test_data/pinhole.expected index 8eb7d2354..c071d93cf 100644 --- a/generator/test/good_test_data/pinhole.expected +++ b/generator/test/good_test_data/pinhole.expected @@ -2,39 +2,41 @@ // RealmObjectGenerator // ************************************************************************** -class Foo extends _Foo with RealmEntity, RealmObject { +class Foo extends _Foo with RealmEntity, RealmObjectBase, RealmObject { static var _defaultsSet = false; Foo({ int x = 0, }) { if (!_defaultsSet) { - _defaultsSet = RealmObject.setDefaults({ + _defaultsSet = RealmObjectBase.setDefaults({ 'x': 0, }); } - RealmObject.set(this, 'x', x); + RealmObjectBase.set(this, 'x', x); } Foo._(); @override - int get x => RealmObject.get(this, 'x') as int; + int get x => RealmObjectBase.get(this, 'x') as int; @override - set x(int value) => RealmObject.set(this, 'x', value); + set x(int value) => RealmObjectBase.set(this, 'x', value); @override - Stream> get changes => RealmObject.getChanges(this); + Stream> get changes => + RealmObjectBase.getChanges(this); @override - Foo freeze() => RealmObject.freezeObject(this); + Foo freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Foo._); - return const SchemaObject(Foo, 'Foo', [ + RealmObjectBase.registerFactory(Foo._); + return const SchemaObject(ObjectType.topLevel, Foo, 'Foo', [ SchemaProperty('x', RealmPropertyType.int), ]); } } + diff --git a/generator/test/good_test_data/primary_key.expected b/generator/test/good_test_data/primary_key.expected index 68b02ff12..136e71258 100644 --- a/generator/test/good_test_data/primary_key.expected +++ b/generator/test/good_test_data/primary_key.expected @@ -2,254 +2,267 @@ // RealmObjectGenerator // ************************************************************************** -class IntPK extends _IntPK with RealmEntity, RealmObject { +class IntPK extends _IntPK with RealmEntity, RealmObjectBase, RealmObject { IntPK( int id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } IntPK._(); @override - int get id => RealmObject.get(this, 'id') as int; + int get id => RealmObjectBase.get(this, 'id') as int; @override - set id(int value) => RealmObject.set(this, 'id', value); + set id(int value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - IntPK freeze() => RealmObject.freezeObject(this); + IntPK freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(IntPK._); - return const SchemaObject(IntPK, 'IntPK', [ + RealmObjectBase.registerFactory(IntPK._); + return const SchemaObject(ObjectType.topLevel, IntPK, 'IntPK', [ SchemaProperty('id', RealmPropertyType.int, primaryKey: true), ]); } } -class NullableIntPK extends _NullableIntPK with RealmEntity, RealmObject { +class NullableIntPK extends _NullableIntPK + with RealmEntity, RealmObjectBase, RealmObject { NullableIntPK( int? id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } NullableIntPK._(); @override - int? get id => RealmObject.get(this, 'id') as int?; + int? get id => RealmObjectBase.get(this, 'id') as int?; @override - set id(int? value) => RealmObject.set(this, 'id', value); + set id(int? value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - NullableIntPK freeze() => RealmObject.freezeObject(this); + NullableIntPK freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(NullableIntPK._); - return const SchemaObject(NullableIntPK, 'NullableIntPK', [ + RealmObjectBase.registerFactory(NullableIntPK._); + return const SchemaObject( + ObjectType.topLevel, NullableIntPK, 'NullableIntPK', [ SchemaProperty('id', RealmPropertyType.int, optional: true, primaryKey: true), ]); } } -class StringPK extends _StringPK with RealmEntity, RealmObject { +class StringPK extends _StringPK + with RealmEntity, RealmObjectBase, RealmObject { StringPK( String id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } StringPK._(); @override - String get id => RealmObject.get(this, 'id') as String; + String get id => RealmObjectBase.get(this, 'id') as String; @override - set id(String value) => RealmObject.set(this, 'id', value); + set id(String value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - StringPK freeze() => RealmObject.freezeObject(this); + StringPK freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(StringPK._); - return const SchemaObject(StringPK, 'StringPK', [ + RealmObjectBase.registerFactory(StringPK._); + return const SchemaObject(ObjectType.topLevel, StringPK, 'StringPK', [ SchemaProperty('id', RealmPropertyType.string, primaryKey: true), ]); } } -class NullableStringPK extends _NullableStringPK with RealmEntity, RealmObject { +class NullableStringPK extends _NullableStringPK + with RealmEntity, RealmObjectBase, RealmObject { NullableStringPK( String? id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } NullableStringPK._(); @override - String? get id => RealmObject.get(this, 'id') as String?; + String? get id => RealmObjectBase.get(this, 'id') as String?; @override - set id(String? value) => RealmObject.set(this, 'id', value); + set id(String? value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - NullableStringPK freeze() => RealmObject.freezeObject(this); + NullableStringPK freeze() => + RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(NullableStringPK._); - return const SchemaObject(NullableStringPK, 'NullableStringPK', [ + RealmObjectBase.registerFactory(NullableStringPK._); + return const SchemaObject( + ObjectType.topLevel, NullableStringPK, 'NullableStringPK', [ SchemaProperty('id', RealmPropertyType.string, optional: true, primaryKey: true), ]); } } -class ObjectIdPK extends _ObjectIdPK with RealmEntity, RealmObject { +class ObjectIdPK extends _ObjectIdPK + with RealmEntity, RealmObjectBase, RealmObject { ObjectIdPK( ObjectId id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } ObjectIdPK._(); @override - ObjectId get id => RealmObject.get(this, 'id') as ObjectId; + ObjectId get id => RealmObjectBase.get(this, 'id') as ObjectId; @override - set id(ObjectId value) => RealmObject.set(this, 'id', value); + set id(ObjectId value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - ObjectIdPK freeze() => RealmObject.freezeObject(this); + ObjectIdPK freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(ObjectIdPK._); - return const SchemaObject(ObjectIdPK, 'ObjectIdPK', [ + RealmObjectBase.registerFactory(ObjectIdPK._); + return const SchemaObject(ObjectType.topLevel, ObjectIdPK, 'ObjectIdPK', [ SchemaProperty('id', RealmPropertyType.objectid, primaryKey: true), ]); } } -class NullableObjectIdPK extends _NullableObjectIdPK with RealmEntity, RealmObject { +class NullableObjectIdPK extends _NullableObjectIdPK + with RealmEntity, RealmObjectBase, RealmObject { NullableObjectIdPK( ObjectId? id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } NullableObjectIdPK._(); @override - ObjectId? get id => RealmObject.get(this, 'id') as ObjectId?; + ObjectId? get id => RealmObjectBase.get(this, 'id') as ObjectId?; @override - set id(ObjectId? value) => RealmObject.set(this, 'id', value); + set id(ObjectId? value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - NullableObjectIdPK freeze() => RealmObject.freezeObject(this); + NullableObjectIdPK freeze() => + RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(NullableObjectIdPK._); - return const SchemaObject(NullableObjectIdPK, 'NullableObjectIdPK', [ + RealmObjectBase.registerFactory(NullableObjectIdPK._); + return const SchemaObject( + ObjectType.topLevel, NullableObjectIdPK, 'NullableObjectIdPK', [ SchemaProperty('id', RealmPropertyType.objectid, optional: true, primaryKey: true), ]); } } -class UuidPK extends _UuidPK with RealmEntity, RealmObject { +class UuidPK extends _UuidPK with RealmEntity, RealmObjectBase, RealmObject { UuidPK( Uuid id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } UuidPK._(); @override - Uuid get id => RealmObject.get(this, 'id') as Uuid; + Uuid get id => RealmObjectBase.get(this, 'id') as Uuid; @override - set id(Uuid value) => RealmObject.set(this, 'id', value); + set id(Uuid value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - UuidPK freeze() => RealmObject.freezeObject(this); + UuidPK freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(UuidPK._); - return const SchemaObject(UuidPK, 'UuidPK', [ + RealmObjectBase.registerFactory(UuidPK._); + return const SchemaObject(ObjectType.topLevel, UuidPK, 'UuidPK', [ SchemaProperty('id', RealmPropertyType.uuid, primaryKey: true), ]); } } -class NullableUuidPK extends _NullableUuidPK with RealmEntity, RealmObject { +class NullableUuidPK extends _NullableUuidPK + with RealmEntity, RealmObjectBase, RealmObject { NullableUuidPK( Uuid? id, ) { - RealmObject.set(this, 'id', id); + RealmObjectBase.set(this, 'id', id); } NullableUuidPK._(); @override - Uuid? get id => RealmObject.get(this, 'id') as Uuid?; + Uuid? get id => RealmObjectBase.get(this, 'id') as Uuid?; @override - set id(Uuid? value) => RealmObject.set(this, 'id', value); + set id(Uuid? value) => RealmObjectBase.set(this, 'id', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - NullableUuidPK freeze() => RealmObject.freezeObject(this); + NullableUuidPK freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(NullableUuidPK._); - return const SchemaObject(NullableUuidPK, 'NullableUuidPK', [ + RealmObjectBase.registerFactory(NullableUuidPK._); + return const SchemaObject( + ObjectType.topLevel, NullableUuidPK, 'NullableUuidPK', [ SchemaProperty('id', RealmPropertyType.uuid, optional: true, primaryKey: true), ]); } -} \ No newline at end of file +} + diff --git a/generator/test/good_test_data/required_argument.expected b/generator/test/good_test_data/required_argument.expected index 68552d19a..d58318e17 100644 --- a/generator/test/good_test_data/required_argument.expected +++ b/generator/test/good_test_data/required_argument.expected @@ -2,32 +2,34 @@ // RealmObjectGenerator // ************************************************************************** -class Person extends _Person with RealmEntity, RealmObject { +class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { Person( String name, ) { - RealmObject.set(this, 'name', name); + RealmObjectBase.set(this, 'name', name); } Person._(); @override - String get name => RealmObject.get(this, 'name') as String; + String get name => RealmObjectBase.get(this, 'name') as String; @override - set name(String value) => RealmObject.set(this, 'name', value); + set name(String value) => RealmObjectBase.set(this, 'name', value); @override - Stream> get changes => RealmObject.getChanges(this); + Stream> get changes => + RealmObjectBase.getChanges(this); @override - Person freeze() => RealmObject.freezeObject(this); + Person freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Person._); - return const SchemaObject(Person, 'Person', [ + RealmObjectBase.registerFactory(Person._); + return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), ]); } } + diff --git a/generator/test/good_test_data/required_argument_with_default_value.expected b/generator/test/good_test_data/required_argument_with_default_value.expected index 688231e51..0c3619182 100644 --- a/generator/test/good_test_data/required_argument_with_default_value.expected +++ b/generator/test/good_test_data/required_argument_with_default_value.expected @@ -2,39 +2,41 @@ // RealmObjectGenerator // ************************************************************************** -class Person extends _Person with RealmEntity, RealmObject { +class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static var _defaultsSet = false; Person({ int age = 47, }) { if (!_defaultsSet) { - _defaultsSet = RealmObject.setDefaults({ + _defaultsSet = RealmObjectBase.setDefaults({ 'age': 47, }); } - RealmObject.set(this, 'age', age); + RealmObjectBase.set(this, 'age', age); } Person._(); @override - int get age => RealmObject.get(this, 'age') as int; + int get age => RealmObjectBase.get(this, 'age') as int; @override - set age(int value) => RealmObject.set(this, 'age', value); + set age(int value) => RealmObjectBase.set(this, 'age', value); @override - Stream> get changes => RealmObject.getChanges(this); + Stream> get changes => + RealmObjectBase.getChanges(this); @override - Person freeze() => RealmObject.freezeObject(this); + Person freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Person._); - return const SchemaObject(Person, 'Person', [ + RealmObjectBase.registerFactory(Person._); + return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ SchemaProperty('age', RealmPropertyType.int), ]); } } + diff --git a/generator/test/good_test_data/user_defined_getter.expected b/generator/test/good_test_data/user_defined_getter.expected index a5ef2d006..569d9993f 100644 --- a/generator/test/good_test_data/user_defined_getter.expected +++ b/generator/test/good_test_data/user_defined_getter.expected @@ -2,32 +2,34 @@ // RealmObjectGenerator // ************************************************************************** -class Person extends _Person with RealmEntity, RealmObject { +class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { Person( String name, ) { - RealmObject.set(this, 'name', name); + RealmObjectBase.set(this, 'name', name); } Person._(); @override - String get name => RealmObject.get(this, 'name') as String; + String get name => RealmObjectBase.get(this, 'name') as String; @override - set name(String value) => RealmObject.set(this, 'name', value); + set name(String value) => RealmObjectBase.set(this, 'name', value); @override - Stream> get changes => RealmObject.getChanges(this); + Stream> get changes => + RealmObjectBase.getChanges(this); @override - Person freeze() => RealmObject.freezeObject(this); + Person freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Person._); - return const SchemaObject(Person, 'Person', [ + RealmObjectBase.registerFactory(Person._); + return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string), ]); } } + From af2aafca1ab5b3612ea2414ffca016fa300a7649 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Thu, 20 Oct 2022 00:13:13 +0200 Subject: [PATCH 03/13] Fix tests --- flutter/realm_flutter/example/lib/main.g.dart | 61 ++++++++++--------- test/list_test.dart | 2 +- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/flutter/realm_flutter/example/lib/main.g.dart b/flutter/realm_flutter/example/lib/main.g.dart index 96e747646..0dc5e98f0 100644 --- a/flutter/realm_flutter/example/lib/main.g.dart +++ b/flutter/realm_flutter/example/lib/main.g.dart @@ -6,7 +6,7 @@ part of 'main.dart'; // RealmObjectGenerator // ************************************************************************** -class Car extends _Car with RealmEntity, RealmObject { +class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static var _defaultsSet = false; Car( @@ -16,50 +16,51 @@ class Car extends _Car with RealmEntity, RealmObject { Person? owner, }) { if (!_defaultsSet) { - _defaultsSet = RealmObject.setDefaults({ + _defaultsSet = RealmObjectBase.setDefaults({ 'kilometers': 500, }); } - RealmObject.set(this, 'make', make); - RealmObject.set(this, 'model', model); - RealmObject.set(this, 'kilometers', kilometers); - RealmObject.set(this, 'owner', owner); + RealmObjectBase.set(this, 'make', make); + RealmObjectBase.set(this, 'model', model); + RealmObjectBase.set(this, 'kilometers', kilometers); + RealmObjectBase.set(this, 'owner', owner); } Car._(); @override - String get make => RealmObject.get(this, 'make') as String; + String get make => RealmObjectBase.get(this, 'make') as String; @override - set make(String value) => RealmObject.set(this, 'make', value); + set make(String value) => RealmObjectBase.set(this, 'make', value); @override - String? get model => RealmObject.get(this, 'model') as String?; + String? get model => RealmObjectBase.get(this, 'model') as String?; @override - set model(String? value) => RealmObject.set(this, 'model', value); + set model(String? value) => RealmObjectBase.set(this, 'model', value); @override - int? get kilometers => RealmObject.get(this, 'kilometers') as int?; + int? get kilometers => RealmObjectBase.get(this, 'kilometers') as int?; @override - set kilometers(int? value) => RealmObject.set(this, 'kilometers', value); + set kilometers(int? value) => RealmObjectBase.set(this, 'kilometers', value); @override - Person? get owner => RealmObject.get(this, 'owner') as Person?; + Person? get owner => RealmObjectBase.get(this, 'owner') as Person?; @override - set owner(covariant Person? value) => RealmObject.set(this, 'owner', value); + set owner(covariant Person? value) => + RealmObjectBase.set(this, 'owner', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Car freeze() => RealmObject.freezeObject(this); + Car freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Car._); - return const SchemaObject(Car, 'Car', [ + RealmObjectBase.registerFactory(Car._); + return const SchemaObject(ObjectType.topLevel, Car, 'Car', [ SchemaProperty('make', RealmPropertyType.string), SchemaProperty('model', RealmPropertyType.string, optional: true), SchemaProperty('kilometers', RealmPropertyType.int, optional: true), @@ -69,7 +70,7 @@ class Car extends _Car with RealmEntity, RealmObject { } } -class Person extends _Person with RealmEntity, RealmObject { +class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static var _defaultsSet = false; Person( @@ -77,38 +78,38 @@ class Person extends _Person with RealmEntity, RealmObject { int age = 1, }) { if (!_defaultsSet) { - _defaultsSet = RealmObject.setDefaults({ + _defaultsSet = RealmObjectBase.setDefaults({ 'age': 1, }); } - RealmObject.set(this, 'name', name); - RealmObject.set(this, 'age', age); + RealmObjectBase.set(this, 'name', name); + RealmObjectBase.set(this, 'age', age); } Person._(); @override - String get name => RealmObject.get(this, 'name') as String; + String get name => RealmObjectBase.get(this, 'name') as String; @override - set name(String value) => RealmObject.set(this, 'name', value); + set name(String value) => RealmObjectBase.set(this, 'name', value); @override - int get age => RealmObject.get(this, 'age') as int; + int get age => RealmObjectBase.get(this, 'age') as int; @override - set age(int value) => RealmObject.set(this, 'age', value); + set age(int value) => RealmObjectBase.set(this, 'age', value); @override Stream> get changes => - RealmObject.getChanges(this); + RealmObjectBase.getChanges(this); @override - Person freeze() => RealmObject.freezeObject(this); + Person freeze() => RealmObjectBase.freezeObject(this); static SchemaObject get schema => _schema ??= _initSchema(); static SchemaObject? _schema; static SchemaObject _initSchema() { - RealmObject.registerFactory(Person._); - return const SchemaObject(Person, 'Person', [ + RealmObjectBase.registerFactory(Person._); + return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('age', RealmPropertyType.int), ]); diff --git a/test/list_test.dart b/test/list_test.dart index 36f0bb61d..1688bce91 100644 --- a/test/list_test.dart +++ b/test/list_test.dart @@ -93,7 +93,7 @@ Future main([List? args]) async { final teams = realm.all(); final players = teams[0].players; - expect(() => realm.write(() => players[-1] = Person('')), throws("Index out of range")); + expect(() => realm.write(() => players[-1] = Person('')), throws("Index can not be negative")); expect(() => realm.write(() => players[800] = Person('')), throws()); }); From 71d4542f2b681b44f97c98f700a8f1b51ccefb57 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Thu, 20 Oct 2022 18:05:18 +0200 Subject: [PATCH 04/13] Address some PR feedback --- common/lib/src/realm_common_base.dart | 19 ++++++++---- generator/lib/src/class_element_ex.dart | 2 +- .../embedded_object_primary_key.expected | 2 +- lib/src/native/realm_core.dart | 30 +++---------------- 4 files changed, 19 insertions(+), 34 deletions(-) diff --git a/common/lib/src/realm_common_base.dart b/common/lib/src/realm_common_base.dart index 58d23362a..dc80eea23 100644 --- a/common/lib/src/realm_common_base.dart +++ b/common/lib/src/realm_common_base.dart @@ -22,21 +22,28 @@ enum ObjectType { /// A standalone top-level object that can be persisted in Realm. It can link /// to other objects or collections of other objects. - topLevel('RealmObject'), + topLevel('RealmObject', 0), /// An object that can be embedded in other objects. It is considered owned /// by its parent and will be deleted if its parent is deleted. - embedded('EmbeddedObject'), + embedded('EmbeddedObject', 1), /// A special type of object used to facilitate unidirectional synchronization /// with Atlas App Services. It is used to push data to Realm without the ability /// to query or modify it. - _asymmetric('AsymmetricObject'); + _asymmetric('AsymmetricObject', 2); - const ObjectType([this.className = 'Unknown']); + const ObjectType([this._className = 'Unknown', this._flags = -1]); - /// The name of the base class exposed by the Realm SDK. - final String className; + final String _className; + + final int _flags; +} + +extension ObjectTypeInternal on ObjectType { + int get flags => _flags; + + String get className => _className; } /// Annotation class used to define `Realm` data model classes and their properties diff --git a/generator/lib/src/class_element_ex.dart b/generator/lib/src/class_element_ex.dart index ab2d78b44..1323879de 100644 --- a/generator/lib/src/class_element_ex.dart +++ b/generator/lib/src/class_element_ex.dart @@ -158,7 +158,7 @@ extension ClassElementEx on ClassElement { primarySpan: pkSpan, secondarySpans: {span!: ''}, primaryLabel: "$realmName is marked as embedded but has primary key defined", - todo: 'Remove the @PrimaryKey annotation from the field or set the object type to topLevel.'); + todo: 'Remove the @PrimaryKey annotation from the field or set the model type to a value different from ObjectType.embedded.'); } return RealmModelInfo(name, modelName, realmName, mappedFields, objectType); diff --git a/generator/test/error_test_data/embedded_object_primary_key.expected b/generator/test/error_test_data/embedded_object_primary_key.expected index fa7e93a53..f482f03a9 100644 --- a/generator/test/error_test_data/embedded_object_primary_key.expected +++ b/generator/test/error_test_data/embedded_object_primary_key.expected @@ -9,5 +9,5 @@ in: asset:pkg/test/error_test_data/embedded_object_primary_key.dart:8:12 8 │ late int id; │ ^^ Bad is marked as embedded but has primary key defined ╵ -Remove the @PrimaryKey annotation from the field or set the object type to topLevel. +Remove the @PrimaryKey annotation from the field or set the model type to a value different from ObjectType.embedded. diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index 193fb7d91..e9fb13ed7 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -28,6 +28,7 @@ import 'package:cancellation_token/cancellation_token.dart'; import 'package:ffi/ffi.dart' hide StringUtf8Pointer, StringUtf16Pointer; import 'package:logging/logging.dart'; import 'package:path/path.dart' as path; +import 'package:realm_common/realm_common.dart'; import '../app.dart'; import '../collections.dart'; @@ -125,7 +126,7 @@ class _RealmCore { classInfo.num_properties = schemaObject.properties.length; classInfo.num_computed_properties = 0; classInfo.key = RLM_INVALID_CLASS_KEY; - classInfo.flags = schemaObject.baseType.toFlags(); + classInfo.flags = schemaObject.baseType.flags; final propertiesCount = schemaObject.properties.length; final properties = arena(propertiesCount); @@ -530,7 +531,8 @@ class _RealmCore { _realmLib.invokeGetBool(() => _realmLib.realm_get_class(realm.handle._pointer, classKey, classInfo)); final name = classInfo.ref.name.cast().toDartString(); - final baseType = ObjectTypeNative.fromFlags(classInfo.ref.flags); + final baseType = ObjectType.values.firstWhere((element) => element.flags == classInfo.ref.flags, + orElse: () => throw RealmError('No object type found for flags ${classInfo.ref.flags}')); final schema = _getSchemaForClassKey(realm, classKey, name, baseType, arena, expectedSize: classInfo.ref.num_properties + classInfo.ref.num_computed_properties); schemas.add(schema); @@ -2627,30 +2629,6 @@ extension on Completer { } } -extension ObjectTypeNative on ObjectType { - int toFlags() { - switch (this) { - case ObjectType.topLevel: - return realm_class_flags.RLM_CLASS_NORMAL; - case ObjectType.embedded: - return realm_class_flags.RLM_CLASS_EMBEDDED; - default: - throw RealmException('Invalid ObjectType: $this'); - } - } - - static ObjectType fromFlags(int flags) { - switch (flags) { - case realm_class_flags.RLM_CLASS_NORMAL: - return ObjectType.topLevel; - case realm_class_flags.RLM_CLASS_EMBEDDED: - return ObjectType.embedded; - default: - throw RealmException('Invalid ObjectType: $flags'); - } - } -} - enum _CustomErrorCode { noError(0), unknownHttp(998), From 7d52097a6f7bc986db0ac257da66553522c137de Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Thu, 20 Oct 2022 21:54:25 +0200 Subject: [PATCH 05/13] Add tests for freeze and notifications --- CHANGELOG.md | 17 +++ test/embedded_test.dart | 229 +++++++++++++++++++++++++++++++++++++++- test/test.dart | 4 +- 3 files changed, 244 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc1c3f1c4..cc45ca3b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,23 @@ * Added `realm.beginWriteAsync` which returns a `Future` that resolves when the write lock has been obtained. * Added `realm.writeAsync` which opens an asynchronous transaction, invokes the provided callback, then commits the transaction asynchronously. * Support `Realm.open` API to asynchronously open a local or synced Realm. When opening a synchronized Realm it will download all the content available at the time the operation began and then return a usable Realm. ([#731](https://github.com/realm/realm-dart/pull/731)) +* Add support for embedded objects. Embedded objects are objects which are owned by a single parent object, and are deleted when that parent object is deleted or their parent no longer references them. Embedded objects are declared by passing `ObjectType.embedded` to the `@RealmModel` annotation. Reassigning an embedded object is not allowed and neither is linking to it from multiple parents. Querying for embedded objects directly is also disallowed as they should be viewed as complex structures belonging to their parents as opposed to standalone objects. (Issue [#662](https://github.com/realm/realm-dart/issues/662)) + +```dart +@RealmModel() +class _Person { + late String name; + + _Address? address; +} + +// The generated `Address` class will be an embedded object. +@RealmModel(ObjectType.embedded) +class _Address { + late String street; + late String city; +} +``` ### Fixed * Added more validations when using `User.apiKeys` to return more meaningful errors when the user cannot perform API key actions - e.g. when the user has been logged in with API key credentials or when the user has been logged out. (Issue [#950](https://github.com/realm/realm-dart/issues/950)) diff --git a/test/embedded_test.dart b/test/embedded_test.dart index 8eec5cb26..3b62ce71c 100644 --- a/test/embedded_test.dart +++ b/test/embedded_test.dart @@ -16,8 +16,6 @@ // //////////////////////////////////////////////////////////////////////////////// -// ignore_for_file: unused_local_variable - import 'package:test/test.dart' hide test, throws; import '../lib/realm.dart'; @@ -448,7 +446,7 @@ Future main([List? args]) async { }); for (final isDynamic in [true, false]) { - Realm _getDynamicRealm(Realm original) { + Realm getDynamicRealm(Realm original) { if (isDynamic) { original.close(); return getRealm(Configuration.local([])); @@ -467,7 +465,7 @@ Future main([List? args]) async { recursiveList: [RecursiveEmbedded1('1.2')])); }); - final dynamicRealm = _getDynamicRealm(realm); + final dynamicRealm = getDynamicRealm(realm); final parent = dynamicRealm.dynamic.find('ObjectWithEmbedded', '123')!; // String API with casting @@ -530,6 +528,229 @@ Future main([List? args]) async { expect(dynamicChild31.value, '3.1'); }); } + + test('Embedded object can be frozen', () { + final realm = getLocalRealm(); + + final parent = ObjectWithEmbedded('123', + recursiveList: [ + RecursiveEmbedded1('1.1'), + RecursiveEmbedded1('1.2'), + ], + recursiveObject: RecursiveEmbedded1('1.3', child: RecursiveEmbedded2('2.3'))); + + realm.write(() { + realm.add(parent); + }); + + final frozenEmbedded = freezeObject(parent.recursiveObject!); + expect(() => frozenEmbedded.changes, throws('Object is frozen and cannot emit changes')); + expect(frozenEmbedded.isFrozen, true); + expect(frozenEmbedded.value, '1.3'); + expect(frozenEmbedded.child!.isFrozen, true); + expect(frozenEmbedded.child!.value, '2.3'); + + expect(parent.isFrozen, false); + + realm.write(() { + parent.recursiveObject!.value = '1.4'; + }); + + expect(frozenEmbedded.value, '1.3'); + + final frozenListElement = freezeObject(parent.recursiveList[0]); + + expect(frozenListElement.isFrozen, true); + expect(parent.isFrozen, false); + expect(parent.recursiveList.isFrozen, false); + + realm.write(() { + parent.recursiveList[0].value = '99'; + }); + + expect(frozenListElement.value, '1.1'); + }); + + test('Embedded list can be frozen', () { + final realm = getLocalRealm(); + + final parent = ObjectWithEmbedded('123', recursiveList: [ + RecursiveEmbedded1('1.1'), + RecursiveEmbedded1('1.2'), + ]); + + realm.write(() { + realm.add(parent); + }); + + final frozenList = freezeList(parent.recursiveList); + expect(frozenList.isFrozen, true); + expect(frozenList[0].isFrozen, true); + expect(parent.isFrozen, false); + + realm.write(() { + parent.recursiveList[0].value = '99'; + }); + + expect(frozenList[0].value, '1.1'); + }); + + test('Embedded results can be frozen', () { + final realm = getLocalRealm(); + + final parent = ObjectWithEmbedded('123', recursiveList: [ + RecursiveEmbedded1('1.1'), + RecursiveEmbedded1('1.2'), + ]); + + realm.write(() { + realm.add(parent); + }); + + final frozenResults = freezeResults(parent.recursiveList.query('TRUEPREDICATE')); + expect(frozenResults.isFrozen, true); + expect(frozenResults[0].isFrozen, true); + expect(parent.isFrozen, false); + + realm.write(() { + parent.recursiveList[0].value = '99'; + }); + + expect(frozenResults[0].value, '1.1'); + }); + + Future waitForNotification(List notifications) async { + await waitForCondition(() => notifications.length == 1, retryDelay: Duration(milliseconds: 10)); + return notifications.removeAt(0); + } + + test('Embedded object notifications', () async { + final realm = getLocalRealm(); + + final parent = ObjectWithEmbedded('123', recursiveObject: RecursiveEmbedded1('1.3', child: RecursiveEmbedded2('2.3'))); + + realm.write(() { + realm.add(parent); + }); + + final notifications = []; + final subscription = parent.recursiveObject!.changes.listen((event) { + notifications.add(event); + }); + + // Initial notification + await waitForNotification(notifications); + + realm.write(() { + parent.recursiveObject!.value = '1.4'; + }); + + var event = await waitForNotification(notifications); + expect(event.properties, ['value']); + + realm.write(() { + parent.recursiveObject!.child = null; + }); + + event = await waitForNotification(notifications); + expect(event.properties, ['child']); + + realm.write(() { + parent.recursiveObject = null; + }); + + event = await waitForNotification(notifications); + expect(event.isDeleted, true); + expect(event.properties, isEmpty); + + await subscription.cancel(); + }); + + test('Embedded list notifications', () async { + final realm = getLocalRealm(); + + final parent = ObjectWithEmbedded('123', recursiveList: [ + RecursiveEmbedded1('1.1'), + ]); + + realm.write(() { + realm.add(parent); + }); + + final notifications = []; + final subscription = parent.recursiveList.changes.listen((event) { + notifications.add(event); + }); + + // Initial notification + await waitForNotification(notifications); + + realm.write(() { + parent.recursiveList.add(RecursiveEmbedded1('1.2')); + }); + + var event = await waitForNotification(notifications); + expect(event.inserted, [1]); + + realm.write(() { + parent.recursiveList[0].value = '99'; + }); + + event = await waitForNotification(notifications); + expect(event.modified, [0]); + + realm.write(() { + parent.recursiveList.removeAt(0); + }); + + event = await waitForNotification(notifications); + expect(event.deleted, [0]); + + await subscription.cancel(); + }); + + test('Embedded results notifications', () async { + final realm = getLocalRealm(); + + final parent = ObjectWithEmbedded('123', recursiveList: [ + RecursiveEmbedded1('1.1'), + ]); + + realm.write(() { + realm.add(parent); + }); + + final notifications = []; + final subscription = parent.recursiveList.query('TRUEPREDICATE').changes.listen((event) { + notifications.add(event); + }); + + // Initial notification + await waitForNotification(notifications); + + realm.write(() { + parent.recursiveList.add(RecursiveEmbedded1('1.2')); + }); + + var event = await waitForNotification(notifications); + expect(event.inserted, [1]); + + realm.write(() { + parent.recursiveList[0].value = '99'; + }); + + event = await waitForNotification(notifications); + expect(event.modified, [0]); + + realm.write(() { + parent.recursiveList.removeAt(0); + }); + + event = await waitForNotification(notifications); + expect(event.deleted, [0]); + + await subscription.cancel(); + }); } extension on RealmObjectBase { diff --git a/test/test.dart b/test/test.dart index 7e3e72908..47da6ae3d 100644 --- a/test/test.dart +++ b/test/test.dart @@ -429,7 +429,7 @@ Realm freezeRealm(Realm realm) { /// This is needed to make sure the frozen Realm gets forcefully closed by the /// time the test ends. -RealmResults freezeResults(RealmResults results) { +RealmResults freezeResults(RealmResults results) { final frozen = results.freeze(); _openRealms.add(frozen.realm); return frozen; @@ -445,7 +445,7 @@ RealmList freezeList(RealmList list) { /// This is needed to make sure the frozen Realm gets forcefully closed by the /// time the test ends. -T freezeObject(T object) { +T freezeObject(T object) { final frozen = object.freeze(); _openRealms.add(frozen.realm); return frozen as T; From ebd43336989d5b446caec7bd64ff6cce2984489f Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Thu, 20 Oct 2022 22:16:00 +0200 Subject: [PATCH 06/13] Add annotation tests --- .../good_test_data/embedded_annotations.dart | 26 +++++ .../embedded_annotations.expected | 104 ++++++++++++++++++ test/dynamic_realm_test.dart | 3 - test/embedded_test.dart | 4 +- test/migration_test.dart | 3 - test/user_test.dart | 4 - 6 files changed, 132 insertions(+), 12 deletions(-) create mode 100644 generator/test/good_test_data/embedded_annotations.dart create mode 100644 generator/test/good_test_data/embedded_annotations.expected diff --git a/generator/test/good_test_data/embedded_annotations.dart b/generator/test/good_test_data/embedded_annotations.dart new file mode 100644 index 000000000..456b42fdf --- /dev/null +++ b/generator/test/good_test_data/embedded_annotations.dart @@ -0,0 +1,26 @@ +import 'package:realm_common/realm_common.dart'; + +@RealmModel() +class _Parent { + @MapTo('single child') + late _Child1? child; + + @MapTo('CHILDREN') + late List<_Child1> children; +} + +@RealmModel(ObjectType.embedded) +@MapTo('MySuperChild') +class _Child1 { + @MapTo('_value') + late String value; + + @MapTo('_parent') + late _Parent? linkToParent; + + @Indexed() + late String indexedString; + + @Ignored() + late String ignoredString; +} diff --git a/generator/test/good_test_data/embedded_annotations.expected b/generator/test/good_test_data/embedded_annotations.expected new file mode 100644 index 000000000..361820d1c --- /dev/null +++ b/generator/test/good_test_data/embedded_annotations.expected @@ -0,0 +1,104 @@ +// ************************************************************************** +// RealmObjectGenerator +// ************************************************************************** + +class Parent extends _Parent with RealmEntity, RealmObjectBase, RealmObject { + Parent({ + Child1? child, + Iterable children = const [], + }) { + RealmObjectBase.set(this, 'single child', child); + RealmObjectBase.set>( + this, 'CHILDREN', RealmList(children)); + } + + Parent._(); + + @override + Child1? get child => + RealmObjectBase.get(this, 'single child') as Child1?; + @override + set child(covariant Child1? value) => + RealmObjectBase.set(this, 'single child', value); + + @override + RealmList get children => + RealmObjectBase.get(this, 'CHILDREN') as RealmList; + @override + set children(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + Stream> get changes => + RealmObjectBase.getChanges(this); + + @override + Parent freeze() => RealmObjectBase.freezeObject(this); + + static SchemaObject get schema => _schema ??= _initSchema(); + static SchemaObject? _schema; + static SchemaObject _initSchema() { + RealmObjectBase.registerFactory(Parent._); + return const SchemaObject(ObjectType.topLevel, Parent, 'Parent', [ + SchemaProperty('single child', RealmPropertyType.object, + mapTo: 'single child', optional: true, linkTarget: 'MySuperChild'), + SchemaProperty('CHILDREN', RealmPropertyType.object, + mapTo: 'CHILDREN', + linkTarget: 'MySuperChild', + collectionType: RealmCollectionType.list), + ]); + } +} + +class Child1 extends _Child1 with RealmEntity, RealmObjectBase, EmbeddedObject { + Child1( + String value, + String indexedString, { + Parent? linkToParent, + }) { + RealmObjectBase.set(this, '_value', value); + RealmObjectBase.set(this, '_parent', linkToParent); + RealmObjectBase.set(this, 'indexedString', indexedString); + } + + Child1._(); + + @override + String get value => RealmObjectBase.get(this, '_value') as String; + @override + set value(String value) => RealmObjectBase.set(this, '_value', value); + + @override + Parent? get linkToParent => + RealmObjectBase.get(this, '_parent') as Parent?; + @override + set linkToParent(covariant Parent? value) => + RealmObjectBase.set(this, '_parent', value); + + @override + String get indexedString => + RealmObjectBase.get(this, 'indexedString') as String; + @override + set indexedString(String value) => + RealmObjectBase.set(this, 'indexedString', value); + + @override + Stream> get changes => + RealmObjectBase.getChanges(this); + + @override + Child1 freeze() => RealmObjectBase.freezeObject(this); + + static SchemaObject get schema => _schema ??= _initSchema(); + static SchemaObject? _schema; + static SchemaObject _initSchema() { + RealmObjectBase.registerFactory(Child1._); + return const SchemaObject(ObjectType.embedded, Child1, 'MySuperChild', [ + SchemaProperty('_value', RealmPropertyType.string, mapTo: '_value'), + SchemaProperty('_parent', RealmPropertyType.object, + mapTo: '_parent', optional: true, linkTarget: 'Parent'), + SchemaProperty('indexedString', RealmPropertyType.string), + ]); + } +} + diff --git a/test/dynamic_realm_test.dart b/test/dynamic_realm_test.dart index e7ba708d7..4f1c21c4f 100644 --- a/test/dynamic_realm_test.dart +++ b/test/dynamic_realm_test.dart @@ -18,15 +18,12 @@ // ignore_for_file: avoid_relative_lib_imports -import 'dart:io'; import 'package:test/test.dart' hide test, throws; import '../lib/realm.dart'; import 'test.dart'; Future main([List? args]) async { - print("Current PID $pid"); - await setupTests(args); _assertSchemaExists(Realm realm, SchemaObject expected) { diff --git a/test/embedded_test.dart b/test/embedded_test.dart index 3b62ce71c..edc0a379e 100644 --- a/test/embedded_test.dart +++ b/test/embedded_test.dart @@ -42,7 +42,7 @@ Future main([List? args]) async { return getRealm(realmConfig); } - test('local Realm with orphan embedded schemas works', () { + test('Local Realm with orphan embedded schemas works', () { final config = Configuration.local([AllTypesEmbedded.schema]); final realm = getRealm(config); realm.close(); @@ -53,7 +53,7 @@ Future main([List? args]) async { expect(schema.single.baseType, ObjectType.embedded); }); - baasTest('synchronized Realm with orphan embedded schemas throws', (configuration) async { + baasTest('Synchronized Realm with orphan embedded schemas throws', (configuration) async { final app = App(configuration); final user = await getIntegrationUser(app); final config = Configuration.flexibleSync(user, [AllTypesEmbedded.schema]); diff --git a/test/migration_test.dart b/test/migration_test.dart index 2fcbcc724..1a405046a 100644 --- a/test/migration_test.dart +++ b/test/migration_test.dart @@ -19,7 +19,6 @@ // ignore_for_file: unused_local_variable import 'dart:async'; -import 'dart:io'; import 'package:test/test.dart' hide test, throws; import '../lib/realm.dart'; import 'test.dart'; @@ -70,8 +69,6 @@ class _MyObjectWithoutValue { } Future main([List? args]) async { - print("Current PID $pid"); - await setupTests(args); test('Configuration.migrationCallback executed when schema version changes', () { diff --git a/test/user_test.dart b/test/user_test.dart index aec15c39b..06fee6e93 100644 --- a/test/user_test.dart +++ b/test/user_test.dart @@ -16,16 +16,12 @@ // //////////////////////////////////////////////////////////////////////////////// -import 'dart:io'; - import 'package:test/expect.dart'; import '../lib/realm.dart'; import 'test.dart'; Future main([List? args]) async { - print("Current PID $pid"); - await setupTests(args); baasTest('User logout anon user is marked as removed', (configuration) async { From b8288d7a03cc628d4df066e2a73164d4b27802e1 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Thu, 20 Oct 2022 22:17:18 +0200 Subject: [PATCH 07/13] Remove skip --- test/embedded_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/embedded_test.dart b/test/embedded_test.dart index edc0a379e..9c50e9b17 100644 --- a/test/embedded_test.dart +++ b/test/embedded_test.dart @@ -254,7 +254,7 @@ Future main([List? args]) async { expect(obj.recursiveList.length, 2); expect(obj.recursiveList[0].value, '2'); expect(obj.recursiveList[1].value, '1'); - }, skip: 'Needs https://github.com/realm/realm-dart/pull/894'); + }); test('Embedded list .set', () { final realm = getLocalRealm(); From a0d3622115311046a2471dfa24b0f27facb1cd19 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Thu, 20 Oct 2022 23:30:29 +0200 Subject: [PATCH 08/13] Fix equals --- lib/src/realm_object.dart | 2 +- test/embedded_test.dart | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/src/realm_object.dart b/lib/src/realm_object.dart index ef13acaca..69bdb9af2 100644 --- a/lib/src/realm_object.dart +++ b/lib/src/realm_object.dart @@ -326,7 +326,7 @@ mixin RealmObjectBase on RealmEntity implements Finalizable { @override bool operator ==(Object other) { if (identical(this, other)) return true; - if (other is! RealmObject) return false; + if (other is! RealmObjectBase) return false; if (!isManaged || !other.isManaged) return false; return realmCore.objectEquals(this, other); } diff --git a/test/embedded_test.dart b/test/embedded_test.dart index 9c50e9b17..08a6440ad 100644 --- a/test/embedded_test.dart +++ b/test/embedded_test.dart @@ -751,6 +751,22 @@ Future main([List? args]) async { await subscription.cancel(); }); + + test('EmbeddedObject.equals returns expected results', () { + final realm = getLocalRealm(); + + final parent = ObjectWithEmbedded('123', recursiveObject: RecursiveEmbedded1('1.1'), recursiveList: [RecursiveEmbedded1('1.1')]); + realm.write(() { + realm.add(parent); + }); + + expect(parent.recursiveObject, parent.recursiveObject); + expect(identical(parent.recursiveObject, parent.recursiveObject), false); + + expect(parent.recursiveObject, isNot(RecursiveEmbedded1('1.1'))); + expect(parent.recursiveObject, isNot(parent.recursiveList[0])); + expect(parent.recursiveObject, isNot(parent)); + }); } extension on RealmObjectBase { From ed83f7376f61e0e4a4c24227e62650297c0cd8f7 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Fri, 21 Oct 2022 12:11:14 +0200 Subject: [PATCH 09/13] Address comments --- lib/src/realm_object.dart | 2 ++ lib/src/realm_property.dart | 5 ++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/src/realm_object.dart b/lib/src/realm_object.dart index 69bdb9af2..3f672b524 100644 --- a/lib/src/realm_object.dart +++ b/lib/src/realm_object.dart @@ -619,6 +619,8 @@ class DynamicRealmObject { _typeOf(): RealmPropertyType.uuid, RealmObject: RealmPropertyType.object, _typeOf(): RealmPropertyType.object, + EmbeddedObject: RealmPropertyType.object, + _typeOf(): RealmPropertyType.object, }; RealmPropertyType? _getPropertyType() => _propertyTypeMap[T]; diff --git a/lib/src/realm_property.dart b/lib/src/realm_property.dart index 54816ff7c..d3d90f265 100644 --- a/lib/src/realm_property.dart +++ b/lib/src/realm_property.dart @@ -38,11 +38,10 @@ class SchemaProperty { /// `true` if the property is optional final bool optional; - /// An alias to another property of the same RealmObject + /// An alias to another property of the same RealmObject/EmbeddedObject final String? mapTo; /// @nodoc const SchemaProperty(this.name, this.propertyType, - {this.optional = false, this.mapTo, this.primaryKey = false, this.linkTarget, this.collectionType = RealmCollectionType.none - }); + {this.optional = false, this.mapTo, this.primaryKey = false, this.linkTarget, this.collectionType = RealmCollectionType.none}); } From 8374f3646fa1d041df61092f1cc5f4fb756a38cc Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Fri, 21 Oct 2022 14:14:20 +0200 Subject: [PATCH 10/13] Final touches --- common/lib/src/realm_common_base.dart | 10 ++- example/bin/myapp.g.dart | 4 +- flutter/realm_flutter/example/lib/main.g.dart | 4 +- generator/lib/src/class_element_ex.dart | 4 +- .../embedded_object_primary_key.dart | 2 +- generator/test/good_test.dart | 2 +- .../test/good_test_data/all_types.expected | 6 +- .../good_test_data/embedded_annotations.dart | 2 +- .../embedded_annotations.expected | 2 +- .../test/good_test_data/embedded_objects.dart | 4 +- .../good_test_data/embedded_objects.expected | 2 +- .../list_initialization.expected | 2 +- generator/test/good_test_data/mapto.expected | 2 +- .../good_test_data/optional_argument.expected | 2 +- .../test/good_test_data/pinhole.expected | 2 +- .../test/good_test_data/primary_key.expected | 16 ++-- .../good_test_data/required_argument.expected | 2 +- ...uired_argument_with_default_value.expected | 2 +- .../user_defined_getter.expected | 2 +- lib/src/list.dart | 4 +- lib/src/native/realm_core.dart | 6 +- lib/src/realm_class.dart | 2 +- lib/src/realm_object.dart | 8 +- lib/src/realm_property.dart | 8 +- test/embedded_test.dart | 84 ++++++++++++++----- test/migration_test.g.dart | 10 +-- test/realm_object_test.g.dart | 26 +++--- test/test.dart | 12 +-- test/test.g.dart | 79 ++++++++--------- 29 files changed, 179 insertions(+), 132 deletions(-) diff --git a/common/lib/src/realm_common_base.dart b/common/lib/src/realm_common_base.dart index dc80eea23..23ecbf52e 100644 --- a/common/lib/src/realm_common_base.dart +++ b/common/lib/src/realm_common_base.dart @@ -22,16 +22,16 @@ enum ObjectType { /// A standalone top-level object that can be persisted in Realm. It can link /// to other objects or collections of other objects. - topLevel('RealmObject', 0), + realmObject('RealmObject', 0), /// An object that can be embedded in other objects. It is considered owned /// by its parent and will be deleted if its parent is deleted. - embedded('EmbeddedObject', 1), + embeddedObject('EmbeddedObject', 1), /// A special type of object used to facilitate unidirectional synchronization /// with Atlas App Services. It is used to push data to Realm without the ability /// to query or modify it. - _asymmetric('AsymmetricObject', 2); + _asymmetricObject('AsymmetricObject', 2); const ObjectType([this._className = 'Unknown', this._flags = -1]); @@ -50,9 +50,11 @@ extension ObjectTypeInternal on ObjectType { /// /// {@category Annotations} class RealmModel { + /// The base type of the object final ObjectType type; - const RealmModel([this.type = ObjectType.topLevel]); + /// Creates a new instance of [RealmModel] specifying the desired base type. + const RealmModel([this.type = ObjectType.realmObject]); } /// MapTo annotation for class member level. diff --git a/example/bin/myapp.g.dart b/example/bin/myapp.g.dart index 6e347cb07..6ea6cfb71 100644 --- a/example/bin/myapp.g.dart +++ b/example/bin/myapp.g.dart @@ -60,7 +60,7 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Car._); - return const SchemaObject(ObjectType.topLevel, Car, 'Car', [ + return const SchemaObject(ObjectType.realmObject, Car, 'Car', [ SchemaProperty('make', RealmPropertyType.string), SchemaProperty('model', RealmPropertyType.string, optional: true), SchemaProperty('kilometers', RealmPropertyType.int, optional: true), @@ -109,7 +109,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Person._); - return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('age', RealmPropertyType.int), ]); diff --git a/flutter/realm_flutter/example/lib/main.g.dart b/flutter/realm_flutter/example/lib/main.g.dart index 0dc5e98f0..f7627287f 100644 --- a/flutter/realm_flutter/example/lib/main.g.dart +++ b/flutter/realm_flutter/example/lib/main.g.dart @@ -60,7 +60,7 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Car._); - return const SchemaObject(ObjectType.topLevel, Car, 'Car', [ + return const SchemaObject(ObjectType.realmObject, Car, 'Car', [ SchemaProperty('make', RealmPropertyType.string), SchemaProperty('model', RealmPropertyType.string, optional: true), SchemaProperty('kilometers', RealmPropertyType.int, optional: true), @@ -109,7 +109,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Person._); - return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('age', RealmPropertyType.int), ]); diff --git a/generator/lib/src/class_element_ex.dart b/generator/lib/src/class_element_ex.dart index 1323879de..3f3f7d261 100644 --- a/generator/lib/src/class_element_ex.dart +++ b/generator/lib/src/class_element_ex.dart @@ -151,14 +151,14 @@ extension ClassElementEx on ClassElement { final mappedFields = fields.realmInfo.toList(); - if (objectType == ObjectType.embedded && mappedFields.any((field) => field.isPrimaryKey)) { + if (objectType == ObjectType.embeddedObject && mappedFields.any((field) => field.isPrimaryKey)) { final pkSpan = fields.firstWhere((field) => field.realmInfo?.isPrimaryKey == true).span; throw RealmInvalidGenerationSourceError("Primary key not allowed on embedded objects", element: this, primarySpan: pkSpan, secondarySpans: {span!: ''}, primaryLabel: "$realmName is marked as embedded but has primary key defined", - todo: 'Remove the @PrimaryKey annotation from the field or set the model type to a value different from ObjectType.embedded.'); + todo: 'Remove the @PrimaryKey annotation from the field or set the model type to a value different from ObjectType.embeddedObject.'); } return RealmModelInfo(name, modelName, realmName, mappedFields, objectType); diff --git a/generator/test/error_test_data/embedded_object_primary_key.dart b/generator/test/error_test_data/embedded_object_primary_key.dart index f0b5f6183..4998ceb28 100644 --- a/generator/test/error_test_data/embedded_object_primary_key.dart +++ b/generator/test/error_test_data/embedded_object_primary_key.dart @@ -2,7 +2,7 @@ import 'package:realm_common/realm_common.dart'; //part 'embedded_object_primary_key.g.dart'; -@RealmModel(ObjectType.embedded) +@RealmModel(ObjectType.embeddedObject) class _Bad { @PrimaryKey() late int id; diff --git a/generator/test/good_test.dart b/generator/test/good_test.dart index fbbfc6618..bc0e3a6f9 100644 --- a/generator/test/good_test.dart +++ b/generator/test/good_test.dart @@ -75,7 +75,7 @@ class MappedToo extends _MappedToo static SchemaObject _initSchema() { RealmObjectBase.registerFactory(MappedToo._); return const SchemaObject( - ObjectType.topLevel, MappedToo, 'this is also mapped', [ + ObjectType.realmObject, MappedToo, 'this is also mapped', [ SchemaProperty('singleLink', RealmPropertyType.object, optional: true, linkTarget: 'another type'), SchemaProperty('listLink', RealmPropertyType.object, diff --git a/generator/test/good_test_data/all_types.expected b/generator/test/good_test_data/all_types.expected index 49fc2eff1..e06c1a152 100644 --- a/generator/test/good_test_data/all_types.expected +++ b/generator/test/good_test_data/all_types.expected @@ -34,7 +34,7 @@ class Foo extends _Foo with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Foo._); - return const SchemaObject(ObjectType.topLevel, Foo, 'MyFoo', [ + return const SchemaObject(ObjectType.realmObject, Foo, 'MyFoo', [ SchemaProperty('x', RealmPropertyType.int), ]); } @@ -144,7 +144,7 @@ class Bar extends _Bar with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Bar._); - return const SchemaObject(ObjectType.topLevel, Bar, 'Bar', [ + return const SchemaObject(ObjectType.realmObject, Bar, 'Bar', [ SchemaProperty('id', RealmPropertyType.string, primaryKey: true), SchemaProperty('aBool', RealmPropertyType.bool), SchemaProperty('another', RealmPropertyType.bool), @@ -225,7 +225,7 @@ class PrimitiveTypes extends _PrimitiveTypes static SchemaObject _initSchema() { RealmObjectBase.registerFactory(PrimitiveTypes._); return const SchemaObject( - ObjectType.topLevel, PrimitiveTypes, 'PrimitiveTypes', [ + ObjectType.realmObject, PrimitiveTypes, 'PrimitiveTypes', [ SchemaProperty('stringProp', RealmPropertyType.string), SchemaProperty('boolProp', RealmPropertyType.bool), SchemaProperty('dateProp', RealmPropertyType.timestamp), diff --git a/generator/test/good_test_data/embedded_annotations.dart b/generator/test/good_test_data/embedded_annotations.dart index 456b42fdf..e0923ec95 100644 --- a/generator/test/good_test_data/embedded_annotations.dart +++ b/generator/test/good_test_data/embedded_annotations.dart @@ -9,7 +9,7 @@ class _Parent { late List<_Child1> children; } -@RealmModel(ObjectType.embedded) +@RealmModel(ObjectType.embeddedObject) @MapTo('MySuperChild') class _Child1 { @MapTo('_value') diff --git a/generator/test/good_test_data/embedded_annotations.expected b/generator/test/good_test_data/embedded_annotations.expected index 361820d1c..987c2f015 100644 --- a/generator/test/good_test_data/embedded_annotations.expected +++ b/generator/test/good_test_data/embedded_annotations.expected @@ -39,7 +39,7 @@ class Parent extends _Parent with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Parent._); - return const SchemaObject(ObjectType.topLevel, Parent, 'Parent', [ + return const SchemaObject(ObjectType.realmObject, Parent, 'Parent', [ SchemaProperty('single child', RealmPropertyType.object, mapTo: 'single child', optional: true, linkTarget: 'MySuperChild'), SchemaProperty('CHILDREN', RealmPropertyType.object, diff --git a/generator/test/good_test_data/embedded_objects.dart b/generator/test/good_test_data/embedded_objects.dart index 169d5dd0d..b2b212b27 100644 --- a/generator/test/good_test_data/embedded_objects.dart +++ b/generator/test/good_test_data/embedded_objects.dart @@ -7,7 +7,7 @@ class _Parent { late List<_Child1> children; } -@RealmModel(ObjectType.embedded) +@RealmModel(ObjectType.embeddedObject) class _Child1 { late String value; @@ -18,7 +18,7 @@ class _Child1 { late _Parent? linkToParent; } -@RealmModel(ObjectType.embedded) +@RealmModel(ObjectType.embeddedObject) class _Child2 { late bool boolProp; late int intProp; diff --git a/generator/test/good_test_data/embedded_objects.expected b/generator/test/good_test_data/embedded_objects.expected index ba0f92bad..4911f539b 100644 --- a/generator/test/good_test_data/embedded_objects.expected +++ b/generator/test/good_test_data/embedded_objects.expected @@ -38,7 +38,7 @@ class Parent extends _Parent with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Parent._); - return const SchemaObject(ObjectType.topLevel, Parent, 'Parent', [ + return const SchemaObject(ObjectType.realmObject, Parent, 'Parent', [ SchemaProperty('child', RealmPropertyType.object, optional: true, linkTarget: 'Child1'), SchemaProperty('children', RealmPropertyType.object, diff --git a/generator/test/good_test_data/list_initialization.expected b/generator/test/good_test_data/list_initialization.expected index 8f7ac45f8..5fcfb33b7 100644 --- a/generator/test/good_test_data/list_initialization.expected +++ b/generator/test/good_test_data/list_initialization.expected @@ -30,7 +30,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Person._); - return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('children', RealmPropertyType.object, linkTarget: 'Person', collectionType: RealmCollectionType.list), ]); diff --git a/generator/test/good_test_data/mapto.expected b/generator/test/good_test_data/mapto.expected index 05b5fb468..5bb225ef2 100644 --- a/generator/test/good_test_data/mapto.expected +++ b/generator/test/good_test_data/mapto.expected @@ -57,7 +57,7 @@ class Original extends $Original static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Original._); - return const SchemaObject(ObjectType.topLevel, Original, 'another type', [ + return const SchemaObject(ObjectType.realmObject, Original, 'another type', [ SchemaProperty('remapped primitive', RealmPropertyType.int, mapTo: 'remapped primitive'), SchemaProperty('remapped object', RealmPropertyType.object, diff --git a/generator/test/good_test_data/optional_argument.expected b/generator/test/good_test_data/optional_argument.expected index 33dab79ad..fb7565b4f 100644 --- a/generator/test/good_test_data/optional_argument.expected +++ b/generator/test/good_test_data/optional_argument.expected @@ -28,7 +28,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Person._); - return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('spouse', RealmPropertyType.object, optional: true, linkTarget: 'Person'), ]); diff --git a/generator/test/good_test_data/pinhole.expected b/generator/test/good_test_data/pinhole.expected index c071d93cf..fcc868bcb 100644 --- a/generator/test/good_test_data/pinhole.expected +++ b/generator/test/good_test_data/pinhole.expected @@ -34,7 +34,7 @@ class Foo extends _Foo with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Foo._); - return const SchemaObject(ObjectType.topLevel, Foo, 'Foo', [ + return const SchemaObject(ObjectType.realmObject, Foo, 'Foo', [ SchemaProperty('x', RealmPropertyType.int), ]); } diff --git a/generator/test/good_test_data/primary_key.expected b/generator/test/good_test_data/primary_key.expected index 136e71258..fc18b7497 100644 --- a/generator/test/good_test_data/primary_key.expected +++ b/generator/test/good_test_data/primary_key.expected @@ -27,7 +27,7 @@ class IntPK extends _IntPK with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(IntPK._); - return const SchemaObject(ObjectType.topLevel, IntPK, 'IntPK', [ + return const SchemaObject(ObjectType.realmObject, IntPK, 'IntPK', [ SchemaProperty('id', RealmPropertyType.int, primaryKey: true), ]); } @@ -60,7 +60,7 @@ class NullableIntPK extends _NullableIntPK static SchemaObject _initSchema() { RealmObjectBase.registerFactory(NullableIntPK._); return const SchemaObject( - ObjectType.topLevel, NullableIntPK, 'NullableIntPK', [ + ObjectType.realmObject, NullableIntPK, 'NullableIntPK', [ SchemaProperty('id', RealmPropertyType.int, optional: true, primaryKey: true), ]); @@ -93,7 +93,7 @@ class StringPK extends _StringPK static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(StringPK._); - return const SchemaObject(ObjectType.topLevel, StringPK, 'StringPK', [ + return const SchemaObject(ObjectType.realmObject, StringPK, 'StringPK', [ SchemaProperty('id', RealmPropertyType.string, primaryKey: true), ]); } @@ -127,7 +127,7 @@ class NullableStringPK extends _NullableStringPK static SchemaObject _initSchema() { RealmObjectBase.registerFactory(NullableStringPK._); return const SchemaObject( - ObjectType.topLevel, NullableStringPK, 'NullableStringPK', [ + ObjectType.realmObject, NullableStringPK, 'NullableStringPK', [ SchemaProperty('id', RealmPropertyType.string, optional: true, primaryKey: true), ]); @@ -160,7 +160,7 @@ class ObjectIdPK extends _ObjectIdPK static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(ObjectIdPK._); - return const SchemaObject(ObjectType.topLevel, ObjectIdPK, 'ObjectIdPK', [ + return const SchemaObject(ObjectType.realmObject, ObjectIdPK, 'ObjectIdPK', [ SchemaProperty('id', RealmPropertyType.objectid, primaryKey: true), ]); } @@ -194,7 +194,7 @@ class NullableObjectIdPK extends _NullableObjectIdPK static SchemaObject _initSchema() { RealmObjectBase.registerFactory(NullableObjectIdPK._); return const SchemaObject( - ObjectType.topLevel, NullableObjectIdPK, 'NullableObjectIdPK', [ + ObjectType.realmObject, NullableObjectIdPK, 'NullableObjectIdPK', [ SchemaProperty('id', RealmPropertyType.objectid, optional: true, primaryKey: true), ]); @@ -226,7 +226,7 @@ class UuidPK extends _UuidPK with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(UuidPK._); - return const SchemaObject(ObjectType.topLevel, UuidPK, 'UuidPK', [ + return const SchemaObject(ObjectType.realmObject, UuidPK, 'UuidPK', [ SchemaProperty('id', RealmPropertyType.uuid, primaryKey: true), ]); } @@ -259,7 +259,7 @@ class NullableUuidPK extends _NullableUuidPK static SchemaObject _initSchema() { RealmObjectBase.registerFactory(NullableUuidPK._); return const SchemaObject( - ObjectType.topLevel, NullableUuidPK, 'NullableUuidPK', [ + ObjectType.realmObject, NullableUuidPK, 'NullableUuidPK', [ SchemaProperty('id', RealmPropertyType.uuid, optional: true, primaryKey: true), ]); diff --git a/generator/test/good_test_data/required_argument.expected b/generator/test/good_test_data/required_argument.expected index d58318e17..ba7db413e 100644 --- a/generator/test/good_test_data/required_argument.expected +++ b/generator/test/good_test_data/required_argument.expected @@ -27,7 +27,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Person._); - return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), ]); } diff --git a/generator/test/good_test_data/required_argument_with_default_value.expected b/generator/test/good_test_data/required_argument_with_default_value.expected index 0c3619182..5ea8348de 100644 --- a/generator/test/good_test_data/required_argument_with_default_value.expected +++ b/generator/test/good_test_data/required_argument_with_default_value.expected @@ -34,7 +34,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Person._); - return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('age', RealmPropertyType.int), ]); } diff --git a/generator/test/good_test_data/user_defined_getter.expected b/generator/test/good_test_data/user_defined_getter.expected index 569d9993f..b9506bae9 100644 --- a/generator/test/good_test_data/user_defined_getter.expected +++ b/generator/test/good_test_data/user_defined_getter.expected @@ -27,7 +27,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Person._); - return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string), ]); } diff --git a/lib/src/list.dart b/lib/src/list.dart index c000ce232..2ecb53176 100644 --- a/lib/src/list.dart +++ b/lib/src/list.dart @@ -145,7 +145,7 @@ class ManagedRealmList with RealmEntity, ListMixin impleme @override int indexOf(covariant T element, [int start = 0]) { - if (element is RealmObject && !element.isManaged) { + if (element is RealmObjectBase && !element.isManaged) { throw RealmStateError('Cannot call indexOf on a managed list with an element that is an unmanaged object'); } if (start < 0) start = 0; @@ -189,7 +189,7 @@ class UnmanagedRealmList extends collection.DelegatingList extension RealmListOfObject on RealmList { /// Filters the list and returns a new [RealmResults] according to the provided [query] (with optional [arguments]). /// - /// Only works for lists of [RealmObject]s. + /// Only works for lists of [RealmObject]s or [EmbeddedObject]s. /// /// The Realm Dart and Realm Flutter SDKs supports querying based on a language inspired by [NSPredicate](https://academy.realm.io/posts/nspredicate-cheatsheet/) /// and [Predicate Programming Guide.](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Predicates/AdditionalChapters/Introduction.html#//apple_ref/doc/uid/TP40001789) diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index e9fb13ed7..dcc28fdf2 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -560,10 +560,10 @@ class _RealmCore { late Type type; switch (baseType) { - case ObjectType.topLevel: + case ObjectType.realmObject: type = RealmObject; break; - case ObjectType.embedded: + case ObjectType.embeddedObject: type = EmbeddedObject; break; default: @@ -793,7 +793,7 @@ class _RealmCore { }); } - void deleteRealmObject(RealmObject object) { + void deleteRealmObject(RealmObjectBase object) { _realmLib.invokeGetBool(() => _realmLib.realm_object_delete(object.handle._pointer)); } diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index 86b68be7e..4f30e9942 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -254,7 +254,7 @@ class Realm implements Finalizable { } /// Deletes a [RealmObject] from this `Realm`. - void delete(T object) { + void delete(T object) { if (!object.isManaged) { throw RealmError('Cannot delete an unmanaged object'); } diff --git a/lib/src/realm_object.dart b/lib/src/realm_object.dart index 3f672b524..735bb6904 100644 --- a/lib/src/realm_object.dart +++ b/lib/src/realm_object.dart @@ -150,9 +150,9 @@ class RealmCoreAccessor implements RealmAccessor { // RealmObjects since we don't know the type of the object. if (listMetadata != null && _isTypeGenericObject()) { switch (listMetadata.schema.baseType) { - case ObjectType.topLevel: + case ObjectType.realmObject: return object.realm.createList(handle, listMetadata); - case ObjectType.embedded: + case ObjectType.embeddedObject: return object.realm.createList(handle, listMetadata); default: throw RealmError('List of ${listMetadata.schema.baseType} is not supported yet'); @@ -284,9 +284,9 @@ mixin RealmObjectBase on RealmEntity implements Finalizable { if (factory == null) { if (type == RealmObjectBase) { switch (metadata.schema.baseType) { - case ObjectType.topLevel: + case ObjectType.realmObject: return _ConcreteRealmObject(); - case ObjectType.embedded: + case ObjectType.embeddedObject: return _ConcreteEmbeddedObject(); default: throw RealmException("ObjectType ${metadata.schema.baseType} not supported"); diff --git a/lib/src/realm_property.dart b/lib/src/realm_property.dart index d3d90f265..b8fd60198 100644 --- a/lib/src/realm_property.dart +++ b/lib/src/realm_property.dart @@ -18,7 +18,7 @@ import 'package:realm_common/realm_common.dart'; -/// Describes a [RealmObject]'s property with its name, type and other attributes in the [RealmSchema] +/// Describes a `RealmObject`/`EmbeddedObject`'s property with its name, type and other attributes in the [RealmSchema] ///{@category Configuration} class SchemaProperty { /// The name of the property as persisted in the `Realm` @@ -26,10 +26,10 @@ class SchemaProperty { final String? linkTarget; - /// Defines the `Realm` collection type if this property is a collection. + /// Defines the `Realm` collection type if this property is a collection final RealmCollectionType collectionType; - /// `true` if the property is a primary key. + /// `true` if the property is a primary key final bool primaryKey; /// The `Realm` type of the property @@ -38,7 +38,7 @@ class SchemaProperty { /// `true` if the property is optional final bool optional; - /// An alias to another property of the same RealmObject/EmbeddedObject + /// Indicates that the property should be persisted under a different name final String? mapTo; /// @nodoc diff --git a/test/embedded_test.dart b/test/embedded_test.dart index 08a6440ad..a86f4db2a 100644 --- a/test/embedded_test.dart +++ b/test/embedded_test.dart @@ -50,7 +50,7 @@ Future main([List? args]) async { final dynamicRealm = getRealm(Configuration.local([], path: config.path)); final schema = dynamicRealm.schema; - expect(schema.single.baseType, ObjectType.embedded); + expect(schema.single.baseType, ObjectType.embeddedObject); }); baasTest('Synchronized Realm with orphan embedded schemas throws', (configuration) async { @@ -98,12 +98,12 @@ Future main([List? args]) async { final graph = ObjectWithEmbedded('TopLevel1', recursiveObject: RecursiveEmbedded1('Child1', child: RecursiveEmbedded2('Child2'), children: [ - RecursiveEmbedded2('List1', child: RecursiveEmbedded3('Child3'), topLevel: ObjectWithEmbedded('TopLeve2')), + RecursiveEmbedded2('List1', child: RecursiveEmbedded3('Child3'), realmObject: ObjectWithEmbedded('TopLeve2')), RecursiveEmbedded2('List2'), ])); // Make a cycle - graph.recursiveObject!.child!.topLevel = graph; + graph.recursiveObject!.child!.realmObject = graph; realm.write(() { realm.add(graph); }); @@ -120,13 +120,13 @@ Future main([List? args]) async { expect(child1, isNotNull); expect(child1!.isManaged, true); expect(child1.value, 'Child1'); - expect(child1.topLevel, isNull); + expect(child1.realmObject, isNull); final child2 = child1.child; expect(child2, isNotNull); expect(child2!.isManaged, true); expect(child2.value, 'Child2'); - expect(child2.topLevel, refetched); + expect(child2.realmObject, refetched); expect(child2.child, isNull); expect(child2.children, isEmpty); @@ -134,7 +134,7 @@ Future main([List? args]) async { expect(listChild1.isManaged, true); expect(listChild1.value, 'List1'); expect(listChild1.child!.value, 'Child3'); - expect(listChild1.topLevel!.id, 'TopLeve2'); + expect(listChild1.realmObject!.id, 'TopLeve2'); final listChild2 = child1.children[1]; expect(listChild2.isManaged, true); @@ -144,44 +144,44 @@ Future main([List? args]) async { test('Embedded object when replaced invalidates old object', () { final realm = getLocalRealm(); - final topLevel = realm.write(() { + final realmObject = realm.write(() { return realm.add(ObjectWithEmbedded('', recursiveObject: RecursiveEmbedded1('first'))); }); - final firstEmbeded = topLevel.recursiveObject!; + final firstEmbeded = realmObject.recursiveObject!; expect(firstEmbeded.isManaged, true); expect(firstEmbeded.isValid, true); expect(firstEmbeded.value, 'first'); realm.write(() { - topLevel.recursiveObject = RecursiveEmbedded1('second'); + realmObject.recursiveObject = RecursiveEmbedded1('second'); }); // We replaced firstEmbedded with another one, so we expect it to be invalid expect(firstEmbeded.isManaged, true); expect(firstEmbeded.isValid, false); - final secondEmbedded = topLevel.recursiveObject!; + final secondEmbedded = realmObject.recursiveObject!; expect(secondEmbedded.value, 'second'); realm.write(() { - topLevel.recursiveObject = null; + realmObject.recursiveObject = null; }); // We replaced secondEmbedded with null, so we expect it to be invalid expect(secondEmbedded.isManaged, true); expect(secondEmbedded.isValid, false); - expect(topLevel.recursiveObject, isNull); + expect(realmObject.recursiveObject, isNull); }); test('Embedded object in list when replaced invalidates old object', () { final realm = getLocalRealm(); - final topLevel = realm.write(() { + final realmObject = realm.write(() { return realm.add(ObjectWithEmbedded('', recursiveList: [RecursiveEmbedded1('first'), RecursiveEmbedded1('second'), RecursiveEmbedded1('third')])); }); - final list = topLevel.recursiveList; + final list = realmObject.recursiveList; final first = list[0]; @@ -349,7 +349,7 @@ Future main([List? args]) async { expect(obj.isValid, true); final newGraph = ObjectWithEmbedded('456', - recursiveObject: RecursiveEmbedded1('embedded 456', topLevel: ObjectWithEmbedded('123', recursiveList: [RecursiveEmbedded1('value')]))); + recursiveObject: RecursiveEmbedded1('embedded 456', realmObject: ObjectWithEmbedded('123', recursiveList: [RecursiveEmbedded1('value')]))); realm.write(() { realm.add(newGraph, update: true); @@ -472,29 +472,29 @@ Future main([List? args]) async { final child11 = parent.dynamic.get('recursiveObject')!; expect(child11.dynamic.get('value'), '1.1'); expect(child11.instanceSchema.name, 'RecursiveEmbedded1'); - expect(child11.instanceSchema.baseType, ObjectType.embedded); + expect(child11.instanceSchema.baseType, ObjectType.embeddedObject); final list1 = parent.dynamic.getList('recursiveList'); expect(list1.length, 1); expect(list1[0].dynamic.get('value'), '1.2'); expect(list1[0].instanceSchema.name, 'RecursiveEmbedded1'); - expect(list1[0].instanceSchema.baseType, ObjectType.embedded); + expect(list1[0].instanceSchema.baseType, ObjectType.embeddedObject); final child21 = child11.dynamic.get('child')!; expect(child21.dynamic.get('value'), '2.1'); expect(child21.instanceSchema.name, 'RecursiveEmbedded2'); - expect(child21.instanceSchema.baseType, ObjectType.embedded); + expect(child21.instanceSchema.baseType, ObjectType.embeddedObject); final list2 = child11.dynamic.getList('children'); expect(list2.length, 1); expect(list2[0].dynamic.get('value'), '2.2'); expect(list2[0].instanceSchema.name, 'RecursiveEmbedded2'); - expect(list2[0].instanceSchema.baseType, ObjectType.embedded); + expect(list2[0].instanceSchema.baseType, ObjectType.embeddedObject); final child31 = child21.dynamic.get('child')!; expect(child31.dynamic.get('value'), '3.1'); expect(child31.instanceSchema.name, 'RecursiveEmbedded3'); - expect(child31.instanceSchema.baseType, ObjectType.embedded); + expect(child31.instanceSchema.baseType, ObjectType.embeddedObject); // String API without casting final genericChild11 = parent.dynamic.get('recursiveObject'); @@ -767,6 +767,50 @@ Future main([List? args]) async { expect(parent.recursiveObject, isNot(parent.recursiveList[0])); expect(parent.recursiveObject, isNot(parent)); }); + + test('Realm.delete deletes embedded object', () { + final realm = getLocalRealm(); + + final parent = ObjectWithEmbedded('123', recursiveObject: RecursiveEmbedded1('1'), recursiveList: [RecursiveEmbedded1('2')]); + realm.write(() { + realm.add(parent); + }); + + final allEmbedded = realm.allEmbedded(); + expect(allEmbedded.length, 2); + + realm.write(() { + realm.delete(parent.recursiveList[0]); + }); + + expect(parent.recursiveList, isEmpty); + expect(allEmbedded.length, 1); + expect(allEmbedded.single.value, '1'); + + realm.write(() { + realm.delete(parent.recursiveObject!); + }); + + expect(parent.recursiveObject, isNull); + expect(allEmbedded, isEmpty); + }); + + test('List.clear deletes all embedded objects', () { + final realm = getLocalRealm(); + + final parent = ObjectWithEmbedded('123', recursiveList: [RecursiveEmbedded1('1'), RecursiveEmbedded1('2')]); + + realm.write(() => realm.add(parent)); + + final allEmbedded = realm.allEmbedded(); + expect(allEmbedded.length, 2); + + realm.write(() { + parent.recursiveList.clear(); + }); + + expect(parent.recursiveList, isEmpty); + }); } extension on RealmObjectBase { diff --git a/test/migration_test.g.dart b/test/migration_test.g.dart index 5bc2b65ac..7cb6db9e9 100644 --- a/test/migration_test.g.dart +++ b/test/migration_test.g.dart @@ -32,7 +32,7 @@ class PersonIntName extends _PersonIntName static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(PersonIntName._); - return const SchemaObject(ObjectType.topLevel, PersonIntName, 'Person', [ + return const SchemaObject(ObjectType.realmObject, PersonIntName, 'Person', [ SchemaProperty('name', RealmPropertyType.int), ]); } @@ -72,7 +72,7 @@ class StudentV1 extends _StudentV1 static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(StudentV1._); - return const SchemaObject(ObjectType.topLevel, StudentV1, 'Student', [ + return const SchemaObject(ObjectType.realmObject, StudentV1, 'Student', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('yearOfBirth', RealmPropertyType.int, optional: true), ]); @@ -114,7 +114,7 @@ class MyObjectWithTypo extends _MyObjectWithTypo static SchemaObject _initSchema() { RealmObjectBase.registerFactory(MyObjectWithTypo._); return const SchemaObject( - ObjectType.topLevel, MyObjectWithTypo, 'MyObject', [ + ObjectType.realmObject, MyObjectWithTypo, 'MyObject', [ SchemaProperty('nmae', RealmPropertyType.string), SchemaProperty('vlaue', RealmPropertyType.int), ]); @@ -156,7 +156,7 @@ class MyObjectWithoutTypo extends _MyObjectWithoutTypo static SchemaObject _initSchema() { RealmObjectBase.registerFactory(MyObjectWithoutTypo._); return const SchemaObject( - ObjectType.topLevel, MyObjectWithoutTypo, 'MyObject', [ + ObjectType.realmObject, MyObjectWithoutTypo, 'MyObject', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('value', RealmPropertyType.int), ]); @@ -191,7 +191,7 @@ class MyObjectWithoutValue extends _MyObjectWithoutValue static SchemaObject _initSchema() { RealmObjectBase.registerFactory(MyObjectWithoutValue._); return const SchemaObject( - ObjectType.topLevel, MyObjectWithoutValue, 'MyObject', [ + ObjectType.realmObject, MyObjectWithoutValue, 'MyObject', [ SchemaProperty('name', RealmPropertyType.string), ]); } diff --git a/test/realm_object_test.g.dart b/test/realm_object_test.g.dart index 8451a143e..a1b2b1531 100644 --- a/test/realm_object_test.g.dart +++ b/test/realm_object_test.g.dart @@ -34,7 +34,7 @@ class ObjectIdPrimaryKey extends _ObjectIdPrimaryKey static SchemaObject _initSchema() { RealmObjectBase.registerFactory(ObjectIdPrimaryKey._); return const SchemaObject( - ObjectType.topLevel, ObjectIdPrimaryKey, 'ObjectIdPrimaryKey', [ + ObjectType.realmObject, ObjectIdPrimaryKey, 'ObjectIdPrimaryKey', [ SchemaProperty('id', RealmPropertyType.objectid, primaryKey: true), ]); } @@ -67,8 +67,8 @@ class NullableObjectIdPrimaryKey extends _NullableObjectIdPrimaryKey static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(NullableObjectIdPrimaryKey._); - return const SchemaObject(ObjectType.topLevel, NullableObjectIdPrimaryKey, - 'NullableObjectIdPrimaryKey', [ + return const SchemaObject(ObjectType.realmObject, + NullableObjectIdPrimaryKey, 'NullableObjectIdPrimaryKey', [ SchemaProperty('id', RealmPropertyType.objectid, optional: true, primaryKey: true), ]); @@ -102,7 +102,7 @@ class IntPrimaryKey extends _IntPrimaryKey static SchemaObject _initSchema() { RealmObjectBase.registerFactory(IntPrimaryKey._); return const SchemaObject( - ObjectType.topLevel, IntPrimaryKey, 'IntPrimaryKey', [ + ObjectType.realmObject, IntPrimaryKey, 'IntPrimaryKey', [ SchemaProperty('id', RealmPropertyType.int, primaryKey: true), ]); } @@ -135,8 +135,8 @@ class NullableIntPrimaryKey extends _NullableIntPrimaryKey static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(NullableIntPrimaryKey._); - return const SchemaObject( - ObjectType.topLevel, NullableIntPrimaryKey, 'NullableIntPrimaryKey', [ + return const SchemaObject(ObjectType.realmObject, NullableIntPrimaryKey, + 'NullableIntPrimaryKey', [ SchemaProperty('id', RealmPropertyType.int, optional: true, primaryKey: true), ]); @@ -171,7 +171,7 @@ class StringPrimaryKey extends _StringPrimaryKey static SchemaObject _initSchema() { RealmObjectBase.registerFactory(StringPrimaryKey._); return const SchemaObject( - ObjectType.topLevel, StringPrimaryKey, 'StringPrimaryKey', [ + ObjectType.realmObject, StringPrimaryKey, 'StringPrimaryKey', [ SchemaProperty('id', RealmPropertyType.string, primaryKey: true), ]); } @@ -204,7 +204,7 @@ class NullableStringPrimaryKey extends _NullableStringPrimaryKey static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(NullableStringPrimaryKey._); - return const SchemaObject(ObjectType.topLevel, NullableStringPrimaryKey, + return const SchemaObject(ObjectType.realmObject, NullableStringPrimaryKey, 'NullableStringPrimaryKey', [ SchemaProperty('id', RealmPropertyType.string, optional: true, primaryKey: true), @@ -239,7 +239,7 @@ class UuidPrimaryKey extends _UuidPrimaryKey static SchemaObject _initSchema() { RealmObjectBase.registerFactory(UuidPrimaryKey._); return const SchemaObject( - ObjectType.topLevel, UuidPrimaryKey, 'UuidPrimaryKey', [ + ObjectType.realmObject, UuidPrimaryKey, 'UuidPrimaryKey', [ SchemaProperty('id', RealmPropertyType.uuid, primaryKey: true), ]); } @@ -272,8 +272,8 @@ class NullableUuidPrimaryKey extends _NullableUuidPrimaryKey static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(NullableUuidPrimaryKey._); - return const SchemaObject( - ObjectType.topLevel, NullableUuidPrimaryKey, 'NullableUuidPrimaryKey', [ + return const SchemaObject(ObjectType.realmObject, NullableUuidPrimaryKey, + 'NullableUuidPrimaryKey', [ SchemaProperty('id', RealmPropertyType.uuid, optional: true, primaryKey: true), ]); @@ -311,7 +311,7 @@ class RemappedFromAnotherFile extends _RemappedFromAnotherFile static SchemaObject _initSchema() { RealmObjectBase.registerFactory(RemappedFromAnotherFile._); return const SchemaObject( - ObjectType.topLevel, RemappedFromAnotherFile, 'class with spaces', [ + ObjectType.realmObject, RemappedFromAnotherFile, 'class with spaces', [ SchemaProperty('property with spaces', RealmPropertyType.object, mapTo: 'property with spaces', optional: true, @@ -353,7 +353,7 @@ class BoolValue extends _BoolValue static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(BoolValue._); - return const SchemaObject(ObjectType.topLevel, BoolValue, 'BoolValue', [ + return const SchemaObject(ObjectType.realmObject, BoolValue, 'BoolValue', [ SchemaProperty('key', RealmPropertyType.int, primaryKey: true), SchemaProperty('value', RealmPropertyType.bool), ]); diff --git a/test/test.dart b/test/test.dart index 47da6ae3d..0c8247149 100644 --- a/test/test.dart +++ b/test/test.dart @@ -230,7 +230,7 @@ class _Game { int get rounds => winnerByRound.length; } -@RealmModel(ObjectType.embedded) +@RealmModel(ObjectType.embeddedObject) class _AllTypesEmbedded { late String stringProp; late bool boolProp; @@ -274,27 +274,27 @@ class _ObjectWithEmbedded { late List<_RecursiveEmbedded1> recursiveList; } -@RealmModel(ObjectType.embedded) +@RealmModel(ObjectType.embeddedObject) class _RecursiveEmbedded1 { late String value; late _RecursiveEmbedded2? child; late List<_RecursiveEmbedded2> children; - late _ObjectWithEmbedded? topLevel; + late _ObjectWithEmbedded? realmObject; } -@RealmModel(ObjectType.embedded) +@RealmModel(ObjectType.embeddedObject) class _RecursiveEmbedded2 { late String value; late _RecursiveEmbedded3? child; late List<_RecursiveEmbedded3> children; - late _ObjectWithEmbedded? topLevel; + late _ObjectWithEmbedded? realmObject; } -@RealmModel(ObjectType.embedded) +@RealmModel(ObjectType.embeddedObject) class _RecursiveEmbedded3 { late String value; } diff --git a/test/test.g.dart b/test/test.g.dart index 6bbea5855..1a6917dad 100644 --- a/test/test.g.dart +++ b/test/test.g.dart @@ -31,7 +31,7 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Car._); - return const SchemaObject(ObjectType.topLevel, Car, 'Car', [ + return const SchemaObject(ObjectType.realmObject, Car, 'Car', [ SchemaProperty('make', RealmPropertyType.string, primaryKey: true), ]); } @@ -62,7 +62,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Person._); - return const SchemaObject(ObjectType.topLevel, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string), ]); } @@ -108,7 +108,7 @@ class Dog extends _Dog with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Dog._); - return const SchemaObject(ObjectType.topLevel, Dog, 'Dog', [ + return const SchemaObject(ObjectType.realmObject, Dog, 'Dog', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('age', RealmPropertyType.int, optional: true), SchemaProperty('owner', RealmPropertyType.object, @@ -161,7 +161,7 @@ class Team extends _Team with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Team._); - return const SchemaObject(ObjectType.topLevel, Team, 'Team', [ + return const SchemaObject(ObjectType.realmObject, Team, 'Team', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('players', RealmPropertyType.object, linkTarget: 'Person', collectionType: RealmCollectionType.list), @@ -219,7 +219,7 @@ class Student extends _Student with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Student._); - return const SchemaObject(ObjectType.topLevel, Student, 'Student', [ + return const SchemaObject(ObjectType.realmObject, Student, 'Student', [ SchemaProperty('number', RealmPropertyType.int, primaryKey: true), SchemaProperty('name', RealmPropertyType.string, optional: true), SchemaProperty('yearOfBirth', RealmPropertyType.int, optional: true), @@ -290,7 +290,7 @@ class School extends _School with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(School._); - return const SchemaObject(ObjectType.topLevel, School, 'School', [ + return const SchemaObject(ObjectType.realmObject, School, 'School', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('city', RealmPropertyType.string, optional: true), SchemaProperty('students', RealmPropertyType.object, @@ -343,7 +343,7 @@ class RemappedClass extends $RemappedClass static SchemaObject _initSchema() { RealmObjectBase.registerFactory(RemappedClass._); return const SchemaObject( - ObjectType.topLevel, RemappedClass, 'myRemappedClass', [ + ObjectType.realmObject, RemappedClass, 'myRemappedClass', [ SchemaProperty('primitive_property', RealmPropertyType.string, mapTo: 'primitive_property'), SchemaProperty('list-with-dashes', RealmPropertyType.object, @@ -379,7 +379,7 @@ class Task extends _Task with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Task._); - return const SchemaObject(ObjectType.topLevel, Task, 'Task', [ + return const SchemaObject(ObjectType.realmObject, Task, 'Task', [ SchemaProperty('_id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), ]); @@ -420,7 +420,7 @@ class Product extends _Product with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Product._); - return const SchemaObject(ObjectType.topLevel, Product, 'Product', [ + return const SchemaObject(ObjectType.realmObject, Product, 'Product', [ SchemaProperty('_id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('stringQueryField', RealmPropertyType.string, @@ -464,7 +464,7 @@ class Schedule extends _Schedule static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Schedule._); - return const SchemaObject(ObjectType.topLevel, Schedule, 'Schedule', [ + return const SchemaObject(ObjectType.realmObject, Schedule, 'Schedule', [ SchemaProperty('_id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('tasks', RealmPropertyType.object, @@ -611,7 +611,7 @@ class AllTypes extends _AllTypes static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(AllTypes._); - return const SchemaObject(ObjectType.topLevel, AllTypes, 'AllTypes', [ + return const SchemaObject(ObjectType.realmObject, AllTypes, 'AllTypes', [ SchemaProperty('stringProp', RealmPropertyType.string), SchemaProperty('boolProp', RealmPropertyType.bool), SchemaProperty('dateProp', RealmPropertyType.timestamp), @@ -681,7 +681,8 @@ class LinksClass extends _LinksClass static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(LinksClass._); - return const SchemaObject(ObjectType.topLevel, LinksClass, 'LinksClass', [ + return const SchemaObject( + ObjectType.realmObject, LinksClass, 'LinksClass', [ SchemaProperty('id', RealmPropertyType.uuid, primaryKey: true), SchemaProperty('link', RealmPropertyType.object, optional: true, linkTarget: 'LinksClass'), @@ -851,7 +852,7 @@ class AllCollections extends _AllCollections static SchemaObject _initSchema() { RealmObjectBase.registerFactory(AllCollections._); return const SchemaObject( - ObjectType.topLevel, AllCollections, 'AllCollections', [ + ObjectType.realmObject, AllCollections, 'AllCollections', [ SchemaProperty('strings', RealmPropertyType.string, collectionType: RealmCollectionType.list), SchemaProperty('bools', RealmPropertyType.bool, @@ -976,7 +977,7 @@ class NullableTypes extends _NullableTypes static SchemaObject _initSchema() { RealmObjectBase.registerFactory(NullableTypes._); return const SchemaObject( - ObjectType.topLevel, NullableTypes, 'NullableTypes', [ + ObjectType.realmObject, NullableTypes, 'NullableTypes', [ SchemaProperty('_id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('differentiator', RealmPropertyType.objectid), @@ -1053,7 +1054,7 @@ class Event extends _Event with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Event._); - return const SchemaObject(ObjectType.topLevel, Event, 'Event', [ + return const SchemaObject(ObjectType.realmObject, Event, 'Event', [ SchemaProperty('_id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('stringQueryField', RealmPropertyType.string, @@ -1117,7 +1118,7 @@ class Party extends _Party with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Party._); - return const SchemaObject(ObjectType.topLevel, Party, 'Party', [ + return const SchemaObject(ObjectType.realmObject, Party, 'Party', [ SchemaProperty('host', RealmPropertyType.object, optional: true, linkTarget: 'Friend'), SchemaProperty('year', RealmPropertyType.int), @@ -1187,7 +1188,7 @@ class Friend extends _Friend with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Friend._); - return const SchemaObject(ObjectType.topLevel, Friend, 'Friend', [ + return const SchemaObject(ObjectType.realmObject, Friend, 'Friend', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('age', RealmPropertyType.int), SchemaProperty('bestFriend', RealmPropertyType.object, @@ -1234,7 +1235,7 @@ class When extends _When with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(When._); - return const SchemaObject(ObjectType.topLevel, When, 'When', [ + return const SchemaObject(ObjectType.realmObject, When, 'When', [ SchemaProperty('dateTimeUtc', RealmPropertyType.timestamp), SchemaProperty('locationName', RealmPropertyType.string), ]); @@ -1283,7 +1284,7 @@ class Player extends _Player with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Player._); - return const SchemaObject(ObjectType.topLevel, Player, 'Player', [ + return const SchemaObject(ObjectType.realmObject, Player, 'Player', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('game', RealmPropertyType.object, optional: true, linkTarget: 'Game'), @@ -1321,7 +1322,7 @@ class Game extends _Game with RealmEntity, RealmObjectBase, RealmObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Game._); - return const SchemaObject(ObjectType.topLevel, Game, 'Game', [ + return const SchemaObject(ObjectType.realmObject, Game, 'Game', [ SchemaProperty('winnerByRound', RealmPropertyType.object, linkTarget: 'Player', collectionType: RealmCollectionType.list), ]); @@ -1534,7 +1535,7 @@ class AllTypesEmbedded extends _AllTypesEmbedded static SchemaObject _initSchema() { RealmObjectBase.registerFactory(AllTypesEmbedded._); return const SchemaObject( - ObjectType.embedded, AllTypesEmbedded, 'AllTypesEmbedded', [ + ObjectType.embeddedObject, AllTypesEmbedded, 'AllTypesEmbedded', [ SchemaProperty('stringProp', RealmPropertyType.string), SchemaProperty('boolProp', RealmPropertyType.bool), SchemaProperty('dateProp', RealmPropertyType.timestamp), @@ -1652,7 +1653,7 @@ class ObjectWithEmbedded extends _ObjectWithEmbedded static SchemaObject _initSchema() { RealmObjectBase.registerFactory(ObjectWithEmbedded._); return const SchemaObject( - ObjectType.topLevel, ObjectWithEmbedded, 'ObjectWithEmbedded', [ + ObjectType.realmObject, ObjectWithEmbedded, 'ObjectWithEmbedded', [ SchemaProperty('_id', RealmPropertyType.string, mapTo: '_id', primaryKey: true), SchemaProperty('differentiator', RealmPropertyType.uuid, optional: true), @@ -1675,12 +1676,12 @@ class RecursiveEmbedded1 extends _RecursiveEmbedded1 RecursiveEmbedded1( String value, { RecursiveEmbedded2? child, - ObjectWithEmbedded? topLevel, + ObjectWithEmbedded? realmObject, Iterable children = const [], }) { RealmObjectBase.set(this, 'value', value); RealmObjectBase.set(this, 'child', child); - RealmObjectBase.set(this, 'topLevel', topLevel); + RealmObjectBase.set(this, 'realmObject', realmObject); RealmObjectBase.set>( this, 'children', RealmList(children)); } @@ -1709,12 +1710,12 @@ class RecursiveEmbedded1 extends _RecursiveEmbedded1 throw RealmUnsupportedSetError(); @override - ObjectWithEmbedded? get topLevel => - RealmObjectBase.get(this, 'topLevel') + ObjectWithEmbedded? get realmObject => + RealmObjectBase.get(this, 'realmObject') as ObjectWithEmbedded?; @override - set topLevel(covariant ObjectWithEmbedded? value) => - RealmObjectBase.set(this, 'topLevel', value); + set realmObject(covariant ObjectWithEmbedded? value) => + RealmObjectBase.set(this, 'realmObject', value); @override Stream> get changes => @@ -1729,14 +1730,14 @@ class RecursiveEmbedded1 extends _RecursiveEmbedded1 static SchemaObject _initSchema() { RealmObjectBase.registerFactory(RecursiveEmbedded1._); return const SchemaObject( - ObjectType.embedded, RecursiveEmbedded1, 'RecursiveEmbedded1', [ + ObjectType.embeddedObject, RecursiveEmbedded1, 'RecursiveEmbedded1', [ SchemaProperty('value', RealmPropertyType.string), SchemaProperty('child', RealmPropertyType.object, optional: true, linkTarget: 'RecursiveEmbedded2'), SchemaProperty('children', RealmPropertyType.object, linkTarget: 'RecursiveEmbedded2', collectionType: RealmCollectionType.list), - SchemaProperty('topLevel', RealmPropertyType.object, + SchemaProperty('realmObject', RealmPropertyType.object, optional: true, linkTarget: 'ObjectWithEmbedded'), ]); } @@ -1747,12 +1748,12 @@ class RecursiveEmbedded2 extends _RecursiveEmbedded2 RecursiveEmbedded2( String value, { RecursiveEmbedded3? child, - ObjectWithEmbedded? topLevel, + ObjectWithEmbedded? realmObject, Iterable children = const [], }) { RealmObjectBase.set(this, 'value', value); RealmObjectBase.set(this, 'child', child); - RealmObjectBase.set(this, 'topLevel', topLevel); + RealmObjectBase.set(this, 'realmObject', realmObject); RealmObjectBase.set>( this, 'children', RealmList(children)); } @@ -1781,12 +1782,12 @@ class RecursiveEmbedded2 extends _RecursiveEmbedded2 throw RealmUnsupportedSetError(); @override - ObjectWithEmbedded? get topLevel => - RealmObjectBase.get(this, 'topLevel') + ObjectWithEmbedded? get realmObject => + RealmObjectBase.get(this, 'realmObject') as ObjectWithEmbedded?; @override - set topLevel(covariant ObjectWithEmbedded? value) => - RealmObjectBase.set(this, 'topLevel', value); + set realmObject(covariant ObjectWithEmbedded? value) => + RealmObjectBase.set(this, 'realmObject', value); @override Stream> get changes => @@ -1801,14 +1802,14 @@ class RecursiveEmbedded2 extends _RecursiveEmbedded2 static SchemaObject _initSchema() { RealmObjectBase.registerFactory(RecursiveEmbedded2._); return const SchemaObject( - ObjectType.embedded, RecursiveEmbedded2, 'RecursiveEmbedded2', [ + ObjectType.embeddedObject, RecursiveEmbedded2, 'RecursiveEmbedded2', [ SchemaProperty('value', RealmPropertyType.string), SchemaProperty('child', RealmPropertyType.object, optional: true, linkTarget: 'RecursiveEmbedded3'), SchemaProperty('children', RealmPropertyType.object, linkTarget: 'RecursiveEmbedded3', collectionType: RealmCollectionType.list), - SchemaProperty('topLevel', RealmPropertyType.object, + SchemaProperty('realmObject', RealmPropertyType.object, optional: true, linkTarget: 'ObjectWithEmbedded'), ]); } @@ -1842,7 +1843,7 @@ class RecursiveEmbedded3 extends _RecursiveEmbedded3 static SchemaObject _initSchema() { RealmObjectBase.registerFactory(RecursiveEmbedded3._); return const SchemaObject( - ObjectType.embedded, RecursiveEmbedded3, 'RecursiveEmbedded3', [ + ObjectType.embeddedObject, RecursiveEmbedded3, 'RecursiveEmbedded3', [ SchemaProperty('value', RealmPropertyType.string), ]); } From a03da321919a8d193f0e838f351520abd529ab92 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Fri, 21 Oct 2022 14:37:55 +0200 Subject: [PATCH 11/13] fix generator tests --- .../test/error_test_data/embedded_object_primary_key.expected | 4 ++-- generator/test/good_test_data/embedded_annotations.expected | 2 +- generator/test/good_test_data/embedded_objects.expected | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/generator/test/error_test_data/embedded_object_primary_key.expected b/generator/test/error_test_data/embedded_object_primary_key.expected index f482f03a9..869f6e39f 100644 --- a/generator/test/error_test_data/embedded_object_primary_key.expected +++ b/generator/test/error_test_data/embedded_object_primary_key.expected @@ -2,12 +2,12 @@ Primary key not allowed on embedded objects in: asset:pkg/test/error_test_data/embedded_object_primary_key.dart:8:12 ╷ -5 │ @RealmModel(ObjectType.embedded) +5 │ @RealmModel(ObjectType.embeddedObject) 6 │ class _Bad { │ ━━━━ 7 │ @PrimaryKey() 8 │ late int id; │ ^^ Bad is marked as embedded but has primary key defined ╵ -Remove the @PrimaryKey annotation from the field or set the model type to a value different from ObjectType.embedded. +Remove the @PrimaryKey annotation from the field or set the model type to a value different from ObjectType.embeddedObject. diff --git a/generator/test/good_test_data/embedded_annotations.expected b/generator/test/good_test_data/embedded_annotations.expected index 987c2f015..b04c8c91b 100644 --- a/generator/test/good_test_data/embedded_annotations.expected +++ b/generator/test/good_test_data/embedded_annotations.expected @@ -93,7 +93,7 @@ class Child1 extends _Child1 with RealmEntity, RealmObjectBase, EmbeddedObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Child1._); - return const SchemaObject(ObjectType.embedded, Child1, 'MySuperChild', [ + return const SchemaObject(ObjectType.embeddedObject, Child1, 'MySuperChild', [ SchemaProperty('_value', RealmPropertyType.string, mapTo: '_value'), SchemaProperty('_parent', RealmPropertyType.object, mapTo: '_parent', optional: true, linkTarget: 'Parent'), diff --git a/generator/test/good_test_data/embedded_objects.expected b/generator/test/good_test_data/embedded_objects.expected index 4911f539b..efff5f50f 100644 --- a/generator/test/good_test_data/embedded_objects.expected +++ b/generator/test/good_test_data/embedded_objects.expected @@ -99,7 +99,7 @@ class Child1 extends _Child1 with RealmEntity, RealmObjectBase, EmbeddedObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Child1._); - return const SchemaObject(ObjectType.embedded, Child1, 'Child1', [ + return const SchemaObject(ObjectType.embeddedObject, Child1, 'Child1', [ SchemaProperty('value', RealmPropertyType.string), SchemaProperty('child', RealmPropertyType.object, optional: true, linkTarget: 'Child2'), @@ -248,7 +248,7 @@ class Child2 extends _Child2 with RealmEntity, RealmObjectBase, EmbeddedObject { static SchemaObject? _schema; static SchemaObject _initSchema() { RealmObjectBase.registerFactory(Child2._); - return const SchemaObject(ObjectType.embedded, Child2, 'Child2', [ + return const SchemaObject(ObjectType.embeddedObject, Child2, 'Child2', [ SchemaProperty('boolProp', RealmPropertyType.bool), SchemaProperty('intProp', RealmPropertyType.int), SchemaProperty('doubleProp', RealmPropertyType.double), From ceca6531638c7ec5896613d69eff27949035cc35 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Fri, 21 Oct 2022 14:38:52 +0200 Subject: [PATCH 12/13] Fix waitForCondition --- test/test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.dart b/test/test.dart index 23529c79d..70f0ef3ae 100644 --- a/test/test.dart +++ b/test/test.dart @@ -629,7 +629,7 @@ Future waitForCondition( Duration retryDelay = const Duration(milliseconds: 100), String? message, }) { - return waitForConditionWithResult(() => condition(), (value) => value == true); + return waitForConditionWithResult(() => condition(), (value) => value == true, timeout: timeout, retryDelay: retryDelay, message: message); } Future waitForConditionWithResult(FutureOr Function() getter, FutureOr Function(T value) condition, From 48b45374cb8755d79aef4baa99f8733d4344d01a Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Fri, 21 Oct 2022 15:37:15 +0200 Subject: [PATCH 13/13] Update docs on SchemaProperty --- lib/src/realm_property.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/realm_property.dart b/lib/src/realm_property.dart index b8fd60198..e33abf86a 100644 --- a/lib/src/realm_property.dart +++ b/lib/src/realm_property.dart @@ -16,9 +16,9 @@ // //////////////////////////////////////////////////////////////////////////////// -import 'package:realm_common/realm_common.dart'; +import 'realm_class.dart'; -/// Describes a `RealmObject`/`EmbeddedObject`'s property with its name, type and other attributes in the [RealmSchema] +/// Describes a property on [RealmObject]/[EmbeddedObject] with its name, type and other attributes in the [RealmSchema] ///{@category Configuration} class SchemaProperty { /// The name of the property as persisted in the `Realm`