diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d2decbaf..75fbcd092 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Enhancements * Added a new parameter of type `SyncTimeoutOptions` to `AppConfiguration`. It allows users to control sync timings, such as ping/pong intervals as well various connection timeouts. (Issue [#1763](https://github.com/realm/realm-dart/issues/1763)) * Added a new parameter `cancelAsyncOperationsOnNonFatalErrors` on `Configuration.flexibleSync` that allows users to control whether non-fatal errors such as connection timeouts should be surfaced in the form of errors or if sync should try and reconnect in the background. (PR [#1764](https://github.com/realm/realm-dart/pull/1764)) +* Allow nullable and other optional fields to be absent in EJson, when deserializing realm objects. (Issue [#1735](https://github.com/realm/realm-dart/issues/1735)) ### Fixed * Fixed an issue where creating a flexible sync configuration with an embedded object not referenced by any top-level object would throw a "No such table" exception with no meaningful information about the issue. Now a `RealmException` will be thrown that includes the offending object name, as well as more precise text for what the root cause of the error is. (PR [#1748](https://github.com/realm/realm-dart/pull/1748)) diff --git a/packages/CHANGELOG.ejson.md b/packages/CHANGELOG.ejson.md index 8e29de2bc..03e24fd6d 100644 --- a/packages/CHANGELOG.ejson.md +++ b/packages/CHANGELOG.ejson.md @@ -1,3 +1,10 @@ +## 0.4.0 + +- `fromEJson` now accepts a `defaultValue` argument that is returned if + `null` is passed as `ejson`. +- `register` takes an optional `superTypes` argument to specify the super + types of `T` if needed. + ## 0.3.1 - Update sane_uuid dependency to ^1.0.0 (compensate for breaking change) diff --git a/packages/ejson/lib/src/configuration.dart b/packages/ejson/lib/src/configuration.dart index 7ef45d9f9..df02c86b3 100644 --- a/packages/ejson/lib/src/configuration.dart +++ b/packages/ejson/lib/src/configuration.dart @@ -9,8 +9,8 @@ import 'encoding.dart'; /// Register custom EJSON [encoder] and [decoder] for a type [T]. /// The last registered codec pair for a given type [T] will be used. -void register(EJsonEncoder encoder, EJsonDecoder decoder) { - TypePlus.add(); +void register(EJsonEncoder encoder, EJsonDecoder decoder, {Iterable? superTypes}) { + TypePlus.add(superTypes: superTypes); customEncoders[T] = encoder; customDecoders[T] = decoder; } diff --git a/packages/ejson/lib/src/decoding.dart b/packages/ejson/lib/src/decoding.dart index f91549abb..d551381c2 100644 --- a/packages/ejson/lib/src/decoding.dart +++ b/packages/ejson/lib/src/decoding.dart @@ -19,6 +19,7 @@ const _commonDecoders = { Object: _decodeAny, Iterable: _decodeArray, List: _decodeArray, + Set: _decodeSet, bool: _decodeBool, DateTime: _decodeDate, Defined: _decodeDefined, @@ -32,34 +33,42 @@ const _commonDecoders = { Symbol: _decodeSymbol, Uint8List: _decodeBinary, Uuid: _decodeUuid, + DBRef: _decodeDBRef, Undefined: _decodeUndefined, UndefinedOr: _decodeUndefinedOr, }; -/// Custom decoders for specific types. Use `register` to add a custom decoder. -final customDecoders = {}; - -final _decoders = () { +/// Predefined decoders for common types +final commonDecoders = () { // register extra common types on first access undefinedOr(dynamic f) => f>(); TypePlus.addFactory(undefinedOr); TypePlus.addFactory((dynamic f) => f>(), superTypes: [undefinedOr]); TypePlus.addFactory((dynamic f) => f>(), superTypes: [undefinedOr]); + TypePlus.addFactory((dynamic f) => f>()); TypePlus.add(); TypePlus.add(); TypePlus.add(); TypePlus.add(); - return CombinedMapView([customDecoders, _commonDecoders]); + return _commonDecoders; }(); +/// Custom decoders for specific types. Use `register` to add a custom decoder. +final customDecoders = {}; + +final _decoders = CombinedMapView([customDecoders, commonDecoders]); + /// Converts [ejson] to type [T]. /// +/// [defaultValue] is returned if set, and [ejson] is `null`. +/// /// Throws [InvalidEJson] if [ejson] is not valid for [T]. /// Throws [MissingDecoder] if no decoder is registered for [T]. -T fromEJson(EJsonValue ejson) { +T fromEJson(EJsonValue ejson, {T? defaultValue}) { final type = T; final nullable = type.isNullable; + if (ejson == null && defaultValue != null) return defaultValue; final decoder = nullable ? _decodeNullable : _decoders[type.base]; if (decoder == null) { throw MissingDecoder._(ejson, type); @@ -94,32 +103,30 @@ dynamic _decodeAny(EJsonValue ejson) { {'\$numberDouble': _} => _decodeDouble(ejson), {'\$numberInt': _} => _decodeInt(ejson), {'\$numberLong': _} => _decodeInt(ejson), + {'\$ref': _, '\$id': _} => _decodeDBRef(ejson), {'\$regex': _} => _decodeString(ejson), {'\$symbol': _} => _decodeSymbol(ejson), {'\$undefined': _} => _decodeUndefined(ejson), {'\$oid': _} => _decodeObjectId(ejson), {'\$binary': {'base64': _, 'subType': '04'}} => _decodeUuid(ejson), {'\$binary': _} => _decodeBinary(ejson), - List _ => _decodeArray(ejson), - Map _ => _tryDecodeCustom(ejson) ?? _decodeDocument(ejson), // other maps goes last!! + List _ => _decodeArray(ejson), + Set _ => _decodeSet(ejson), + Map _ => _decodeDocument(ejson), // other maps goes last!! _ => raiseInvalidEJson(ejson), }; } -dynamic _tryDecodeCustom(EJsonValue ejson) { - for (final decoder in customDecoders.values) { - try { - return decoder(ejson); - } catch (_) { - // ignore - } - } - return null; +List _decodeArray(EJsonValue ejson) { + return switch (ejson) { + Iterable i => i.map((ejson) => fromEJson(ejson)).toList(), + _ => raiseInvalidEJson(ejson), + }; } -List _decodeArray(EJsonValue ejson) { +Set _decodeSet(EJsonValue ejson) { return switch (ejson) { - Iterable i => i.map((ejson) => fromEJson(ejson)).toList(), + Iterable i => i.map((ejson) => fromEJson(ejson)).toSet(), _ => raiseInvalidEJson(ejson), }; } @@ -139,14 +146,24 @@ DateTime _decodeDate(EJsonValue ejson) { }; } +DBRef _decodeDBRef(EJsonValue ejson) { + switch (ejson) { + case {'\$ref': String collection, '\$id': EJsonValue id}: + KeyT key = fromEJson(id); + return DBRef.new.callWith(parameters: [collection, key], typeArguments: [key.runtimeType]) as DBRef; + default: + return raiseInvalidEJson(ejson); + } +} + Defined _decodeDefined(EJsonValue ejson) { if (ejson case {'\$undefined': 1}) return raiseInvalidEJson(ejson); - return Defined(fromEJson(ejson)); + return Defined(fromEJson(ejson)); } Map _decodeDocument(EJsonValue ejson) { return switch (ejson) { - Map m => m.map((key, value) => MapEntry(key as K, fromEJson(value))), + Map m => m.map((key, value) => MapEntry(key as K, fromEJson(value))), _ => raiseInvalidEJson(ejson), }; } @@ -229,14 +246,14 @@ Symbol _decodeSymbol(EJsonValue ejson) { Undefined _decodeUndefined(EJsonValue ejson) { return switch (ejson) { - {'\$undefined': 1} => Undefined(), + {'\$undefined': 1} => Undefined(), _ => raiseInvalidEJson(ejson), }; } UndefinedOr _decodeUndefinedOr(EJsonValue ejson) { return switch (ejson) { - {'\$undefined': 1} => Undefined(), + {'\$undefined': 1} => Undefined(), _ => _decodeDefined(ejson), }; } diff --git a/packages/ejson/lib/src/encoding.dart b/packages/ejson/lib/src/encoding.dart index cfe11b15d..8a3156a1d 100644 --- a/packages/ejson/lib/src/encoding.dart +++ b/packages/ejson/lib/src/encoding.dart @@ -34,12 +34,13 @@ EJsonValue _encodeAny(Object? value) { null => null, bool b => _encodeBool(b), DateTime d => _encodeDate(d), + DBRef d => _encodeDBRef(d), Defined d => _encodeDefined(d), double d => _encodeDouble(d), int i => _encodeInt(i), BsonKey k => _encodeKey(k), Uint8List b => _encodeBinary(b, subtype: '00'), - Iterable l => _encodeArray(l), + Iterable l => _encodeArray(l), // handles List and Set as well Map m => _encodeDocument(m), ObjectId o => _encodeObjectId(o), String s => _encodeString(s), @@ -71,6 +72,13 @@ EJsonValue _encodeDate(DateTime value) { }; } +EJsonValue _encodeDBRef(DBRef d) { + return { + '\$ref': d.collection, + '\$id': toEJson(d.id), + }; +} + EJsonValue _encodeDefined(Defined defined) => toEJson(defined.value); EJsonValue _encodeDocument(Map map) => map.map((k, v) => MapEntry(k, toEJson(v))); @@ -150,6 +158,12 @@ extension DateTimeEJsonEncoderExtension on DateTime { EJsonValue toEJson() => _encodeDate(this); } +extension DBRefEJsonEncoderExtension on DBRef { + /// Converts this [DBRef] to EJson + @pragma('vm:prefer-inline') + EJsonValue toEJson() => _encodeDBRef(this); +} + extension DefinedEJsonEncoderExtension on Defined { /// Converts this [Defined] to EJson @pragma('vm:prefer-inline') diff --git a/packages/ejson/lib/src/types.dart b/packages/ejson/lib/src/types.dart index 671e84df9..37f080927 100644 --- a/packages/ejson/lib/src/types.dart +++ b/packages/ejson/lib/src/types.dart @@ -5,7 +5,8 @@ enum EJsonType { array, binary, boolean, - date, + databaseRef, + date, // use this instead of timestamp decimal128, document, double, @@ -13,16 +14,15 @@ enum EJsonType { int64, maxKey, minKey, + nil, // aka. null objectId, string, symbol, - nil, // aka. null undefined, // TODO: The following is not supported yet // code, // codeWithScope, - // databasePointer, - // databaseRef, + // databasePointer, // deprecated // regularExpression, // timestamp, // This is not what you think, see https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-Timestamp } @@ -31,6 +31,22 @@ enum EJsonType { /// and [MinKey](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-MinKey) enum BsonKey { min, max } +/// See [DBRef](https://github.com/mongodb/specifications/blob/master/source/dbref.md) +/// This is not technically a BSON type, but a common convention. +final class DBRef { + // Do we need to support the database name? + final String collection; + final KeyT id; + + const DBRef(this.collection, this.id); + + @override + int get hashCode => Object.hash(collection, id); + + @override + bool operator ==(Object other) => other is DBRef && collection == other.collection && id == other.id; +} + sealed class UndefinedOr { const UndefinedOr(); } diff --git a/packages/ejson/test/ejson_test.dart b/packages/ejson/test/ejson_test.dart index 04e1e96d7..ee3a18633 100644 --- a/packages/ejson/test/ejson_test.dart +++ b/packages/ejson/test/ejson_test.dart @@ -10,9 +10,41 @@ import 'package:ejson/ejson.dart'; import 'package:objectid/objectid.dart'; import 'package:sane_uuid/uuid.dart'; import 'package:test/test.dart'; +import 'package:type_plus/type_plus.dart'; import 'person.dart'; +bool _canDecodeAny([Type? type]) { + commonDecoders; // ensure common types has been registered; + type ??= T; + if (type.isNullable) return _canDecodeAny(type.base); + if ([ + dynamic, + Null, + Object, + bool, + double, + int, + num, + String, + DateTime, + BsonKey, + Symbol, + ObjectId, + Uuid, + Uint8List, + ].contains(type)) return true; + if ([ + List, + Set, + Map, + DBRef, + Undefined, + UndefinedOr, + ].contains(type.base)) return type.args.every(_canDecodeAny); + return false; +} + void _testCase(T value, EJsonValue expected) { test('encode from $value of type $T', () { expect(toEJson(value), expected); @@ -46,7 +78,7 @@ void _testCase(T value, EJsonValue expected) { expect(() => fromEJson(expected), returnsNormally); }); - if (value is! Defined) { + if (_canDecodeAny()) { test('roundtrip $value of type $T as dynamic', () { // no here, so dynamic expect(fromEJson(toEJson(value)), value); @@ -82,6 +114,7 @@ void main() { expect([1, 2, 3].toEJson(), toEJson([1, 2, 3])); expect({'a': 1, 'b': 2}.toEJson(), toEJson({'a': 1, 'b': 2})); expect(DateTime(1974, 4, 10, 2, 42, 12, 202).toEJson(), toEJson(DateTime(1974, 4, 10, 2, 42, 12, 202))); + expect(DBRef('collection', 42).toEJson(), toEJson(DBRef('collection', 42))); expect((#sym).toEJson(), toEJson(#sym)); expect(BsonKey.max.toEJson(), toEJson(BsonKey.max)); expect(BsonKey.min.toEJson(), toEJson(BsonKey.min)); @@ -105,11 +138,17 @@ void main() { group('invalid', () { _invalidTestCase(); _invalidTestCase(); + _invalidTestCase(); + _invalidTestCase>(); _invalidTestCase({'\$numberDouble': 'foobar'}); _invalidTestCase(); _invalidTestCase(); _invalidTestCase(); + _invalidTestCase(); _invalidTestCase>(); + _invalidTestCase(); + _invalidTestCase>(); + _invalidTestCase([]); _invalidTestCase>([]); _invalidTestCase(); _invalidTestCase(); @@ -118,6 +157,7 @@ void main() { _invalidTestCase(); _invalidTestCase(); _invalidTestCase(); + _invalidTestCase(); _invalidTestCase>(); _invalidTestCase(); @@ -151,6 +191,16 @@ void main() { ] : [1, 2, 3], ); + _testCase( + {1, 2, 3}, + canonical + ? { + {'\$numberInt': '1'}, + {'\$numberInt': '2'}, + {'\$numberInt': '3'}, + } + : {1, 2, 3}, + ); _testCase( [1, 1.1], canonical @@ -190,6 +240,10 @@ void main() { _testCase(#sym, {'\$symbol': 'sym'}); _testCase(BsonKey.max, {'\$maxKey': 1}); _testCase(BsonKey.min, {'\$minKey': 1}); + _testCase(const DBRef('collection', 42), { + '\$ref': 'collection', + '\$id': canonical ? {'\$numberInt': '42'} : 42, + }); _testCase(undefined, {'\$undefined': 1}); _testCase(const Undefined(), {'\$undefined': 1}); _testCase(Undefined(), {'\$undefined': 1}); diff --git a/packages/ejson_generator/test/ctor_test.dart b/packages/ejson_generator/test/ctor_test.dart index e97641993..a7ab3a0eb 100644 --- a/packages/ejson_generator/test/ctor_test.dart +++ b/packages/ejson_generator/test/ctor_test.dart @@ -85,13 +85,6 @@ void _testCase(T value, EJsonValue expected) { test('roundtrip $expected as $T', () { expect(toEJson(fromEJson(expected)), expected); }); - - test('roundtrip $expected of type $T as dynamic', () { - // no here, so dynamic - final decoded = fromEJson(expected); - expect(decoded, isA()); - expect(toEJson(decoded), expected); - }); } void main() { diff --git a/packages/realm/example/lib/main.realm.dart b/packages/realm/example/lib/main.realm.dart index e43d88cff..af89ced93 100644 --- a/packages/realm/example/lib/main.realm.dart +++ b/packages/realm/example/lib/main.realm.dart @@ -72,18 +72,16 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Car value) => value.toEJson(); static Car _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'make': EJsonValue make, - 'model': EJsonValue model, - 'kilometers': EJsonValue kilometers, - 'owner': EJsonValue owner, } => Car( fromEJson(make), - model: fromEJson(model), - kilometers: fromEJson(kilometers), - owner: fromEJson(owner), + model: fromEJson(ejson['model']), + kilometers: fromEJson(ejson['kilometers'], defaultValue: 500), + owner: fromEJson(ejson['owner']), ), _ => raiseInvalidEJson(ejson), }; @@ -92,7 +90,7 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Car._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, 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), @@ -153,14 +151,14 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Person value) => value.toEJson(); static Person _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, - 'age': EJsonValue age, } => Person( fromEJson(name), - age: fromEJson(age), + age: fromEJson(ejson['age'], defaultValue: 1), ), _ => raiseInvalidEJson(ejson), }; @@ -169,7 +167,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Person._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('age', RealmPropertyType.int), ]); diff --git a/packages/realm/tests/integration_test/all_tests.dart b/packages/realm/tests/integration_test/all_tests.dart index 21e725115..8b2b98ab3 100644 --- a/packages/realm/tests/integration_test/all_tests.dart +++ b/packages/realm/tests/integration_test/all_tests.dart @@ -8,6 +8,7 @@ import 'package:integration_test/integration_test.dart'; import 'package:test/test.dart'; import '../../../realm_dart/test/test.dart' as test; + import '../../../realm_dart/test/app_test.dart' as app_test; import '../../../realm_dart/test/asymmetric_test.dart' as asymmetric_test; import '../../../realm_dart/test/backlinks_test.dart' as backlinks_test; @@ -29,10 +30,11 @@ import '../../../realm_dart/test/realm_set_test.dart' as realm_set_test; import '../../../realm_dart/test/realm_test.dart' as realm_test; import '../../../realm_dart/test/realm_value_test.dart' as realm_value_test; import '../../../realm_dart/test/results_test.dart' as results_test; +import '../../../realm_dart/test/serialization_test.dart' as serialization_test; import '../../../realm_dart/test/session_test.dart' as session_test; import '../../../realm_dart/test/subscription_test.dart' as subscription_test; -import '../../../realm_dart/test/user_test.dart' as user_test; import '../../../realm_dart/test/sync_migration_test.dart' as sync_migration_test; +import '../../../realm_dart/test/user_test.dart' as user_test; Future _copyBundledFile(String fromPath, String toPath) async { final data = await rootBundle.load(fromPath); @@ -68,8 +70,9 @@ void main() { group('realm_test.dart', realm_test.main); group('realm_value_test.dart', realm_value_test.main); group('results_test.dart', results_test.main); + group('serialization_test.dart', serialization_test.main); group('session_test.dart', session_test.main); group('subscription_test.dart', subscription_test.main); - group('user_test.dart', user_test.main); group('sync_migration_test.dart', sync_migration_test.main); + group('user_test.dart', user_test.main); } diff --git a/packages/realm_dart/example/bin/myapp.realm.dart b/packages/realm_dart/example/bin/myapp.realm.dart index 1a13aa667..fa5822b11 100644 --- a/packages/realm_dart/example/bin/myapp.realm.dart +++ b/packages/realm_dart/example/bin/myapp.realm.dart @@ -72,18 +72,16 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Car value) => value.toEJson(); static Car _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'make': EJsonValue make, - 'model': EJsonValue model, - 'kilometers': EJsonValue kilometers, - 'owner': EJsonValue owner, } => Car( fromEJson(make), - model: fromEJson(model), - kilometers: fromEJson(kilometers), - owner: fromEJson(owner), + model: fromEJson(ejson['model']), + kilometers: fromEJson(ejson['kilometers'], defaultValue: 500), + owner: fromEJson(ejson['owner']), ), _ => raiseInvalidEJson(ejson), }; @@ -92,7 +90,7 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Car._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, 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), @@ -153,14 +151,14 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Person value) => value.toEJson(); static Person _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, - 'age': EJsonValue age, } => Person( fromEJson(name), - age: fromEJson(age), + age: fromEJson(ejson['age'], defaultValue: 42), ), _ => raiseInvalidEJson(ejson), }; @@ -169,7 +167,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Person._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('age', RealmPropertyType.int), ]); diff --git a/packages/realm_dart/lib/src/configuration.dart b/packages/realm_dart/lib/src/configuration.dart index c6ade339b..95cd4fa5d 100644 --- a/packages/realm_dart/lib/src/configuration.dart +++ b/packages/realm_dart/lib/src/configuration.dart @@ -4,6 +4,7 @@ import 'dart:async'; // ignore: no_leading_underscores_for_library_prefixes +import 'package:collection/collection.dart'; import 'package:path/path.dart' as _path; import 'app.dart'; @@ -430,7 +431,7 @@ class SchemaObject extends Iterable { final ObjectType baseType; /// Creates schema instance with object type and collection of object's properties. - SchemaObject(this.baseType, this.type, this.name, Iterable properties) : _properties = List.from(properties); + const SchemaObject(this.baseType, this.type, this.name, this._properties); @override Iterator get iterator => _properties.iterator; @@ -442,6 +443,8 @@ class SchemaObject extends Iterable { @override SchemaProperty elementAt(int index) => _properties.elementAt(index); + + SchemaProperty? get primaryKey => _properties.firstWhereOrNull((p) => p.primaryKey); } /// Describes the complete set of classes which may be stored in a `Realm` diff --git a/packages/realm_dart/lib/src/handles/native/init.dart b/packages/realm_dart/lib/src/handles/native/init.dart index 4dcdf2816..dbebc737e 100644 --- a/packages/realm_dart/lib/src/handles/native/init.dart +++ b/packages/realm_dart/lib/src/handles/native/init.dart @@ -4,8 +4,10 @@ import 'dart:ffi'; import 'dart:io'; +import 'package:ejson/ejson.dart'; import 'package:package_config/package_config.dart'; import 'package:path/path.dart' as p; +import 'package:type_plus/type_plus.dart'; import '../../cli/common/target_os_type.dart'; import '../../cli/metrics/metrics_command.dart'; @@ -15,6 +17,8 @@ import '../../realm_class.dart'; import '../../../realm.dart' show isFlutterPlatform; export '../../../realm.dart' show isFlutterPlatform; +import 'decimal128.dart' as impl; + const realmBinaryName = 'realm_dart'; final targetOsType = Platform.operatingSystem.asTargetOsType ?? _platformNotSupported(); final nativeLibraryName = _getLibName(realmBinaryName); @@ -137,6 +141,33 @@ DynamicLibrary _openRealmLib() { throwError(candidatePaths); } +EJsonValue encodeDecimal128(Decimal128 value) => {'\$numberDecimal': value.toString()}; + +impl.Decimal128 decodeDecimal128(EJsonValue ejson) => switch (ejson) { + {'\$numberDecimal': String x} => impl.Decimal128.parse(x), + _ => raiseInvalidEJson(ejson), + }; + +EJsonValue encodeRealmValue(RealmValue value) { + final v = value.value; + if (v is RealmObject) { + final p = RealmObjectBase.get(v, v.objectSchema.primaryKey!.name); + return DBRef(v.objectSchema.name, p).toEJson(); + } + return toEJson(v); +} + +RealmValue decodeRealmValue(EJsonValue ejson) { + final decoded = fromEJson(ejson); + if (decoded is DBRef) { + final t = TypePlus.fromId(decoded.collection); + final o = RealmObjectBase.createObject(t, null); + o.dynamic.set(o.objectSchema.primaryKey!.name, decoded.id); + return RealmValue.realmObject(o.cast()); + } + return RealmValue.from(decoded); +} + /// @nodoc // Initializes Realm library DynamicLibrary initRealm() { @@ -144,6 +175,10 @@ DynamicLibrary initRealm() { return _library!; } + register(encodeDecimal128, decodeDecimal128, superTypes: [Decimal128]); + register(encodeDecimal128, decodeDecimal128); + register(encodeRealmValue, decodeRealmValue); + if (!isFlutterPlatform) { assert(() { try { @@ -166,3 +201,7 @@ DynamicLibrary initRealm() { return _library = realmLibrary; } + +extension on T { + U cast() => this as U; +} diff --git a/packages/realm_dart/lib/src/realm_object.dart b/packages/realm_dart/lib/src/realm_object.dart index fc17fe799..101cd88b7 100644 --- a/packages/realm_dart/lib/src/realm_object.dart +++ b/packages/realm_dart/lib/src/realm_object.dart @@ -5,6 +5,7 @@ import 'dart:async'; import 'package:collection/collection.dart'; import 'package:realm_common/realm_common.dart'; +import 'package:type_plus/type_plus.dart'; import 'configuration.dart'; import 'handles/handle_base.dart'; @@ -400,6 +401,14 @@ mixin RealmObjectBase on RealmEntity implements RealmObjectBaseMarker { /// @nodoc static void registerFactory(T Function() factory) { + // Register type as both (T).hashCode.toString() and T.name + TypePlus.add(); + if (TypePlus.fromId(T.name) == UnresolvedType) { + // Only register by name, if no other class with the same name is registered + // Can happen, if the same class name is used in different libraries. + TypePlus.add(id: T.name); + } + // We register a factory for both the type itself, but also the nullable // version of the type. _factories.putIfAbsent(T, () => factory); @@ -407,10 +416,10 @@ mixin RealmObjectBase on RealmEntity implements RealmObjectBaseMarker { } /// @nodoc - static RealmObjectBase createObject(Type type, RealmObjectMetadata metadata) { + static RealmObjectBase createObject(Type type, RealmObjectMetadata? metadata) { final factory = _factories[type]; if (factory == null) { - if (type == RealmObjectBase) { + if (type == RealmObjectBase && metadata != null) { switch (metadata.schema.baseType) { case ObjectType.realmObject: return _ConcreteRealmObject(); diff --git a/packages/realm_dart/pubspec.yaml b/packages/realm_dart/pubspec.yaml index 960c6096d..f557a2491 100644 --- a/packages/realm_dart/pubspec.yaml +++ b/packages/realm_dart/pubspec.yaml @@ -31,6 +31,7 @@ dependencies: cancellation_token: ^2.0.0 decimal: ^3.0.1 rational: ^2.2.3 + type_plus: ^2.1.1 dev_dependencies: build_cli: ^2.2.2 diff --git a/packages/realm_dart/test/backlinks_test.realm.dart b/packages/realm_dart/test/backlinks_test.realm.dart index bc35db921..a3d1f0663 100644 --- a/packages/realm_dart/test/backlinks_test.realm.dart +++ b/packages/realm_dart/test/backlinks_test.realm.dart @@ -90,29 +90,20 @@ class Source extends _Source with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Source value) => value.toEJson(); static Source _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'name': EJsonValue name, - 'et mål': EJsonValue oneTarget, - 'manyTargets': EJsonValue manyTargets, - 'dynamisk mål': EJsonValue dynamicTarget, - 'dynamicManyTargets': EJsonValue dynamicManyTargets, - } => - Source( - name: fromEJson(name), - oneTarget: fromEJson(oneTarget), - manyTargets: fromEJson(manyTargets), - dynamicTarget: fromEJson(dynamicTarget), - dynamicManyTargets: fromEJson(dynamicManyTargets), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return Source( + name: fromEJson(ejson['name'], defaultValue: 'source'), + oneTarget: fromEJson(ejson['et mål']), + manyTargets: fromEJson(ejson['manyTargets']), + dynamicTarget: fromEJson(ejson['dynamisk mål']), + dynamicManyTargets: fromEJson(ejson['dynamicManyTargets']), + ); } static final schema = () { RealmObjectBase.registerFactory(Source._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Source, 'Source', [ + return const SchemaObject(ObjectType.realmObject, Source, 'Source', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('oneTarget', RealmPropertyType.object, mapTo: 'et mål', optional: true, linkTarget: 'Target'), @@ -204,23 +195,17 @@ class Target extends _Target with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Target value) => value.toEJson(); static Target _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'name': EJsonValue name, - 'source': EJsonValue source, - } => - Target( - name: fromEJson(name), - source: fromEJson(source), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return Target( + name: fromEJson(ejson['name'], defaultValue: 'target'), + source: fromEJson(ejson['source']), + ); } static final schema = () { RealmObjectBase.registerFactory(Target._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Target, 'Target', [ + return const SchemaObject(ObjectType.realmObject, Target, 'Target', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('source', RealmPropertyType.object, optional: true, linkTarget: 'Source'), diff --git a/packages/realm_dart/test/dynamic_realm_test.realm.dart b/packages/realm_dart/test/dynamic_realm_test.realm.dart index 1829305bd..28bd5aebd 100644 --- a/packages/realm_dart/test/dynamic_realm_test.realm.dart +++ b/packages/realm_dart/test/dynamic_realm_test.realm.dart @@ -50,6 +50,7 @@ class Taskv2 extends _Taskv2 with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Taskv2 value) => value.toEJson(); static Taskv2 _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_id': EJsonValue id, @@ -66,7 +67,7 @@ class Taskv2 extends _Taskv2 with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Taskv2._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Taskv2, 'Task', [ + return const SchemaObject(ObjectType.realmObject, Taskv2, 'Task', [ SchemaProperty('id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('description', RealmPropertyType.string), diff --git a/packages/realm_dart/test/geospatial_test.realm.dart b/packages/realm_dart/test/geospatial_test.realm.dart index 70e002d74..a3a86a05a 100644 --- a/packages/realm_dart/test/geospatial_test.realm.dart +++ b/packages/realm_dart/test/geospatial_test.realm.dart @@ -57,23 +57,17 @@ class Location extends _Location static EJsonValue _toEJson(Location value) => value.toEJson(); static Location _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'type': EJsonValue type, - 'coordinates': EJsonValue coordinates, - } => - Location( - type: fromEJson(type), - coordinates: fromEJson(coordinates), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return Location( + type: fromEJson(ejson['type'], defaultValue: 'Point'), + coordinates: fromEJson(ejson['coordinates']), + ); } static final schema = () { RealmObjectBase.registerFactory(Location._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.embeddedObject, Location, 'Location', [ + return const SchemaObject(ObjectType.embeddedObject, Location, 'Location', [ SchemaProperty('type', RealmPropertyType.string), SchemaProperty('coordinates', RealmPropertyType.double, collectionType: RealmCollectionType.list), @@ -128,14 +122,14 @@ class Restaurant extends _Restaurant static EJsonValue _toEJson(Restaurant value) => value.toEJson(); static Restaurant _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, - 'location': EJsonValue location, } => Restaurant( fromEJson(name), - location: fromEJson(location), + location: fromEJson(ejson['location']), ), _ => raiseInvalidEJson(ejson), }; @@ -144,7 +138,8 @@ class Restaurant extends _Restaurant static final schema = () { RealmObjectBase.registerFactory(Restaurant._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Restaurant, 'Restaurant', [ + return const SchemaObject( + ObjectType.realmObject, Restaurant, 'Restaurant', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('location', RealmPropertyType.object, optional: true, linkTarget: 'Location'), @@ -193,21 +188,17 @@ class LocationList extends _LocationList static EJsonValue _toEJson(LocationList value) => value.toEJson(); static LocationList _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'locations': EJsonValue locations, - } => - LocationList( - locations: fromEJson(locations), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return LocationList( + locations: fromEJson(ejson['locations']), + ); } static final schema = () { RealmObjectBase.registerFactory(LocationList._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, LocationList, 'LocationList', [ + return const SchemaObject( + ObjectType.realmObject, LocationList, 'LocationList', [ SchemaProperty('locations', RealmPropertyType.object, linkTarget: 'Location', collectionType: RealmCollectionType.list), ]); diff --git a/packages/realm_dart/test/indexed_test.realm.dart b/packages/realm_dart/test/indexed_test.realm.dart index b1ee9d3f4..754df49f2 100644 --- a/packages/realm_dart/test/indexed_test.realm.dart +++ b/packages/realm_dart/test/indexed_test.realm.dart @@ -85,6 +85,7 @@ class WithIndexes extends _WithIndexes static EJsonValue _toEJson(WithIndexes value) => value.toEJson(); static WithIndexes _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'anInt': EJsonValue anInt, @@ -109,7 +110,8 @@ class WithIndexes extends _WithIndexes static final schema = () { RealmObjectBase.registerFactory(WithIndexes._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, WithIndexes, 'WithIndexes', [ + return const SchemaObject( + ObjectType.realmObject, WithIndexes, 'WithIndexes', [ SchemaProperty('anInt', RealmPropertyType.int, indexType: RealmIndexType.regular), SchemaProperty('aBool', RealmPropertyType.bool, @@ -206,6 +208,7 @@ class NoIndexes extends _NoIndexes static EJsonValue _toEJson(NoIndexes value) => value.toEJson(); static NoIndexes _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'anInt': EJsonValue anInt, @@ -230,7 +233,7 @@ class NoIndexes extends _NoIndexes static final schema = () { RealmObjectBase.registerFactory(NoIndexes._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, NoIndexes, 'NoIndexes', [ + return const SchemaObject(ObjectType.realmObject, NoIndexes, 'NoIndexes', [ SchemaProperty('anInt', RealmPropertyType.int), SchemaProperty('aBool', RealmPropertyType.bool), SchemaProperty('string', RealmPropertyType.string), @@ -298,16 +301,16 @@ class ObjectWithFTSIndex extends _ObjectWithFTSIndex static EJsonValue _toEJson(ObjectWithFTSIndex value) => value.toEJson(); static ObjectWithFTSIndex _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'title': EJsonValue title, 'summary': EJsonValue summary, - 'nullableSummary': EJsonValue nullableSummary, } => ObjectWithFTSIndex( fromEJson(title), fromEJson(summary), - nullableSummary: fromEJson(nullableSummary), + nullableSummary: fromEJson(ejson['nullableSummary']), ), _ => raiseInvalidEJson(ejson), }; @@ -316,7 +319,7 @@ class ObjectWithFTSIndex extends _ObjectWithFTSIndex static final schema = () { RealmObjectBase.registerFactory(ObjectWithFTSIndex._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, ObjectWithFTSIndex, 'ObjectWithFTSIndex', [ SchemaProperty('title', RealmPropertyType.string), SchemaProperty('summary', RealmPropertyType.string, diff --git a/packages/realm_dart/test/migration_test.realm.dart b/packages/realm_dart/test/migration_test.realm.dart index 8673710e7..449d0cdbc 100644 --- a/packages/realm_dart/test/migration_test.realm.dart +++ b/packages/realm_dart/test/migration_test.realm.dart @@ -42,6 +42,7 @@ class PersonIntName extends _PersonIntName static EJsonValue _toEJson(PersonIntName value) => value.toEJson(); static PersonIntName _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, @@ -56,7 +57,7 @@ class PersonIntName extends _PersonIntName static final schema = () { RealmObjectBase.registerFactory(PersonIntName._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, PersonIntName, 'Person', [ + return const SchemaObject(ObjectType.realmObject, PersonIntName, 'Person', [ SchemaProperty('name', RealmPropertyType.int), ]); }(); @@ -108,14 +109,14 @@ class StudentV1 extends _StudentV1 static EJsonValue _toEJson(StudentV1 value) => value.toEJson(); static StudentV1 _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, - 'yearOfBirth': EJsonValue yearOfBirth, } => StudentV1( fromEJson(name), - yearOfBirth: fromEJson(yearOfBirth), + yearOfBirth: fromEJson(ejson['yearOfBirth']), ), _ => raiseInvalidEJson(ejson), }; @@ -124,7 +125,7 @@ class StudentV1 extends _StudentV1 static final schema = () { RealmObjectBase.registerFactory(StudentV1._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, StudentV1, 'Student', [ + return const SchemaObject(ObjectType.realmObject, StudentV1, 'Student', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('yearOfBirth', RealmPropertyType.int, optional: true), ]); @@ -178,6 +179,7 @@ class MyObjectWithTypo extends _MyObjectWithTypo static EJsonValue _toEJson(MyObjectWithTypo value) => value.toEJson(); static MyObjectWithTypo _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'nmae': EJsonValue nmae, @@ -194,7 +196,8 @@ class MyObjectWithTypo extends _MyObjectWithTypo static final schema = () { RealmObjectBase.registerFactory(MyObjectWithTypo._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, MyObjectWithTypo, 'MyObject', [ + return const SchemaObject( + ObjectType.realmObject, MyObjectWithTypo, 'MyObject', [ SchemaProperty('nmae', RealmPropertyType.string), SchemaProperty('vlaue', RealmPropertyType.int), ]); @@ -248,6 +251,7 @@ class MyObjectWithoutTypo extends _MyObjectWithoutTypo static EJsonValue _toEJson(MyObjectWithoutTypo value) => value.toEJson(); static MyObjectWithoutTypo _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, @@ -264,7 +268,7 @@ class MyObjectWithoutTypo extends _MyObjectWithoutTypo static final schema = () { RealmObjectBase.registerFactory(MyObjectWithoutTypo._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, MyObjectWithoutTypo, 'MyObject', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('value', RealmPropertyType.int), @@ -311,6 +315,7 @@ class MyObjectWithoutValue extends _MyObjectWithoutValue static EJsonValue _toEJson(MyObjectWithoutValue value) => value.toEJson(); static MyObjectWithoutValue _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, @@ -325,7 +330,7 @@ class MyObjectWithoutValue extends _MyObjectWithoutValue static final schema = () { RealmObjectBase.registerFactory(MyObjectWithoutValue._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, MyObjectWithoutValue, 'MyObject', [ SchemaProperty('name', RealmPropertyType.string), ]); diff --git a/packages/realm_dart/test/realm_map_test.realm.dart b/packages/realm_dart/test/realm_map_test.realm.dart index 928119434..e591110b2 100644 --- a/packages/realm_dart/test/realm_map_test.realm.dart +++ b/packages/realm_dart/test/realm_map_test.realm.dart @@ -56,16 +56,15 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Car value) => value.toEJson(); static Car _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'make': EJsonValue make, - 'color': EJsonValue color, - 'year': EJsonValue year, } => Car( fromEJson(make), - color: fromEJson(color), - year: fromEJson(year), + color: fromEJson(ejson['color']), + year: fromEJson(ejson['year']), ), _ => raiseInvalidEJson(ejson), }; @@ -74,7 +73,7 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Car._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Car, 'Car', [ + return const SchemaObject(ObjectType.realmObject, Car, 'Car', [ SchemaProperty('make', RealmPropertyType.string, primaryKey: true), SchemaProperty('color', RealmPropertyType.string, optional: true), SchemaProperty('year', RealmPropertyType.int, optional: true), @@ -120,6 +119,7 @@ class EmbeddedValue extends _EmbeddedValue static EJsonValue _toEJson(EmbeddedValue value) => value.toEJson(); static EmbeddedValue _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'intValue': EJsonValue intValue, @@ -134,7 +134,7 @@ class EmbeddedValue extends _EmbeddedValue static final schema = () { RealmObjectBase.registerFactory(EmbeddedValue._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.embeddedObject, EmbeddedValue, 'EmbeddedValue', [ SchemaProperty('intValue', RealmPropertyType.int), ]); @@ -416,54 +416,34 @@ class TestRealmMaps extends _TestRealmMaps static EJsonValue _toEJson(TestRealmMaps value) => value.toEJson(); static TestRealmMaps _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'key': EJsonValue key, - 'boolMap': EJsonValue boolMap, - 'intMap': EJsonValue intMap, - 'stringMap': EJsonValue stringMap, - 'doubleMap': EJsonValue doubleMap, - 'dateTimeMap': EJsonValue dateTimeMap, - 'objectIdMap': EJsonValue objectIdMap, - 'uuidMap': EJsonValue uuidMap, - 'binaryMap': EJsonValue binaryMap, - 'decimalMap': EJsonValue decimalMap, - 'nullableBoolMap': EJsonValue nullableBoolMap, - 'nullableIntMap': EJsonValue nullableIntMap, - 'nullableStringMap': EJsonValue nullableStringMap, - 'nullableDoubleMap': EJsonValue nullableDoubleMap, - 'nullableDateTimeMap': EJsonValue nullableDateTimeMap, - 'nullableObjectIdMap': EJsonValue nullableObjectIdMap, - 'nullableUuidMap': EJsonValue nullableUuidMap, - 'nullableBinaryMap': EJsonValue nullableBinaryMap, - 'nullableDecimalMap': EJsonValue nullableDecimalMap, - 'objectsMap': EJsonValue objectsMap, - 'embeddedMap': EJsonValue embeddedMap, - 'mixedMap': EJsonValue mixedMap, } => TestRealmMaps( fromEJson(key), - boolMap: fromEJson(boolMap), - intMap: fromEJson(intMap), - stringMap: fromEJson(stringMap), - doubleMap: fromEJson(doubleMap), - dateTimeMap: fromEJson(dateTimeMap), - objectIdMap: fromEJson(objectIdMap), - uuidMap: fromEJson(uuidMap), - binaryMap: fromEJson(binaryMap), - decimalMap: fromEJson(decimalMap), - nullableBoolMap: fromEJson(nullableBoolMap), - nullableIntMap: fromEJson(nullableIntMap), - nullableStringMap: fromEJson(nullableStringMap), - nullableDoubleMap: fromEJson(nullableDoubleMap), - nullableDateTimeMap: fromEJson(nullableDateTimeMap), - nullableObjectIdMap: fromEJson(nullableObjectIdMap), - nullableUuidMap: fromEJson(nullableUuidMap), - nullableBinaryMap: fromEJson(nullableBinaryMap), - nullableDecimalMap: fromEJson(nullableDecimalMap), - objectsMap: fromEJson(objectsMap), - embeddedMap: fromEJson(embeddedMap), - mixedMap: fromEJson(mixedMap), + boolMap: fromEJson(ejson['boolMap']), + intMap: fromEJson(ejson['intMap']), + stringMap: fromEJson(ejson['stringMap']), + doubleMap: fromEJson(ejson['doubleMap']), + dateTimeMap: fromEJson(ejson['dateTimeMap']), + objectIdMap: fromEJson(ejson['objectIdMap']), + uuidMap: fromEJson(ejson['uuidMap']), + binaryMap: fromEJson(ejson['binaryMap']), + decimalMap: fromEJson(ejson['decimalMap']), + nullableBoolMap: fromEJson(ejson['nullableBoolMap']), + nullableIntMap: fromEJson(ejson['nullableIntMap']), + nullableStringMap: fromEJson(ejson['nullableStringMap']), + nullableDoubleMap: fromEJson(ejson['nullableDoubleMap']), + nullableDateTimeMap: fromEJson(ejson['nullableDateTimeMap']), + nullableObjectIdMap: fromEJson(ejson['nullableObjectIdMap']), + nullableUuidMap: fromEJson(ejson['nullableUuidMap']), + nullableBinaryMap: fromEJson(ejson['nullableBinaryMap']), + nullableDecimalMap: fromEJson(ejson['nullableDecimalMap']), + objectsMap: fromEJson(ejson['objectsMap']), + embeddedMap: fromEJson(ejson['embeddedMap']), + mixedMap: fromEJson(ejson['mixedMap']), ), _ => raiseInvalidEJson(ejson), }; @@ -472,7 +452,7 @@ class TestRealmMaps extends _TestRealmMaps static final schema = () { RealmObjectBase.registerFactory(TestRealmMaps._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, TestRealmMaps, 'TestRealmMaps', [ SchemaProperty('key', RealmPropertyType.int, primaryKey: true), SchemaProperty('boolMap', RealmPropertyType.bool, diff --git a/packages/realm_dart/test/realm_object_test.realm.dart b/packages/realm_dart/test/realm_object_test.realm.dart index 20c88bcdd..337c52d4a 100644 --- a/packages/realm_dart/test/realm_object_test.realm.dart +++ b/packages/realm_dart/test/realm_object_test.realm.dart @@ -43,6 +43,7 @@ class ObjectIdPrimaryKey extends _ObjectIdPrimaryKey static EJsonValue _toEJson(ObjectIdPrimaryKey value) => value.toEJson(); static ObjectIdPrimaryKey _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, @@ -57,7 +58,7 @@ class ObjectIdPrimaryKey extends _ObjectIdPrimaryKey static final schema = () { RealmObjectBase.registerFactory(ObjectIdPrimaryKey._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, ObjectIdPrimaryKey, 'ObjectIdPrimaryKey', [ SchemaProperty('id', RealmPropertyType.objectid, primaryKey: true), ]); @@ -104,12 +105,13 @@ class NullableObjectIdPrimaryKey extends _NullableObjectIdPrimaryKey static EJsonValue _toEJson(NullableObjectIdPrimaryKey value) => value.toEJson(); static NullableObjectIdPrimaryKey _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, } => NullableObjectIdPrimaryKey( - fromEJson(id), + fromEJson(ejson['id']), ), _ => raiseInvalidEJson(ejson), }; @@ -118,8 +120,8 @@ class NullableObjectIdPrimaryKey extends _NullableObjectIdPrimaryKey static final schema = () { RealmObjectBase.registerFactory(NullableObjectIdPrimaryKey._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, NullableObjectIdPrimaryKey, - 'NullableObjectIdPrimaryKey', [ + return const SchemaObject(ObjectType.realmObject, + NullableObjectIdPrimaryKey, 'NullableObjectIdPrimaryKey', [ SchemaProperty('id', RealmPropertyType.objectid, optional: true, primaryKey: true), ]); @@ -164,6 +166,7 @@ class IntPrimaryKey extends _IntPrimaryKey static EJsonValue _toEJson(IntPrimaryKey value) => value.toEJson(); static IntPrimaryKey _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, @@ -178,7 +181,7 @@ class IntPrimaryKey extends _IntPrimaryKey static final schema = () { RealmObjectBase.registerFactory(IntPrimaryKey._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, IntPrimaryKey, 'IntPrimaryKey', [ SchemaProperty('id', RealmPropertyType.int, primaryKey: true), ]); @@ -224,12 +227,13 @@ class NullableIntPrimaryKey extends _NullableIntPrimaryKey static EJsonValue _toEJson(NullableIntPrimaryKey value) => value.toEJson(); static NullableIntPrimaryKey _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, } => NullableIntPrimaryKey( - fromEJson(id), + fromEJson(ejson['id']), ), _ => raiseInvalidEJson(ejson), }; @@ -238,7 +242,7 @@ class NullableIntPrimaryKey extends _NullableIntPrimaryKey static final schema = () { RealmObjectBase.registerFactory(NullableIntPrimaryKey._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, NullableIntPrimaryKey, + return const SchemaObject(ObjectType.realmObject, NullableIntPrimaryKey, 'NullableIntPrimaryKey', [ SchemaProperty('id', RealmPropertyType.int, optional: true, primaryKey: true), @@ -285,6 +289,7 @@ class StringPrimaryKey extends _StringPrimaryKey static EJsonValue _toEJson(StringPrimaryKey value) => value.toEJson(); static StringPrimaryKey _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, @@ -299,7 +304,7 @@ class StringPrimaryKey extends _StringPrimaryKey static final schema = () { RealmObjectBase.registerFactory(StringPrimaryKey._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, StringPrimaryKey, 'StringPrimaryKey', [ SchemaProperty('id', RealmPropertyType.string, primaryKey: true), ]); @@ -345,12 +350,13 @@ class NullableStringPrimaryKey extends _NullableStringPrimaryKey static EJsonValue _toEJson(NullableStringPrimaryKey value) => value.toEJson(); static NullableStringPrimaryKey _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, } => NullableStringPrimaryKey( - fromEJson(id), + fromEJson(ejson['id']), ), _ => raiseInvalidEJson(ejson), }; @@ -359,7 +365,7 @@ class NullableStringPrimaryKey extends _NullableStringPrimaryKey static final schema = () { RealmObjectBase.registerFactory(NullableStringPrimaryKey._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, NullableStringPrimaryKey, + return const SchemaObject(ObjectType.realmObject, NullableStringPrimaryKey, 'NullableStringPrimaryKey', [ SchemaProperty('id', RealmPropertyType.string, optional: true, primaryKey: true), @@ -405,6 +411,7 @@ class UuidPrimaryKey extends _UuidPrimaryKey static EJsonValue _toEJson(UuidPrimaryKey value) => value.toEJson(); static UuidPrimaryKey _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, @@ -419,7 +426,7 @@ class UuidPrimaryKey extends _UuidPrimaryKey static final schema = () { RealmObjectBase.registerFactory(UuidPrimaryKey._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, UuidPrimaryKey, 'UuidPrimaryKey', [ SchemaProperty('id', RealmPropertyType.uuid, primaryKey: true), ]); @@ -465,12 +472,13 @@ class NullableUuidPrimaryKey extends _NullableUuidPrimaryKey static EJsonValue _toEJson(NullableUuidPrimaryKey value) => value.toEJson(); static NullableUuidPrimaryKey _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, } => NullableUuidPrimaryKey( - fromEJson(id), + fromEJson(ejson['id']), ), _ => raiseInvalidEJson(ejson), }; @@ -479,7 +487,7 @@ class NullableUuidPrimaryKey extends _NullableUuidPrimaryKey static final schema = () { RealmObjectBase.registerFactory(NullableUuidPrimaryKey._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, NullableUuidPrimaryKey, + return const SchemaObject(ObjectType.realmObject, NullableUuidPrimaryKey, 'NullableUuidPrimaryKey', [ SchemaProperty('id', RealmPropertyType.uuid, optional: true, primaryKey: true), @@ -529,21 +537,16 @@ class RemappedFromAnotherFile extends _RemappedFromAnotherFile static EJsonValue _toEJson(RemappedFromAnotherFile value) => value.toEJson(); static RemappedFromAnotherFile _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'property with spaces': EJsonValue linkToAnotherClass, - } => - RemappedFromAnotherFile( - linkToAnotherClass: fromEJson(linkToAnotherClass), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return RemappedFromAnotherFile( + linkToAnotherClass: fromEJson(ejson['property with spaces']), + ); } static final schema = () { RealmObjectBase.registerFactory(RemappedFromAnotherFile._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, RemappedFromAnotherFile, 'class with spaces', [ SchemaProperty('linkToAnotherClass', RealmPropertyType.object, mapTo: 'property with spaces', @@ -598,6 +601,7 @@ class BoolValue extends _BoolValue static EJsonValue _toEJson(BoolValue value) => value.toEJson(); static BoolValue _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'key': EJsonValue key, @@ -614,7 +618,7 @@ class BoolValue extends _BoolValue static final schema = () { RealmObjectBase.registerFactory(BoolValue._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, BoolValue, 'BoolValue', [ + return const SchemaObject(ObjectType.realmObject, BoolValue, 'BoolValue', [ SchemaProperty('key', RealmPropertyType.int, primaryKey: true), SchemaProperty('value', RealmPropertyType.bool), ]); @@ -752,35 +756,23 @@ class TestNotificationObject extends _TestNotificationObject static EJsonValue _toEJson(TestNotificationObject value) => value.toEJson(); static TestNotificationObject _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'stringProperty': EJsonValue stringProperty, - 'intProperty': EJsonValue intProperty, - '_remappedIntProperty': EJsonValue remappedIntProperty, - 'link': EJsonValue link, - 'embedded': EJsonValue embedded, - 'listLinks': EJsonValue listLinks, - 'setLinks': EJsonValue setLinks, - 'mapLinks': EJsonValue mapLinks, - } => - TestNotificationObject( - stringProperty: fromEJson(stringProperty), - intProperty: fromEJson(intProperty), - remappedIntProperty: fromEJson(remappedIntProperty), - link: fromEJson(link), - embedded: fromEJson(embedded), - listLinks: fromEJson(listLinks), - setLinks: fromEJson(setLinks), - mapLinks: fromEJson(mapLinks), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return TestNotificationObject( + stringProperty: fromEJson(ejson['stringProperty']), + intProperty: fromEJson(ejson['intProperty']), + remappedIntProperty: fromEJson(ejson['_remappedIntProperty']), + link: fromEJson(ejson['link']), + embedded: fromEJson(ejson['embedded']), + listLinks: fromEJson(ejson['listLinks']), + setLinks: fromEJson(ejson['setLinks']), + mapLinks: fromEJson(ejson['mapLinks']), + ); } static final schema = () { RealmObjectBase.registerFactory(TestNotificationObject._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, TestNotificationObject, + return const SchemaObject(ObjectType.realmObject, TestNotificationObject, 'TestNotificationObject', [ SchemaProperty('stringProperty', RealmPropertyType.string, optional: true), @@ -861,23 +853,17 @@ class TestNotificationEmbeddedObject extends _TestNotificationEmbeddedObject static EJsonValue _toEJson(TestNotificationEmbeddedObject value) => value.toEJson(); static TestNotificationEmbeddedObject _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'stringProperty': EJsonValue stringProperty, - 'intProperty': EJsonValue intProperty, - } => - TestNotificationEmbeddedObject( - stringProperty: fromEJson(stringProperty), - intProperty: fromEJson(intProperty), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return TestNotificationEmbeddedObject( + stringProperty: fromEJson(ejson['stringProperty']), + intProperty: fromEJson(ejson['intProperty']), + ); } static final schema = () { RealmObjectBase.registerFactory(TestNotificationEmbeddedObject._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.embeddedObject, + return const SchemaObject(ObjectType.embeddedObject, TestNotificationEmbeddedObject, 'TestNotificationEmbeddedObject', [ SchemaProperty('stringProperty', RealmPropertyType.string, optional: true), diff --git a/packages/realm_dart/test/realm_set_test.realm.dart b/packages/realm_dart/test/realm_set_test.realm.dart index 238be7250..6338946e7 100644 --- a/packages/realm_dart/test/realm_set_test.realm.dart +++ b/packages/realm_dart/test/realm_set_test.realm.dart @@ -56,16 +56,15 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Car value) => value.toEJson(); static Car _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'make': EJsonValue make, - 'color': EJsonValue color, - 'year': EJsonValue year, } => Car( fromEJson(make), - color: fromEJson(color), - year: fromEJson(year), + color: fromEJson(ejson['color']), + year: fromEJson(ejson['year']), ), _ => raiseInvalidEJson(ejson), }; @@ -74,7 +73,7 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Car._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Car, 'Car', [ + return const SchemaObject(ObjectType.realmObject, Car, 'Car', [ SchemaProperty('make', RealmPropertyType.string, primaryKey: true), SchemaProperty('color', RealmPropertyType.string, optional: true), SchemaProperty('year', RealmPropertyType.int, optional: true), @@ -321,48 +320,31 @@ class TestRealmSets extends _TestRealmSets static EJsonValue _toEJson(TestRealmSets value) => value.toEJson(); static TestRealmSets _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'key': EJsonValue key, - 'boolSet': EJsonValue boolSet, - 'intSet': EJsonValue intSet, - 'stringSet': EJsonValue stringSet, - 'doubleSet': EJsonValue doubleSet, - 'dateTimeSet': EJsonValue dateTimeSet, - 'objectIdSet': EJsonValue objectIdSet, - 'uuidSet': EJsonValue uuidSet, - 'mixedSet': EJsonValue mixedSet, - 'objectsSet': EJsonValue objectsSet, - 'binarySet': EJsonValue binarySet, - 'nullableBoolSet': EJsonValue nullableBoolSet, - 'nullableIntSet': EJsonValue nullableIntSet, - 'nullableStringSet': EJsonValue nullableStringSet, - 'nullableDoubleSet': EJsonValue nullableDoubleSet, - 'nullableDateTimeSet': EJsonValue nullableDateTimeSet, - 'nullableObjectIdSet': EJsonValue nullableObjectIdSet, - 'nullableUuidSet': EJsonValue nullableUuidSet, - 'nullableBinarySet': EJsonValue nullableBinarySet, } => TestRealmSets( fromEJson(key), - boolSet: fromEJson(boolSet), - intSet: fromEJson(intSet), - stringSet: fromEJson(stringSet), - doubleSet: fromEJson(doubleSet), - dateTimeSet: fromEJson(dateTimeSet), - objectIdSet: fromEJson(objectIdSet), - uuidSet: fromEJson(uuidSet), - mixedSet: fromEJson(mixedSet), - objectsSet: fromEJson(objectsSet), - binarySet: fromEJson(binarySet), - nullableBoolSet: fromEJson(nullableBoolSet), - nullableIntSet: fromEJson(nullableIntSet), - nullableStringSet: fromEJson(nullableStringSet), - nullableDoubleSet: fromEJson(nullableDoubleSet), - nullableDateTimeSet: fromEJson(nullableDateTimeSet), - nullableObjectIdSet: fromEJson(nullableObjectIdSet), - nullableUuidSet: fromEJson(nullableUuidSet), - nullableBinarySet: fromEJson(nullableBinarySet), + boolSet: fromEJson(ejson['boolSet']), + intSet: fromEJson(ejson['intSet']), + stringSet: fromEJson(ejson['stringSet']), + doubleSet: fromEJson(ejson['doubleSet']), + dateTimeSet: fromEJson(ejson['dateTimeSet']), + objectIdSet: fromEJson(ejson['objectIdSet']), + uuidSet: fromEJson(ejson['uuidSet']), + mixedSet: fromEJson(ejson['mixedSet']), + objectsSet: fromEJson(ejson['objectsSet']), + binarySet: fromEJson(ejson['binarySet']), + nullableBoolSet: fromEJson(ejson['nullableBoolSet']), + nullableIntSet: fromEJson(ejson['nullableIntSet']), + nullableStringSet: fromEJson(ejson['nullableStringSet']), + nullableDoubleSet: fromEJson(ejson['nullableDoubleSet']), + nullableDateTimeSet: fromEJson(ejson['nullableDateTimeSet']), + nullableObjectIdSet: fromEJson(ejson['nullableObjectIdSet']), + nullableUuidSet: fromEJson(ejson['nullableUuidSet']), + nullableBinarySet: fromEJson(ejson['nullableBinarySet']), ), _ => raiseInvalidEJson(ejson), }; @@ -371,7 +353,7 @@ class TestRealmSets extends _TestRealmSets static final schema = () { RealmObjectBase.registerFactory(TestRealmSets._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, TestRealmSets, 'TestRealmSets', [ SchemaProperty('key', RealmPropertyType.int, primaryKey: true), SchemaProperty('boolSet', RealmPropertyType.bool, diff --git a/packages/realm_dart/test/realm_value_test.realm.dart b/packages/realm_dart/test/realm_value_test.realm.dart index e3523d6e2..fee838ddc 100644 --- a/packages/realm_dart/test/realm_value_test.realm.dart +++ b/packages/realm_dart/test/realm_value_test.realm.dart @@ -48,21 +48,16 @@ class TuckedIn extends _TuckedIn static EJsonValue _toEJson(TuckedIn value) => value.toEJson(); static TuckedIn _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'x': EJsonValue x, - } => - TuckedIn( - x: fromEJson(x), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return TuckedIn( + x: fromEJson(ejson['x'], defaultValue: 42), + ); } static final schema = () { RealmObjectBase.registerFactory(TuckedIn._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.embeddedObject, TuckedIn, 'TuckedIn', [ + return const SchemaObject(ObjectType.embeddedObject, TuckedIn, 'TuckedIn', [ SchemaProperty('x', RealmPropertyType.int), ]); }(); diff --git a/packages/realm_dart/test/results_test.realm.dart b/packages/realm_dart/test/results_test.realm.dart index 7362763db..b5329ae88 100644 --- a/packages/realm_dart/test/results_test.realm.dart +++ b/packages/realm_dart/test/results_test.realm.dart @@ -192,43 +192,27 @@ class TestNotificationObject extends _TestNotificationObject static EJsonValue _toEJson(TestNotificationObject value) => value.toEJson(); static TestNotificationObject _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'stringProperty': EJsonValue stringProperty, - 'intProperty': EJsonValue intProperty, - '_remappedIntProperty': EJsonValue remappedIntProperty, - 'link': EJsonValue link, - 'list': EJsonValue list, - 'set': EJsonValue set, - 'map': EJsonValue map, - 'linkDifferentType': EJsonValue linkDifferentType, - 'listDifferentType': EJsonValue listDifferentType, - 'setDifferentType': EJsonValue setDifferentType, - 'mapDifferentType': EJsonValue mapDifferentType, - 'embedded': EJsonValue embedded, - } => - TestNotificationObject( - stringProperty: fromEJson(stringProperty), - intProperty: fromEJson(intProperty), - remappedIntProperty: fromEJson(remappedIntProperty), - link: fromEJson(link), - list: fromEJson(list), - set: fromEJson(set), - map: fromEJson(map), - linkDifferentType: fromEJson(linkDifferentType), - listDifferentType: fromEJson(listDifferentType), - setDifferentType: fromEJson(setDifferentType), - mapDifferentType: fromEJson(mapDifferentType), - embedded: fromEJson(embedded), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return TestNotificationObject( + stringProperty: fromEJson(ejson['stringProperty']), + intProperty: fromEJson(ejson['intProperty']), + remappedIntProperty: fromEJson(ejson['_remappedIntProperty']), + link: fromEJson(ejson['link']), + list: fromEJson(ejson['list']), + set: fromEJson(ejson['set']), + map: fromEJson(ejson['map']), + linkDifferentType: fromEJson(ejson['linkDifferentType']), + listDifferentType: fromEJson(ejson['listDifferentType']), + setDifferentType: fromEJson(ejson['setDifferentType']), + mapDifferentType: fromEJson(ejson['mapDifferentType']), + embedded: fromEJson(ejson['embedded']), + ); } static final schema = () { RealmObjectBase.registerFactory(TestNotificationObject._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, TestNotificationObject, + return const SchemaObject(ObjectType.realmObject, TestNotificationObject, 'TestNotificationObject', [ SchemaProperty('stringProperty', RealmPropertyType.string, optional: true), @@ -321,23 +305,17 @@ class TestNotificationEmbeddedObject extends _TestNotificationEmbeddedObject static EJsonValue _toEJson(TestNotificationEmbeddedObject value) => value.toEJson(); static TestNotificationEmbeddedObject _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'stringProperty': EJsonValue stringProperty, - 'intProperty': EJsonValue intProperty, - } => - TestNotificationEmbeddedObject( - stringProperty: fromEJson(stringProperty), - intProperty: fromEJson(intProperty), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return TestNotificationEmbeddedObject( + stringProperty: fromEJson(ejson['stringProperty']), + intProperty: fromEJson(ejson['intProperty']), + ); } static final schema = () { RealmObjectBase.registerFactory(TestNotificationEmbeddedObject._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.embeddedObject, + return const SchemaObject(ObjectType.embeddedObject, TestNotificationEmbeddedObject, 'TestNotificationEmbeddedObject', [ SchemaProperty('stringProperty', RealmPropertyType.string, optional: true), @@ -409,26 +387,19 @@ class TestNotificationDifferentType extends _TestNotificationDifferentType static EJsonValue _toEJson(TestNotificationDifferentType value) => value.toEJson(); static TestNotificationDifferentType _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'stringProperty': EJsonValue stringProperty, - 'intProperty': EJsonValue intProperty, - 'link': EJsonValue link, - } => - TestNotificationDifferentType( - stringProperty: fromEJson(stringProperty), - intProperty: fromEJson(intProperty), - link: fromEJson(link), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return TestNotificationDifferentType( + stringProperty: fromEJson(ejson['stringProperty']), + intProperty: fromEJson(ejson['intProperty']), + link: fromEJson(ejson['link']), + ); } static final schema = () { RealmObjectBase.registerFactory(TestNotificationDifferentType._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, TestNotificationDifferentType, - 'TestNotificationDifferentType', [ + return const SchemaObject(ObjectType.realmObject, + TestNotificationDifferentType, 'TestNotificationDifferentType', [ SchemaProperty('stringProperty', RealmPropertyType.string, optional: true), SchemaProperty('intProperty', RealmPropertyType.int, optional: true), diff --git a/packages/realm_dart/test/serialization_test.dart b/packages/realm_dart/test/serialization_test.dart new file mode 100644 index 000000000..901d07bd5 --- /dev/null +++ b/packages/realm_dart/test/serialization_test.dart @@ -0,0 +1,106 @@ +// Copyright 2024 MongoDB, Inc. +// SPDX-License-Identifier: Apache-2.0 + +import 'package:realm_dart/realm.dart'; + +import 'test.dart'; + +void main() { + setupTests(); + + test('optional absent', () { + // load codecs + Realm(Configuration.inMemory([Player.schema, Game.schema])); + expect(() => fromEJson({}), throwsA(isA())); + final p = fromEJson({'name': 'Christian Eriksen'}); + expect(p.toEJson(), {'name': 'Christian Eriksen', 'game': null, 'scoresByRound': []}); + }); + + group('RealmValue', () { + // load custom codecs for Player and Game. This is done to test that it + // doesn't interfere with the RealmValue codecs. + Realm(Configuration.inMemory([Player.schema, Game.schema])); + for (final entry in { + 42: {'\$numberInt': '42'}, + 42.0: {'\$numberInt': '42'}, + '42': '42', + true: true, + null: null, + [1, 2, 3]: [ + {'\$numberInt': '1'}, + {'\$numberInt': '2'}, + {'\$numberInt': '3'}, + ], + {'a': 1, 'b': 2}: { + // Even though Game could be deserialized from this (everything in Game + // is optional, hence {} and by extension any map can deserialize to Game), + // we always decode a map as a map inside a RealmValue. + 'a': {'\$numberInt': '1'}, + 'b': {'\$numberInt': '2'}, + }, + }.entries) { + final value = entry.key; + final encoded = entry.value; + test(value.runtimeType.toString(), () { + final r = RealmValue.from(value); + expect(r.toEJson(), encoded); + expect(fromEJson(encoded).value, r.value); + if (r.value is! List && r.value is! Map) { + expect(fromEJson(r.toEJson()), r); + } + }); + } + + test('RealmObject', () { + // load custom codecs for Player and Game. This is done to test that it + // doesn't interfere with the RealmValue codecs. + Realm(Configuration.inMemory([Player.schema, Game.schema])); + + final p = Player('Christian Eriksen'); + final rv = RealmValue.from(p); + expect(rv.toEJson(), {'\$id': 'Christian Eriksen', '\$ref': 'Player'}); + expect(fromEJson>(rv.toEJson()), isA>().having((r) => r.id, '\$id', 'Christian Eriksen')); + expect((fromEJson(rv.toEJson()).value as Player).name, p.name); + }); + }); + + test('Decimal128', () { + final d = Decimal128.fromInt(42); + expect(toEJson(d), {'\$numberDecimal': '+42E+0'}); + expect(fromEJson({'\$numberDecimal': '42'}), d); + }); + + test('Set on RealmObject', () { + final oid = ObjectId(); + final realm = Realm(Configuration.inMemory([ObjectWithRealmValue.schema])); + final o = realm.write(() => realm.add(ObjectWithRealmValue(oid, setOfAny: {RealmValue.from(42), RealmValue.from('42')}))); + expect(o.setOfAny, {RealmValue.from(42), RealmValue.from('42')}); + final serialized = toEJsonString(o); + expect(serialized, '{"_id":{"\$oid":"$oid"},"differentiator":null,"oneAny":null,"manyAny":[],"dictOfAny":{},"setOfAny":[{"\$numberInt":"42"},"42"]}'); + final deserialized = fromEJsonString(serialized); + // deserialized is unmanaged, so will never compare equal, but we can test properties + expect(deserialized.id, o.id); + expect(deserialized.setOfAny, o.setOfAny); + }); + + test('Set on RealmObject', () { + final realm = Realm(Configuration.inMemory([AllCollections.schema])); + final o = realm.write(() => realm.add(AllCollections(intSet: {1, 2, 3}))); + expect(o.intSet, {1, 2, 3}); + final deserialized = fromEJsonString(toEJsonString(o)); + // deserialized is unmanaged, so will never compare equal, but we can test properties + expect(deserialized.intSet, o.intSet); + }); + + test('Set in RealmValue', () { + final rv = RealmValue.from({1, 2, 3}); // constructed from a list of ints + expect(rv.value, [RealmValue.from(1), RealmValue.from(2), RealmValue.from(3)]); // but becomes a list RealmValues + expect(rv.toEJson(), [ + // and serializes as a list of EJson + {'\$numberInt': '1'}, + {'\$numberInt': '2'}, + {'\$numberInt': '3'}, + ]); + // expect(rv.as>(), {1, 2, 3}); // doesn't work because the set is a list of RealmValues. Should we support this? + }); +} diff --git a/packages/realm_dart/test/sync_migration_test.realm.dart b/packages/realm_dart/test/sync_migration_test.realm.dart index fddf772f5..40e397929 100644 --- a/packages/realm_dart/test/sync_migration_test.realm.dart +++ b/packages/realm_dart/test/sync_migration_test.realm.dart @@ -136,32 +136,24 @@ class NullablesV0 extends _NullablesV0 static EJsonValue _toEJson(NullablesV0 value) => value.toEJson(); static NullablesV0 _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_id': EJsonValue id, 'differentiator': EJsonValue differentiator, - 'boolValue': EJsonValue boolValue, - 'intValue': EJsonValue intValue, - 'doubleValue': EJsonValue doubleValue, - 'decimalValue': EJsonValue decimalValue, - 'dateValue': EJsonValue dateValue, - 'stringValue': EJsonValue stringValue, - 'objectIdValue': EJsonValue objectIdValue, - 'uuidValue': EJsonValue uuidValue, - 'binaryValue': EJsonValue binaryValue, } => NullablesV0( fromEJson(id), fromEJson(differentiator), - boolValue: fromEJson(boolValue), - intValue: fromEJson(intValue), - doubleValue: fromEJson(doubleValue), - decimalValue: fromEJson(decimalValue), - dateValue: fromEJson(dateValue), - stringValue: fromEJson(stringValue), - objectIdValue: fromEJson(objectIdValue), - uuidValue: fromEJson(uuidValue), - binaryValue: fromEJson(binaryValue), + boolValue: fromEJson(ejson['boolValue']), + intValue: fromEJson(ejson['intValue']), + doubleValue: fromEJson(ejson['doubleValue']), + decimalValue: fromEJson(ejson['decimalValue']), + dateValue: fromEJson(ejson['dateValue']), + stringValue: fromEJson(ejson['stringValue']), + objectIdValue: fromEJson(ejson['objectIdValue']), + uuidValue: fromEJson(ejson['uuidValue']), + binaryValue: fromEJson(ejson['binaryValue']), ), _ => raiseInvalidEJson(ejson), }; @@ -170,7 +162,8 @@ class NullablesV0 extends _NullablesV0 static final schema = () { RealmObjectBase.registerFactory(NullablesV0._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, NullablesV0, 'Nullables', [ + return const SchemaObject( + ObjectType.realmObject, NullablesV0, 'Nullables', [ SchemaProperty('id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('differentiator', RealmPropertyType.objectid), @@ -331,6 +324,7 @@ class NullablesV1 extends _NullablesV1 static EJsonValue _toEJson(NullablesV1 value) => value.toEJson(); static NullablesV1 _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_id': EJsonValue id, @@ -367,7 +361,8 @@ class NullablesV1 extends _NullablesV1 static final schema = () { RealmObjectBase.registerFactory(NullablesV1._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, NullablesV1, 'Nullables', [ + return const SchemaObject( + ObjectType.realmObject, NullablesV1, 'Nullables', [ SchemaProperty('id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('differentiator', RealmPropertyType.objectid), diff --git a/packages/realm_dart/test/test.realm.dart b/packages/realm_dart/test/test.realm.dart index a4f300f2a..4c71abe5c 100644 --- a/packages/realm_dart/test/test.realm.dart +++ b/packages/realm_dart/test/test.realm.dart @@ -40,6 +40,7 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Car value) => value.toEJson(); static Car _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'make': EJsonValue make, @@ -54,7 +55,7 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Car._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Car, 'Car', [ + return const SchemaObject(ObjectType.realmObject, Car, 'Car', [ SchemaProperty('make', RealmPropertyType.string, primaryKey: true), ]); }(); @@ -96,6 +97,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Person value) => value.toEJson(); static Person _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, @@ -110,7 +112,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Person._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string), ]); }(); @@ -169,16 +171,15 @@ class Dog extends _Dog with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Dog value) => value.toEJson(); static Dog _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, - 'age': EJsonValue age, - 'owner': EJsonValue owner, } => Dog( fromEJson(name), - age: fromEJson(age), - owner: fromEJson(owner), + age: fromEJson(ejson['age']), + owner: fromEJson(ejson['owner']), ), _ => raiseInvalidEJson(ejson), }; @@ -187,7 +188,7 @@ class Dog extends _Dog with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Dog._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, 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, @@ -253,16 +254,15 @@ class Team extends _Team with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Team value) => value.toEJson(); static Team _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, - 'players': EJsonValue players, - 'scores': EJsonValue scores, } => Team( fromEJson(name), - players: fromEJson(players), - scores: fromEJson(scores), + players: fromEJson(ejson['players']), + scores: fromEJson(ejson['scores']), ), _ => raiseInvalidEJson(ejson), }; @@ -271,7 +271,7 @@ class Team extends _Team with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Team._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Team, 'Team', [ + return const SchemaObject(ObjectType.realmObject, Team, 'Team', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('players', RealmPropertyType.object, linkTarget: 'Person', collectionType: RealmCollectionType.list), @@ -343,18 +343,16 @@ class Student extends _Student with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Student value) => value.toEJson(); static Student _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'number': EJsonValue number, - 'name': EJsonValue name, - 'yearOfBirth': EJsonValue yearOfBirth, - 'school': EJsonValue school, } => Student( fromEJson(number), - name: fromEJson(name), - yearOfBirth: fromEJson(yearOfBirth), - school: fromEJson(school), + name: fromEJson(ejson['name']), + yearOfBirth: fromEJson(ejson['yearOfBirth']), + school: fromEJson(ejson['school']), ), _ => raiseInvalidEJson(ejson), }; @@ -363,7 +361,7 @@ class Student extends _Student with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Student._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, 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), @@ -449,20 +447,17 @@ class School extends _School with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(School value) => value.toEJson(); static School _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, - 'city': EJsonValue city, - 'students': EJsonValue students, - 'branchOfSchool': EJsonValue branchOfSchool, - 'branches': EJsonValue branches, } => School( fromEJson(name), - city: fromEJson(city), - students: fromEJson(students), - branchOfSchool: fromEJson(branchOfSchool), - branches: fromEJson(branches), + city: fromEJson(ejson['city']), + students: fromEJson(ejson['students'], defaultValue: const []), + branchOfSchool: fromEJson(ejson['branchOfSchool']), + branches: fromEJson(ejson['branches']), ), _ => raiseInvalidEJson(ejson), }; @@ -471,7 +466,7 @@ class School extends _School with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(School._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, 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, @@ -536,14 +531,14 @@ class RemappedClass extends $RemappedClass static EJsonValue _toEJson(RemappedClass value) => value.toEJson(); static RemappedClass _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'primitive_property': EJsonValue remappedProperty, - 'list-with-dashes': EJsonValue listProperty, } => RemappedClass( fromEJson(remappedProperty), - listProperty: fromEJson(listProperty), + listProperty: fromEJson(ejson['list-with-dashes']), ), _ => raiseInvalidEJson(ejson), }; @@ -552,7 +547,7 @@ class RemappedClass extends $RemappedClass static final schema = () { RealmObjectBase.registerFactory(RemappedClass._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, RemappedClass, 'myRemappedClass', [ SchemaProperty('remappedProperty', RealmPropertyType.string, mapTo: 'primitive_property'), @@ -600,6 +595,7 @@ class Task extends _Task with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Task value) => value.toEJson(); static Task _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_id': EJsonValue id, @@ -614,7 +610,7 @@ class Task extends _Task with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Task._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Task, 'Task', [ + return const SchemaObject(ObjectType.realmObject, Task, 'Task', [ SchemaProperty('id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), ]); @@ -667,6 +663,7 @@ class Product extends _Product with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Product value) => value.toEJson(); static Product _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_id': EJsonValue id, @@ -683,7 +680,7 @@ class Product extends _Product with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Product._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Product, 'Product', [ + return const SchemaObject(ObjectType.realmObject, Product, 'Product', [ SchemaProperty('id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('name', RealmPropertyType.string, @@ -739,14 +736,14 @@ class Schedule extends _Schedule static EJsonValue _toEJson(Schedule value) => value.toEJson(); static Schedule _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_id': EJsonValue id, - 'tasks': EJsonValue tasks, } => Schedule( fromEJson(id), - tasks: fromEJson(tasks), + tasks: fromEJson(ejson['tasks'], defaultValue: const []), ), _ => raiseInvalidEJson(ejson), }; @@ -755,7 +752,7 @@ class Schedule extends _Schedule static final schema = () { RealmObjectBase.registerFactory(Schedule._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Schedule, 'Schedule', [ + return const SchemaObject(ObjectType.realmObject, Schedule, 'Schedule', [ SchemaProperty('id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('tasks', RealmPropertyType.object, @@ -812,14 +809,14 @@ class Foo extends _Foo with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Foo value) => value.toEJson(); static Foo _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'requiredBinaryProp': EJsonValue requiredBinaryProp, - 'nullableBinaryProp': EJsonValue nullableBinaryProp, } => Foo( fromEJson(requiredBinaryProp), - nullableBinaryProp: fromEJson(nullableBinaryProp), + nullableBinaryProp: fromEJson(ejson['nullableBinaryProp']), ), _ => raiseInvalidEJson(ejson), }; @@ -828,7 +825,7 @@ class Foo extends _Foo with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Foo._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Foo, 'Foo', [ + return const SchemaObject(ObjectType.realmObject, Foo, 'Foo', [ SchemaProperty('requiredBinaryProp', RealmPropertyType.binary), SchemaProperty('nullableBinaryProp', RealmPropertyType.binary, optional: true), @@ -1049,6 +1046,7 @@ class AllTypes extends _AllTypes static EJsonValue _toEJson(AllTypes value) => value.toEJson(); static AllTypes _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'stringProp': EJsonValue stringProp, @@ -1060,16 +1058,6 @@ class AllTypes extends _AllTypes 'intProp': EJsonValue intProp, 'decimalProp': EJsonValue decimalProp, 'binaryProp': EJsonValue binaryProp, - 'nullableStringProp': EJsonValue nullableStringProp, - 'nullableBoolProp': EJsonValue nullableBoolProp, - 'nullableDateProp': EJsonValue nullableDateProp, - 'nullableDoubleProp': EJsonValue nullableDoubleProp, - 'nullableObjectIdProp': EJsonValue nullableObjectIdProp, - 'nullableUuidProp': EJsonValue nullableUuidProp, - 'nullableIntProp': EJsonValue nullableIntProp, - 'nullableDecimalProp': EJsonValue nullableDecimalProp, - 'nullableBinaryProp': EJsonValue nullableBinaryProp, - 'realmValueProp': EJsonValue realmValueProp, } => AllTypes( fromEJson(stringProp), @@ -1081,16 +1069,16 @@ class AllTypes extends _AllTypes fromEJson(intProp), fromEJson(decimalProp), fromEJson(binaryProp), - nullableStringProp: fromEJson(nullableStringProp), - nullableBoolProp: fromEJson(nullableBoolProp), - nullableDateProp: fromEJson(nullableDateProp), - nullableDoubleProp: fromEJson(nullableDoubleProp), - nullableObjectIdProp: fromEJson(nullableObjectIdProp), - nullableUuidProp: fromEJson(nullableUuidProp), - nullableIntProp: fromEJson(nullableIntProp), - nullableDecimalProp: fromEJson(nullableDecimalProp), - nullableBinaryProp: fromEJson(nullableBinaryProp), - realmValueProp: fromEJson(realmValueProp), + nullableStringProp: fromEJson(ejson['nullableStringProp']), + nullableBoolProp: fromEJson(ejson['nullableBoolProp']), + nullableDateProp: fromEJson(ejson['nullableDateProp']), + nullableDoubleProp: fromEJson(ejson['nullableDoubleProp']), + nullableObjectIdProp: fromEJson(ejson['nullableObjectIdProp']), + nullableUuidProp: fromEJson(ejson['nullableUuidProp']), + nullableIntProp: fromEJson(ejson['nullableIntProp']), + nullableDecimalProp: fromEJson(ejson['nullableDecimalProp']), + nullableBinaryProp: fromEJson(ejson['nullableBinaryProp']), + realmValueProp: fromEJson(ejson['realmValueProp']), ), _ => raiseInvalidEJson(ejson), }; @@ -1099,7 +1087,7 @@ class AllTypes extends _AllTypes static final schema = () { RealmObjectBase.registerFactory(AllTypes._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, AllTypes, 'AllTypes', [ + return const SchemaObject(ObjectType.realmObject, AllTypes, 'AllTypes', [ SchemaProperty('stringProp', RealmPropertyType.string), SchemaProperty('boolProp', RealmPropertyType.bool), SchemaProperty('dateProp', RealmPropertyType.timestamp), @@ -1211,20 +1199,17 @@ class LinksClass extends _LinksClass static EJsonValue _toEJson(LinksClass value) => value.toEJson(); static LinksClass _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, - 'link': EJsonValue link, - 'list': EJsonValue list, - 'linksSet': EJsonValue linksSet, - 'map': EJsonValue map, } => LinksClass( fromEJson(id), - link: fromEJson(link), - list: fromEJson(list), - linksSet: fromEJson(linksSet), - map: fromEJson(map), + link: fromEJson(ejson['link']), + list: fromEJson(ejson['list']), + linksSet: fromEJson(ejson['linksSet']), + map: fromEJson(ejson['map']), ), _ => raiseInvalidEJson(ejson), }; @@ -1233,7 +1218,8 @@ class LinksClass extends _LinksClass static final schema = () { RealmObjectBase.registerFactory(LinksClass._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, LinksClass, 'LinksClass', [ + return const SchemaObject( + ObjectType.realmObject, LinksClass, 'LinksClass', [ SchemaProperty('id', RealmPropertyType.uuid, primaryKey: true), SchemaProperty('link', RealmPropertyType.object, optional: true, linkTarget: 'LinksClass'), @@ -1822,115 +1808,63 @@ class AllCollections extends _AllCollections static EJsonValue _toEJson(AllCollections value) => value.toEJson(); static AllCollections _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'stringList': EJsonValue stringList, - 'boolList': EJsonValue boolList, - 'dateList': EJsonValue dateList, - 'doubleList': EJsonValue doubleList, - 'objectIdList': EJsonValue objectIdList, - 'uuidList': EJsonValue uuidList, - 'intList': EJsonValue intList, - 'decimalList': EJsonValue decimalList, - 'nullableStringList': EJsonValue nullableStringList, - 'nullableBoolList': EJsonValue nullableBoolList, - 'nullableDateList': EJsonValue nullableDateList, - 'nullableDoubleList': EJsonValue nullableDoubleList, - 'nullableObjectIdList': EJsonValue nullableObjectIdList, - 'nullableUuidList': EJsonValue nullableUuidList, - 'nullableIntList': EJsonValue nullableIntList, - 'nullableDecimalList': EJsonValue nullableDecimalList, - 'stringSet': EJsonValue stringSet, - 'boolSet': EJsonValue boolSet, - 'dateSet': EJsonValue dateSet, - 'doubleSet': EJsonValue doubleSet, - 'objectIdSet': EJsonValue objectIdSet, - 'uuidSet': EJsonValue uuidSet, - 'intSet': EJsonValue intSet, - 'decimalSet': EJsonValue decimalSet, - 'nullableStringSet': EJsonValue nullableStringSet, - 'nullableBoolSet': EJsonValue nullableBoolSet, - 'nullableDateSet': EJsonValue nullableDateSet, - 'nullableDoubleSet': EJsonValue nullableDoubleSet, - 'nullableObjectIdSet': EJsonValue nullableObjectIdSet, - 'nullableUuidSet': EJsonValue nullableUuidSet, - 'nullableIntSet': EJsonValue nullableIntSet, - 'nullableDecimalSet': EJsonValue nullableDecimalSet, - 'stringMap': EJsonValue stringMap, - 'boolMap': EJsonValue boolMap, - 'dateMap': EJsonValue dateMap, - 'doubleMap': EJsonValue doubleMap, - 'objectIdMap': EJsonValue objectIdMap, - 'uuidMap': EJsonValue uuidMap, - 'intMap': EJsonValue intMap, - 'decimalMap': EJsonValue decimalMap, - 'nullableStringMap': EJsonValue nullableStringMap, - 'nullableBoolMap': EJsonValue nullableBoolMap, - 'nullableDateMap': EJsonValue nullableDateMap, - 'nullableDoubleMap': EJsonValue nullableDoubleMap, - 'nullableObjectIdMap': EJsonValue nullableObjectIdMap, - 'nullableUuidMap': EJsonValue nullableUuidMap, - 'nullableIntMap': EJsonValue nullableIntMap, - 'nullableDecimalMap': EJsonValue nullableDecimalMap, - } => - AllCollections( - stringList: fromEJson(stringList), - boolList: fromEJson(boolList), - dateList: fromEJson(dateList), - doubleList: fromEJson(doubleList), - objectIdList: fromEJson(objectIdList), - uuidList: fromEJson(uuidList), - intList: fromEJson(intList), - decimalList: fromEJson(decimalList), - nullableStringList: fromEJson(nullableStringList), - nullableBoolList: fromEJson(nullableBoolList), - nullableDateList: fromEJson(nullableDateList), - nullableDoubleList: fromEJson(nullableDoubleList), - nullableObjectIdList: fromEJson(nullableObjectIdList), - nullableUuidList: fromEJson(nullableUuidList), - nullableIntList: fromEJson(nullableIntList), - nullableDecimalList: fromEJson(nullableDecimalList), - stringSet: fromEJson(stringSet), - boolSet: fromEJson(boolSet), - dateSet: fromEJson(dateSet), - doubleSet: fromEJson(doubleSet), - objectIdSet: fromEJson(objectIdSet), - uuidSet: fromEJson(uuidSet), - intSet: fromEJson(intSet), - decimalSet: fromEJson(decimalSet), - nullableStringSet: fromEJson(nullableStringSet), - nullableBoolSet: fromEJson(nullableBoolSet), - nullableDateSet: fromEJson(nullableDateSet), - nullableDoubleSet: fromEJson(nullableDoubleSet), - nullableObjectIdSet: fromEJson(nullableObjectIdSet), - nullableUuidSet: fromEJson(nullableUuidSet), - nullableIntSet: fromEJson(nullableIntSet), - nullableDecimalSet: fromEJson(nullableDecimalSet), - stringMap: fromEJson(stringMap), - boolMap: fromEJson(boolMap), - dateMap: fromEJson(dateMap), - doubleMap: fromEJson(doubleMap), - objectIdMap: fromEJson(objectIdMap), - uuidMap: fromEJson(uuidMap), - intMap: fromEJson(intMap), - decimalMap: fromEJson(decimalMap), - nullableStringMap: fromEJson(nullableStringMap), - nullableBoolMap: fromEJson(nullableBoolMap), - nullableDateMap: fromEJson(nullableDateMap), - nullableDoubleMap: fromEJson(nullableDoubleMap), - nullableObjectIdMap: fromEJson(nullableObjectIdMap), - nullableUuidMap: fromEJson(nullableUuidMap), - nullableIntMap: fromEJson(nullableIntMap), - nullableDecimalMap: fromEJson(nullableDecimalMap), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return AllCollections( + stringList: fromEJson(ejson['stringList']), + boolList: fromEJson(ejson['boolList']), + dateList: fromEJson(ejson['dateList']), + doubleList: fromEJson(ejson['doubleList']), + objectIdList: fromEJson(ejson['objectIdList']), + uuidList: fromEJson(ejson['uuidList']), + intList: fromEJson(ejson['intList']), + decimalList: fromEJson(ejson['decimalList']), + nullableStringList: fromEJson(ejson['nullableStringList']), + nullableBoolList: fromEJson(ejson['nullableBoolList']), + nullableDateList: fromEJson(ejson['nullableDateList']), + nullableDoubleList: fromEJson(ejson['nullableDoubleList']), + nullableObjectIdList: fromEJson(ejson['nullableObjectIdList']), + nullableUuidList: fromEJson(ejson['nullableUuidList']), + nullableIntList: fromEJson(ejson['nullableIntList']), + nullableDecimalList: fromEJson(ejson['nullableDecimalList']), + stringSet: fromEJson(ejson['stringSet']), + boolSet: fromEJson(ejson['boolSet']), + dateSet: fromEJson(ejson['dateSet']), + doubleSet: fromEJson(ejson['doubleSet']), + objectIdSet: fromEJson(ejson['objectIdSet']), + uuidSet: fromEJson(ejson['uuidSet']), + intSet: fromEJson(ejson['intSet']), + decimalSet: fromEJson(ejson['decimalSet']), + nullableStringSet: fromEJson(ejson['nullableStringSet']), + nullableBoolSet: fromEJson(ejson['nullableBoolSet']), + nullableDateSet: fromEJson(ejson['nullableDateSet']), + nullableDoubleSet: fromEJson(ejson['nullableDoubleSet']), + nullableObjectIdSet: fromEJson(ejson['nullableObjectIdSet']), + nullableUuidSet: fromEJson(ejson['nullableUuidSet']), + nullableIntSet: fromEJson(ejson['nullableIntSet']), + nullableDecimalSet: fromEJson(ejson['nullableDecimalSet']), + stringMap: fromEJson(ejson['stringMap']), + boolMap: fromEJson(ejson['boolMap']), + dateMap: fromEJson(ejson['dateMap']), + doubleMap: fromEJson(ejson['doubleMap']), + objectIdMap: fromEJson(ejson['objectIdMap']), + uuidMap: fromEJson(ejson['uuidMap']), + intMap: fromEJson(ejson['intMap']), + decimalMap: fromEJson(ejson['decimalMap']), + nullableStringMap: fromEJson(ejson['nullableStringMap']), + nullableBoolMap: fromEJson(ejson['nullableBoolMap']), + nullableDateMap: fromEJson(ejson['nullableDateMap']), + nullableDoubleMap: fromEJson(ejson['nullableDoubleMap']), + nullableObjectIdMap: fromEJson(ejson['nullableObjectIdMap']), + nullableUuidMap: fromEJson(ejson['nullableUuidMap']), + nullableIntMap: fromEJson(ejson['nullableIntMap']), + nullableDecimalMap: fromEJson(ejson['nullableDecimalMap']), + ); } static final schema = () { RealmObjectBase.registerFactory(AllCollections._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, AllCollections, 'AllCollections', [ SchemaProperty('stringList', RealmPropertyType.string, collectionType: RealmCollectionType.list), @@ -2153,30 +2087,23 @@ class NullableTypes extends _NullableTypes static EJsonValue _toEJson(NullableTypes value) => value.toEJson(); static NullableTypes _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_id': EJsonValue id, 'differentiator': EJsonValue differentiator, - 'stringProp': EJsonValue stringProp, - 'boolProp': EJsonValue boolProp, - 'dateProp': EJsonValue dateProp, - 'doubleProp': EJsonValue doubleProp, - 'objectIdProp': EJsonValue objectIdProp, - 'uuidProp': EJsonValue uuidProp, - 'intProp': EJsonValue intProp, - 'decimalProp': EJsonValue decimalProp, } => NullableTypes( fromEJson(id), fromEJson(differentiator), - stringProp: fromEJson(stringProp), - boolProp: fromEJson(boolProp), - dateProp: fromEJson(dateProp), - doubleProp: fromEJson(doubleProp), - objectIdProp: fromEJson(objectIdProp), - uuidProp: fromEJson(uuidProp), - intProp: fromEJson(intProp), - decimalProp: fromEJson(decimalProp), + stringProp: fromEJson(ejson['stringProp']), + boolProp: fromEJson(ejson['boolProp']), + dateProp: fromEJson(ejson['dateProp']), + doubleProp: fromEJson(ejson['doubleProp']), + objectIdProp: fromEJson(ejson['objectIdProp']), + uuidProp: fromEJson(ejson['uuidProp']), + intProp: fromEJson(ejson['intProp']), + decimalProp: fromEJson(ejson['decimalProp']), ), _ => raiseInvalidEJson(ejson), }; @@ -2185,7 +2112,7 @@ class NullableTypes extends _NullableTypes static final schema = () { RealmObjectBase.registerFactory(NullableTypes._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, NullableTypes, 'NullableTypes', [ SchemaProperty('id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), @@ -2280,20 +2207,17 @@ class Event extends _Event with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Event value) => value.toEJson(); static Event _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_id': EJsonValue id, - 'stringQueryField': EJsonValue name, - 'boolQueryField': EJsonValue isCompleted, - 'intQueryField': EJsonValue durationInMinutes, - 'assignedTo': EJsonValue assignedTo, } => Event( fromEJson(id), - name: fromEJson(name), - isCompleted: fromEJson(isCompleted), - durationInMinutes: fromEJson(durationInMinutes), - assignedTo: fromEJson(assignedTo), + name: fromEJson(ejson['stringQueryField']), + isCompleted: fromEJson(ejson['boolQueryField']), + durationInMinutes: fromEJson(ejson['intQueryField']), + assignedTo: fromEJson(ejson['assignedTo']), ), _ => raiseInvalidEJson(ejson), }; @@ -2302,7 +2226,7 @@ class Event extends _Event with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Event._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Event, 'Event', [ + return const SchemaObject(ObjectType.realmObject, Event, 'Event', [ SchemaProperty('id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('name', RealmPropertyType.string, @@ -2380,18 +2304,16 @@ class Party extends _Party with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Party value) => value.toEJson(); static Party _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { - 'host': EJsonValue host, 'year': EJsonValue year, - 'guests': EJsonValue guests, - 'previous': EJsonValue previous, } => Party( fromEJson(year), - host: fromEJson(host), - guests: fromEJson(guests), - previous: fromEJson(previous), + host: fromEJson(ejson['host']), + guests: fromEJson(ejson['guests'], defaultValue: const []), + previous: fromEJson(ejson['previous']), ), _ => raiseInvalidEJson(ejson), }; @@ -2400,7 +2322,7 @@ class Party extends _Party with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Party._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Party, 'Party', [ + return const SchemaObject(ObjectType.realmObject, Party, 'Party', [ SchemaProperty('host', RealmPropertyType.object, optional: true, linkTarget: 'Friend'), SchemaProperty('year', RealmPropertyType.int), @@ -2484,18 +2406,16 @@ class Friend extends _Friend with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Friend value) => value.toEJson(); static Friend _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, - 'age': EJsonValue age, - 'bestFriend': EJsonValue bestFriend, - 'friends': EJsonValue friends, } => Friend( fromEJson(name), - age: fromEJson(age), - bestFriend: fromEJson(bestFriend), - friends: fromEJson(friends), + age: fromEJson(ejson['age'], defaultValue: 42), + bestFriend: fromEJson(ejson['bestFriend']), + friends: fromEJson(ejson['friends'], defaultValue: const []), ), _ => raiseInvalidEJson(ejson), }; @@ -2504,7 +2424,7 @@ class Friend extends _Friend with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Friend._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Friend, 'Friend', [ + return const SchemaObject(ObjectType.realmObject, Friend, 'Friend', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('age', RealmPropertyType.int), SchemaProperty('bestFriend', RealmPropertyType.object, @@ -2563,6 +2483,7 @@ class When extends _When with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(When value) => value.toEJson(); static When _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'dateTimeUtc': EJsonValue dateTimeUtc, @@ -2579,7 +2500,7 @@ class When extends _When with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(When._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, When, 'When', [ + return const SchemaObject(ObjectType.realmObject, When, 'When', [ SchemaProperty('dateTimeUtc', RealmPropertyType.timestamp), SchemaProperty('locationName', RealmPropertyType.string), ]); @@ -2641,16 +2562,16 @@ class Player extends _Player with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Player value) => value.toEJson(); static Player _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, - 'game': EJsonValue game, - 'scoresByRound': EJsonValue scoresByRound, } => Player( fromEJson(name), - game: fromEJson(game), - scoresByRound: fromEJson(scoresByRound), + game: fromEJson(ejson['game']), + scoresByRound: + fromEJson(ejson['scoresByRound'], defaultValue: const []), ), _ => raiseInvalidEJson(ejson), }; @@ -2659,7 +2580,7 @@ class Player extends _Player with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Player._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Player, 'Player', [ + return const SchemaObject(ObjectType.realmObject, Player, 'Player', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('game', RealmPropertyType.object, optional: true, linkTarget: 'Game'), @@ -2708,21 +2629,16 @@ class Game extends _Game with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Game value) => value.toEJson(); static Game _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'winnerByRound': EJsonValue winnerByRound, - } => - Game( - winnerByRound: fromEJson(winnerByRound), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return Game( + winnerByRound: fromEJson(ejson['winnerByRound'], defaultValue: const []), + ); } static final schema = () { RealmObjectBase.registerFactory(Game._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Game, 'Game', [ + return const SchemaObject(ObjectType.realmObject, Game, 'Game', [ SchemaProperty('winnerByRound', RealmPropertyType.object, linkTarget: 'Player', collectionType: RealmCollectionType.list), ]); @@ -2999,6 +2915,7 @@ class AllTypesEmbedded extends _AllTypesEmbedded static EJsonValue _toEJson(AllTypesEmbedded value) => value.toEJson(); static AllTypesEmbedded _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'stringProp': EJsonValue stringProp, @@ -3009,22 +2926,6 @@ class AllTypesEmbedded extends _AllTypesEmbedded 'uuidProp': EJsonValue uuidProp, 'intProp': EJsonValue intProp, 'decimalProp': EJsonValue decimalProp, - 'nullableStringProp': EJsonValue nullableStringProp, - 'nullableBoolProp': EJsonValue nullableBoolProp, - 'nullableDateProp': EJsonValue nullableDateProp, - 'nullableDoubleProp': EJsonValue nullableDoubleProp, - 'nullableObjectIdProp': EJsonValue nullableObjectIdProp, - 'nullableUuidProp': EJsonValue nullableUuidProp, - 'nullableIntProp': EJsonValue nullableIntProp, - 'nullableDecimalProp': EJsonValue nullableDecimalProp, - 'strings': EJsonValue strings, - 'bools': EJsonValue bools, - 'dates': EJsonValue dates, - 'doubles': EJsonValue doubles, - 'objectIds': EJsonValue objectIds, - 'uuids': EJsonValue uuids, - 'ints': EJsonValue ints, - 'decimals': EJsonValue decimals, } => AllTypesEmbedded( fromEJson(stringProp), @@ -3035,22 +2936,22 @@ class AllTypesEmbedded extends _AllTypesEmbedded fromEJson(uuidProp), fromEJson(intProp), fromEJson(decimalProp), - nullableStringProp: fromEJson(nullableStringProp), - nullableBoolProp: fromEJson(nullableBoolProp), - nullableDateProp: fromEJson(nullableDateProp), - nullableDoubleProp: fromEJson(nullableDoubleProp), - nullableObjectIdProp: fromEJson(nullableObjectIdProp), - nullableUuidProp: fromEJson(nullableUuidProp), - nullableIntProp: fromEJson(nullableIntProp), - nullableDecimalProp: fromEJson(nullableDecimalProp), - strings: fromEJson(strings), - bools: fromEJson(bools), - dates: fromEJson(dates), - doubles: fromEJson(doubles), - objectIds: fromEJson(objectIds), - uuids: fromEJson(uuids), - ints: fromEJson(ints), - decimals: fromEJson(decimals), + nullableStringProp: fromEJson(ejson['nullableStringProp']), + nullableBoolProp: fromEJson(ejson['nullableBoolProp']), + nullableDateProp: fromEJson(ejson['nullableDateProp']), + nullableDoubleProp: fromEJson(ejson['nullableDoubleProp']), + nullableObjectIdProp: fromEJson(ejson['nullableObjectIdProp']), + nullableUuidProp: fromEJson(ejson['nullableUuidProp']), + nullableIntProp: fromEJson(ejson['nullableIntProp']), + nullableDecimalProp: fromEJson(ejson['nullableDecimalProp']), + strings: fromEJson(ejson['strings']), + bools: fromEJson(ejson['bools']), + dates: fromEJson(ejson['dates']), + doubles: fromEJson(ejson['doubles']), + objectIds: fromEJson(ejson['objectIds']), + uuids: fromEJson(ejson['uuids']), + ints: fromEJson(ejson['ints']), + decimals: fromEJson(ejson['decimals']), ), _ => raiseInvalidEJson(ejson), }; @@ -3059,7 +2960,7 @@ class AllTypesEmbedded extends _AllTypesEmbedded static final schema = () { RealmObjectBase.registerFactory(AllTypesEmbedded._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.embeddedObject, AllTypesEmbedded, 'AllTypesEmbedded', [ SchemaProperty('stringProp', RealmPropertyType.string), SchemaProperty('boolProp', RealmPropertyType.bool), @@ -3199,22 +3100,18 @@ class ObjectWithEmbedded extends _ObjectWithEmbedded static EJsonValue _toEJson(ObjectWithEmbedded value) => value.toEJson(); static ObjectWithEmbedded _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_id': EJsonValue id, - 'differentiator': EJsonValue differentiator, - 'singleObject': EJsonValue singleObject, - 'list': EJsonValue list, - 'recursiveObject': EJsonValue recursiveObject, - 'recursiveList': EJsonValue recursiveList, } => ObjectWithEmbedded( fromEJson(id), - differentiator: fromEJson(differentiator), - singleObject: fromEJson(singleObject), - list: fromEJson(list), - recursiveObject: fromEJson(recursiveObject), - recursiveList: fromEJson(recursiveList), + differentiator: fromEJson(ejson['differentiator']), + singleObject: fromEJson(ejson['singleObject']), + list: fromEJson(ejson['list']), + recursiveObject: fromEJson(ejson['recursiveObject']), + recursiveList: fromEJson(ejson['recursiveList']), ), _ => raiseInvalidEJson(ejson), }; @@ -3223,7 +3120,7 @@ class ObjectWithEmbedded extends _ObjectWithEmbedded static final schema = () { RealmObjectBase.registerFactory(ObjectWithEmbedded._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, ObjectWithEmbedded, 'ObjectWithEmbedded', [ SchemaProperty('id', RealmPropertyType.string, mapTo: '_id', primaryKey: true), @@ -3315,18 +3212,16 @@ class RecursiveEmbedded1 extends _RecursiveEmbedded1 static EJsonValue _toEJson(RecursiveEmbedded1 value) => value.toEJson(); static RecursiveEmbedded1 _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'value': EJsonValue value, - 'child': EJsonValue child, - 'children': EJsonValue children, - 'realmObject': EJsonValue realmObject, } => RecursiveEmbedded1( fromEJson(value), - child: fromEJson(child), - children: fromEJson(children), - realmObject: fromEJson(realmObject), + child: fromEJson(ejson['child']), + children: fromEJson(ejson['children']), + realmObject: fromEJson(ejson['realmObject']), ), _ => raiseInvalidEJson(ejson), }; @@ -3335,7 +3230,7 @@ class RecursiveEmbedded1 extends _RecursiveEmbedded1 static final schema = () { RealmObjectBase.registerFactory(RecursiveEmbedded1._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.embeddedObject, RecursiveEmbedded1, 'RecursiveEmbedded1', [ SchemaProperty('value', RealmPropertyType.string), SchemaProperty('child', RealmPropertyType.object, @@ -3422,18 +3317,16 @@ class RecursiveEmbedded2 extends _RecursiveEmbedded2 static EJsonValue _toEJson(RecursiveEmbedded2 value) => value.toEJson(); static RecursiveEmbedded2 _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'value': EJsonValue value, - 'child': EJsonValue child, - 'children': EJsonValue children, - 'realmObject': EJsonValue realmObject, } => RecursiveEmbedded2( fromEJson(value), - child: fromEJson(child), - children: fromEJson(children), - realmObject: fromEJson(realmObject), + child: fromEJson(ejson['child']), + children: fromEJson(ejson['children']), + realmObject: fromEJson(ejson['realmObject']), ), _ => raiseInvalidEJson(ejson), }; @@ -3442,7 +3335,7 @@ class RecursiveEmbedded2 extends _RecursiveEmbedded2 static final schema = () { RealmObjectBase.registerFactory(RecursiveEmbedded2._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.embeddedObject, RecursiveEmbedded2, 'RecursiveEmbedded2', [ SchemaProperty('value', RealmPropertyType.string), SchemaProperty('child', RealmPropertyType.object, @@ -3495,6 +3388,7 @@ class RecursiveEmbedded3 extends _RecursiveEmbedded3 static EJsonValue _toEJson(RecursiveEmbedded3 value) => value.toEJson(); static RecursiveEmbedded3 _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'value': EJsonValue value, @@ -3509,7 +3403,7 @@ class RecursiveEmbedded3 extends _RecursiveEmbedded3 static final schema = () { RealmObjectBase.registerFactory(RecursiveEmbedded3._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.embeddedObject, RecursiveEmbedded3, 'RecursiveEmbedded3', [ SchemaProperty('value', RealmPropertyType.string), ]); @@ -3566,14 +3460,14 @@ class ObjectWithDecimal extends _ObjectWithDecimal static EJsonValue _toEJson(ObjectWithDecimal value) => value.toEJson(); static ObjectWithDecimal _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'decimal': EJsonValue decimal, - 'nullableDecimal': EJsonValue nullableDecimal, } => ObjectWithDecimal( fromEJson(decimal), - nullableDecimal: fromEJson(nullableDecimal), + nullableDecimal: fromEJson(ejson['nullableDecimal']), ), _ => raiseInvalidEJson(ejson), }; @@ -3582,7 +3476,7 @@ class ObjectWithDecimal extends _ObjectWithDecimal static final schema = () { RealmObjectBase.registerFactory(ObjectWithDecimal._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, ObjectWithDecimal, 'ObjectWithDecimal', [ SchemaProperty('decimal', RealmPropertyType.decimal128), SchemaProperty('nullableDecimal', RealmPropertyType.decimal128, @@ -3650,16 +3544,15 @@ class Asymmetric extends _Asymmetric static EJsonValue _toEJson(Asymmetric value) => value.toEJson(); static Asymmetric _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_id': EJsonValue id, - 'symmetric': EJsonValue symmetric, - 'embeddedObjects': EJsonValue embeddedObjects, } => Asymmetric( fromEJson(id), - symmetric: fromEJson(symmetric), - embeddedObjects: fromEJson(embeddedObjects), + symmetric: fromEJson(ejson['symmetric']), + embeddedObjects: fromEJson(ejson['embeddedObjects']), ), _ => raiseInvalidEJson(ejson), }; @@ -3668,7 +3561,8 @@ class Asymmetric extends _Asymmetric static final schema = () { RealmObjectBase.registerFactory(Asymmetric._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.asymmetricObject, Asymmetric, 'Asymmetric', [ + return const SchemaObject( + ObjectType.asymmetricObject, Asymmetric, 'Asymmetric', [ SchemaProperty('id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('symmetric', RealmPropertyType.object, @@ -3735,16 +3629,15 @@ class Embedded extends _Embedded static EJsonValue _toEJson(Embedded value) => value.toEJson(); static Embedded _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'value': EJsonValue value, - 'any': EJsonValue any, - 'symmetric': EJsonValue symmetric, } => Embedded( fromEJson(value), - any: fromEJson(any), - symmetric: fromEJson(symmetric), + any: fromEJson(ejson['any']), + symmetric: fromEJson(ejson['symmetric']), ), _ => raiseInvalidEJson(ejson), }; @@ -3753,7 +3646,7 @@ class Embedded extends _Embedded static final schema = () { RealmObjectBase.registerFactory(Embedded._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.embeddedObject, Embedded, 'Embedded', [ + return const SchemaObject(ObjectType.embeddedObject, Embedded, 'Embedded', [ SchemaProperty('value', RealmPropertyType.int), SchemaProperty('any', RealmPropertyType.mixed, optional: true), SchemaProperty('symmetric', RealmPropertyType.object, @@ -3799,6 +3692,7 @@ class Symmetric extends _Symmetric static EJsonValue _toEJson(Symmetric value) => value.toEJson(); static Symmetric _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_id': EJsonValue id, @@ -3813,7 +3707,7 @@ class Symmetric extends _Symmetric static final schema = () { RealmObjectBase.registerFactory(Symmetric._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Symmetric, 'Symmetric', [ + return const SchemaObject(ObjectType.realmObject, Symmetric, 'Symmetric', [ SchemaProperty('id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), ]); @@ -3912,22 +3806,18 @@ class ObjectWithRealmValue extends _ObjectWithRealmValue static EJsonValue _toEJson(ObjectWithRealmValue value) => value.toEJson(); static ObjectWithRealmValue _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_id': EJsonValue id, - 'differentiator': EJsonValue differentiator, - 'oneAny': EJsonValue oneAny, - 'manyAny': EJsonValue manyAny, - 'dictOfAny': EJsonValue dictOfAny, - 'setOfAny': EJsonValue setOfAny, } => ObjectWithRealmValue( fromEJson(id), - differentiator: fromEJson(differentiator), - oneAny: fromEJson(oneAny), - manyAny: fromEJson(manyAny), - dictOfAny: fromEJson(dictOfAny), - setOfAny: fromEJson(setOfAny), + differentiator: fromEJson(ejson['differentiator']), + oneAny: fromEJson(ejson['oneAny']), + manyAny: fromEJson(ejson['manyAny']), + dictOfAny: fromEJson(ejson['dictOfAny']), + setOfAny: fromEJson(ejson['setOfAny']), ), _ => raiseInvalidEJson(ejson), }; @@ -3936,7 +3826,7 @@ class ObjectWithRealmValue extends _ObjectWithRealmValue static final schema = () { RealmObjectBase.registerFactory(ObjectWithRealmValue._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, ObjectWithRealmValue, 'ObjectWithRealmValue', [ SchemaProperty('id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), @@ -4017,16 +3907,15 @@ class ObjectWithInt extends _ObjectWithInt static EJsonValue _toEJson(ObjectWithInt value) => value.toEJson(); static ObjectWithInt _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_id': EJsonValue id, - 'differentiator': EJsonValue differentiator, - 'i': EJsonValue i, } => ObjectWithInt( fromEJson(id), - differentiator: fromEJson(differentiator), - i: fromEJson(i), + differentiator: fromEJson(ejson['differentiator']), + i: fromEJson(ejson['i'], defaultValue: 42), ), _ => raiseInvalidEJson(ejson), }; @@ -4035,7 +3924,7 @@ class ObjectWithInt extends _ObjectWithInt static final schema = () { RealmObjectBase.registerFactory(ObjectWithInt._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, ObjectWithInt, 'ObjectWithInt', [ SchemaProperty('id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), diff --git a/packages/realm_generator/lib/src/realm_field_info.dart b/packages/realm_generator/lib/src/realm_field_info.dart index 2ad0ae4e6..2e99e4aa7 100644 --- a/packages/realm_generator/lib/src/realm_field_info.dart +++ b/packages/realm_generator/lib/src/realm_field_info.dart @@ -57,12 +57,17 @@ class RealmFieldInfo { String get mappedTypeName => fieldElement.mappedTypeName; String get initializer { - if (type.realmCollectionType == RealmCollectionType.list) return ' = const []'; - if (type.realmCollectionType == RealmCollectionType.set) return ' = const {}'; - if (type.realmCollectionType == RealmCollectionType.map) return ' = const {}'; - if (isMixed) return ' = const RealmValue.nullValue()'; - if (hasDefaultValue) return ' = ${fieldElement.initializerExpression}'; - return ''; // no initializer + final v = defaultValue; + return v == null ? '' : ' = $v'; + } + + String? get defaultValue { + if (type.realmCollectionType == RealmCollectionType.list) return 'const []'; + if (type.realmCollectionType == RealmCollectionType.set) return 'const {}'; + if (type.realmCollectionType == RealmCollectionType.map) return 'const {}'; + if (isMixed) return 'const RealmValue.nullValue()'; + if (hasDefaultValue) return '${fieldElement.initializerExpression}'; + return null; // no default value } RealmCollectionType get realmCollectionType => type.realmCollectionType; diff --git a/packages/realm_generator/lib/src/realm_model_info.dart b/packages/realm_generator/lib/src/realm_model_info.dart index 6e9e3963a..38d6509c6 100644 --- a/packages/realm_generator/lib/src/realm_model_info.dart +++ b/packages/realm_generator/lib/src/realm_model_info.dart @@ -135,23 +135,37 @@ class RealmModelInfo { // Decode yield 'static $name _fromEJson(EJsonValue ejson) {'; { - yield 'return switch (ejson) {'; - { - yield '{'; - { - yield* allSettable.map((f) { - return "'${f.realmName}': EJsonValue ${f.name},"; - }); - } - yield '} => $name('; + yield 'if (ejson is! Map) return raiseInvalidEJson(ejson);'; + final shape = allSettable.where(required); + if (shape.isEmpty) { + yield 'return '; + } else { + yield 'return switch (ejson) {'; { - yield* positional.map((f) => 'fromEJson(${f.name}),'); - yield* named.map((f) => '${paramName(f)}: fromEJson(${f.name}),'); + yield '{'; + { + yield* shape.map((f) { + return "'${f.realmName}': EJsonValue ${f.name},"; + }); + } + yield '} =>'; } - yield '),'; + } + yield '$name('; + { + getter(RealmFieldInfo f) => f.isRequired ? f.name : "ejson['${f.realmName}']"; + fromEJson(RealmFieldInfo f) => 'fromEJson(${getter(f)}${f.hasDefaultValue ? ', defaultValue: ${f.defaultValue}' : ''})'; + yield* positional.map((f) => '${fromEJson(f)},'); + yield* named.map((f) => '${paramName(f)}: ${fromEJson(f)},'); + } + yield ')'; + if (shape.isEmpty) { + yield ';'; + } else { + yield ','; yield '_ => raiseInvalidEJson(ejson),'; + yield '};'; } - yield '};'; } yield '}'; @@ -160,7 +174,7 @@ class RealmModelInfo { { yield 'RealmObjectBase.registerFactory($name._);'; yield 'register(_toEJson, _fromEJson);'; - yield "return SchemaObject(ObjectType.${baseType.name}, $name, '$realmName', ["; + yield "return const SchemaObject(ObjectType.${baseType.name}, $name, '$realmName', ["; { yield* fields.map((f) { final namedArgs = { diff --git a/packages/realm_generator/test/good_test_data/all_named_ctor.expected b/packages/realm_generator/test/good_test_data/all_named_ctor.expected index 295a2722f..6127fcfe5 100644 --- a/packages/realm_generator/test/good_test_data/all_named_ctor.expected +++ b/packages/realm_generator/test/good_test_data/all_named_ctor.expected @@ -55,14 +55,14 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Person value) => value.toEJson(); static Person _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, - 'age': EJsonValue age, } => Person( name: fromEJson(name), - age: fromEJson(age), + age: fromEJson(ejson['age'], defaultValue: 42), ), _ => raiseInvalidEJson(ejson), }; @@ -71,7 +71,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Person._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('age', RealmPropertyType.int), ]); diff --git a/packages/realm_generator/test/good_test_data/all_types.expected b/packages/realm_generator/test/good_test_data/all_types.expected index c32e4a538..abe41e131 100644 --- a/packages/realm_generator/test/good_test_data/all_types.expected +++ b/packages/realm_generator/test/good_test_data/all_types.expected @@ -55,23 +55,17 @@ class Foo extends _Foo with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Foo value) => value.toEJson(); static Foo _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'x': EJsonValue x, - 'bar': EJsonValue bar, - } => - Foo( - x: fromEJson(x), - bar: fromEJson(bar), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return Foo( + x: fromEJson(ejson['x'], defaultValue: 0), + bar: fromEJson(ejson['bar']), + ); } static final schema = () { RealmObjectBase.registerFactory(Foo._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Foo, 'MyFoo', [ + return const SchemaObject(ObjectType.realmObject, Foo, 'MyFoo', [ SchemaProperty('x', RealmPropertyType.int, indexType: RealmIndexType.regular), SchemaProperty('bar', RealmPropertyType.object, @@ -269,6 +263,7 @@ class Bar extends _Bar with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Bar value) => value.toEJson(); static Bar _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, @@ -276,16 +271,8 @@ class Bar extends _Bar with RealmEntity, RealmObjectBase, RealmObject { 'another': EJsonValue another, 'data': EJsonValue data, 'tidspunkt': EJsonValue timestamp, - 'aDouble': EJsonValue aDouble, - 'foo': EJsonValue foo, 'objectId': EJsonValue objectId, 'uuid': EJsonValue uuid, - 'list': EJsonValue list, - 'set': EJsonValue set, - 'map': EJsonValue map, - 'anOptionalString': EJsonValue anOptionalString, - 'any': EJsonValue any, - 'manyAny': EJsonValue manyAny, 'decimal': EJsonValue decimal, } => Bar( @@ -297,14 +284,14 @@ class Bar extends _Bar with RealmEntity, RealmObjectBase, RealmObject { fromEJson(objectId), fromEJson(uuid), fromEJson(decimal), - aDouble: fromEJson(aDouble), - foo: fromEJson(foo), - list: fromEJson(list), - set: fromEJson(set), - map: fromEJson(map), - anOptionalString: fromEJson(anOptionalString), - any: fromEJson(any), - manyAny: fromEJson(manyAny), + aDouble: fromEJson(ejson['aDouble'], defaultValue: 0.0), + foo: fromEJson(ejson['foo']), + list: fromEJson(ejson['list']), + set: fromEJson(ejson['set']), + map: fromEJson(ejson['map']), + anOptionalString: fromEJson(ejson['anOptionalString']), + any: fromEJson(ejson['any']), + manyAny: fromEJson(ejson['manyAny']), ), _ => raiseInvalidEJson(ejson), }; @@ -313,7 +300,7 @@ class Bar extends _Bar with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Bar._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Bar, 'Bar', [ + return const SchemaObject(ObjectType.realmObject, Bar, 'Bar', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), SchemaProperty('aBool', RealmPropertyType.bool, indexType: RealmIndexType.regular), @@ -426,6 +413,7 @@ class PrimitiveTypes extends _PrimitiveTypes static EJsonValue _toEJson(PrimitiveTypes value) => value.toEJson(); static PrimitiveTypes _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'stringProp': EJsonValue stringProp, @@ -448,7 +436,7 @@ class PrimitiveTypes extends _PrimitiveTypes static final schema = () { RealmObjectBase.registerFactory(PrimitiveTypes._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, PrimitiveTypes, 'PrimitiveTypes', [ SchemaProperty('stringProp', RealmPropertyType.string), SchemaProperty('boolProp', RealmPropertyType.bool), diff --git a/packages/realm_generator/test/good_test_data/another_mapto.expected_multi b/packages/realm_generator/test/good_test_data/another_mapto.expected_multi index 62b443a45..847df9270 100644 --- a/packages/realm_generator/test/good_test_data/another_mapto.expected_multi +++ b/packages/realm_generator/test/good_test_data/another_mapto.expected_multi @@ -54,23 +54,17 @@ class MappedToo extends _MappedToo static EJsonValue _toEJson(MappedToo value) => value.toEJson(); static MappedToo _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'singleLink': EJsonValue singleLink, - 'listLink': EJsonValue listLink, - } => - MappedToo( - singleLink: fromEJson(singleLink), - listLink: fromEJson(listLink), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return MappedToo( + singleLink: fromEJson(ejson['singleLink']), + listLink: fromEJson(ejson['listLink']), + ); } static final schema = () { RealmObjectBase.registerFactory(MappedToo._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, MappedToo, 'this is also mapped', [ SchemaProperty('singleLink', RealmPropertyType.object, optional: true, linkTarget: 'another type'), diff --git a/packages/realm_generator/test/good_test_data/asymmetric_object.expected b/packages/realm_generator/test/good_test_data/asymmetric_object.expected index 14f3de6fd..8f20a3342 100644 --- a/packages/realm_generator/test/good_test_data/asymmetric_object.expected +++ b/packages/realm_generator/test/good_test_data/asymmetric_object.expected @@ -72,18 +72,16 @@ class Asymmetric extends _Asymmetric static EJsonValue _toEJson(Asymmetric value) => value.toEJson(); static Asymmetric _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_id': EJsonValue id, - 'children': EJsonValue children, - 'father': EJsonValue father, - 'mother': EJsonValue mother, } => Asymmetric( fromEJson(id), - children: fromEJson(children), - father: fromEJson(father), - mother: fromEJson(mother), + children: fromEJson(ejson['children']), + father: fromEJson(ejson['father']), + mother: fromEJson(ejson['mother']), ), _ => raiseInvalidEJson(ejson), }; @@ -92,7 +90,7 @@ class Asymmetric extends _Asymmetric static final schema = () { RealmObjectBase.registerFactory(Asymmetric._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Asymmetric, 'Asymmetric', [ + return const SchemaObject(ObjectType.realmObject, Asymmetric, 'Asymmetric', [ SchemaProperty('id', RealmPropertyType.objectid, mapTo: '_id', primaryKey: true), SchemaProperty('children', RealmPropertyType.object, @@ -150,6 +148,7 @@ class Embedded extends _Embedded static EJsonValue _toEJson(Embedded value) => value.toEJson(); static Embedded _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, @@ -166,7 +165,7 @@ class Embedded extends _Embedded static final schema = () { RealmObjectBase.registerFactory(Embedded._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.embeddedObject, Embedded, 'Embedded', [ + return const SchemaObject(ObjectType.embeddedObject, Embedded, 'Embedded', [ SchemaProperty('name', RealmPropertyType.string), SchemaProperty('age', RealmPropertyType.int), ]); diff --git a/packages/realm_generator/test/good_test_data/binary_type.expected b/packages/realm_generator/test/good_test_data/binary_type.expected index 7fc9f2a0d..bdc946d52 100644 --- a/packages/realm_generator/test/good_test_data/binary_type.expected +++ b/packages/realm_generator/test/good_test_data/binary_type.expected @@ -52,14 +52,14 @@ class Foo extends _Foo with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Foo value) => value.toEJson(); static Foo _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'requiredBinaryProp': EJsonValue requiredBinaryProp, - 'nullableBinaryProp': EJsonValue nullableBinaryProp, } => Foo( fromEJson(requiredBinaryProp), - nullableBinaryProp: fromEJson(nullableBinaryProp), + nullableBinaryProp: fromEJson(ejson['nullableBinaryProp']), ), _ => raiseInvalidEJson(ejson), }; @@ -68,7 +68,7 @@ class Foo extends _Foo with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Foo._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Foo, 'Foo', [ + return const SchemaObject(ObjectType.realmObject, Foo, 'Foo', [ SchemaProperty('requiredBinaryProp', RealmPropertyType.binary), SchemaProperty('nullableBinaryProp', RealmPropertyType.binary, optional: true), diff --git a/packages/realm_generator/test/good_test_data/const_initializer.expected b/packages/realm_generator/test/good_test_data/const_initializer.expected index 2f040fea8..a8e701304 100644 --- a/packages/realm_generator/test/good_test_data/const_initializer.expected +++ b/packages/realm_generator/test/good_test_data/const_initializer.expected @@ -220,55 +220,37 @@ class ConstInitializer extends _ConstInitializer static EJsonValue _toEJson(ConstInitializer value) => value.toEJson(); static ConstInitializer _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'zero': EJsonValue zero, - 'minusOne': EJsonValue minusOne, - 'fooOrOne': EJsonValue fooOrOne, - 'parenthesis': EJsonValue parenthesis, - 'minusMinusOne': EJsonValue minusMinusOne, - 'add': EJsonValue add, - 'identifier': EJsonValue identifier, - 'infinity': EJsonValue infinity, - 'nan': EJsonValue nan, - 'negativeInfinity': EJsonValue negativeInfinity, - 'fooEnv': EJsonValue fooEnv, - 'fooLit': EJsonValue fooLit, - 'constEmptyList': EJsonValue constEmptyList, - 'constEmptyMap': EJsonValue constEmptyMap, - 'constEmptySet': EJsonValue constEmptySet, - 'emptyList': EJsonValue emptyList, - 'emptyMao': EJsonValue emptyMao, - 'emptySet': EJsonValue emptySet, - } => - ConstInitializer( - zero: fromEJson(zero), - minusOne: fromEJson(minusOne), - fooOrOne: fromEJson(fooOrOne), - parenthesis: fromEJson(parenthesis), - minusMinusOne: fromEJson(minusMinusOne), - add: fromEJson(add), - identifier: fromEJson(identifier), - infinity: fromEJson(infinity), - nan: fromEJson(nan), - negativeInfinity: fromEJson(negativeInfinity), - fooEnv: fromEJson(fooEnv), - fooLit: fromEJson(fooLit), - constEmptyList: fromEJson(constEmptyList), - constEmptyMap: fromEJson(constEmptyMap), - constEmptySet: fromEJson(constEmptySet), - emptyList: fromEJson(emptyList), - emptyMao: fromEJson(emptyMao), - emptySet: fromEJson(emptySet), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return ConstInitializer( + zero: fromEJson(ejson['zero'], defaultValue: 0), + minusOne: fromEJson(ejson['minusOne'], defaultValue: -1), + fooOrOne: fromEJson(ejson['fooOrOne'], + defaultValue: const int.fromEnvironment('FOO', defaultValue: 1)), + parenthesis: fromEJson(ejson['parenthesis'], defaultValue: (1)), + minusMinusOne: fromEJson(ejson['minusMinusOne'], defaultValue: -(-1)), + add: fromEJson(ejson['add'], defaultValue: 1 + 1), + identifier: fromEJson(ejson['identifier'], defaultValue: myConst), + infinity: fromEJson(ejson['infinity'], defaultValue: double.infinity), + nan: fromEJson(ejson['nan'], defaultValue: double.nan), + negativeInfinity: fromEJson(ejson['negativeInfinity'], + defaultValue: double.negativeInfinity), + fooEnv: fromEJson(ejson['fooEnv'], + defaultValue: const String.fromEnvironment('FOO')), + fooLit: fromEJson(ejson['fooLit'], defaultValue: 'foo'), + constEmptyList: + fromEJson(ejson['constEmptyList'], defaultValue: const []), + constEmptyMap: fromEJson(ejson['constEmptyMap'], defaultValue: const {}), + constEmptySet: fromEJson(ejson['constEmptySet'], defaultValue: const {}), + emptyList: fromEJson(ejson['emptyList'], defaultValue: const []), + emptyMao: fromEJson(ejson['emptyMao'], defaultValue: const {}), + emptySet: fromEJson(ejson['emptySet'], defaultValue: const {}), + ); } static final schema = () { RealmObjectBase.registerFactory(ConstInitializer._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, ConstInitializer, 'ConstInitializer', [ SchemaProperty('zero', RealmPropertyType.int), SchemaProperty('minusOne', RealmPropertyType.int), diff --git a/packages/realm_generator/test/good_test_data/embedded_annotations.expected b/packages/realm_generator/test/good_test_data/embedded_annotations.expected index bf7c4cc85..05f07ecb3 100644 --- a/packages/realm_generator/test/good_test_data/embedded_annotations.expected +++ b/packages/realm_generator/test/good_test_data/embedded_annotations.expected @@ -53,23 +53,17 @@ class Parent extends _Parent with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Parent value) => value.toEJson(); static Parent _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'single child': EJsonValue child, - 'CHILDREN': EJsonValue children, - } => - Parent( - child: fromEJson(child), - children: fromEJson(children), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return Parent( + child: fromEJson(ejson['single child']), + children: fromEJson(ejson['CHILDREN']), + ); } static final schema = () { RealmObjectBase.registerFactory(Parent._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Parent, 'Parent', [ + return const SchemaObject(ObjectType.realmObject, Parent, 'Parent', [ SchemaProperty('child', RealmPropertyType.object, mapTo: 'single child', optional: true, linkTarget: 'MySuperChild'), SchemaProperty('children', RealmPropertyType.object, @@ -136,16 +130,16 @@ class Child1 extends _Child1 with RealmEntity, RealmObjectBase, EmbeddedObject { static EJsonValue _toEJson(Child1 value) => value.toEJson(); static Child1 _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_value': EJsonValue value, - '_parent': EJsonValue linkToParent, 'indexedString': EJsonValue indexedString, } => Child1( fromEJson(value), fromEJson(indexedString), - linkToParent: fromEJson(linkToParent), + linkToParent: fromEJson(ejson['_parent']), ), _ => raiseInvalidEJson(ejson), }; @@ -154,7 +148,7 @@ class Child1 extends _Child1 with RealmEntity, RealmObjectBase, EmbeddedObject { static final schema = () { RealmObjectBase.registerFactory(Child1._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.embeddedObject, Child1, 'MySuperChild', [ + return const SchemaObject(ObjectType.embeddedObject, Child1, 'MySuperChild', [ SchemaProperty('value', RealmPropertyType.string, mapTo: '_value'), SchemaProperty('linkToParent', RealmPropertyType.object, mapTo: '_parent', optional: true, linkTarget: 'Parent'), diff --git a/packages/realm_generator/test/good_test_data/embedded_objects.expected b/packages/realm_generator/test/good_test_data/embedded_objects.expected index 291024fdb..9430bb816 100644 --- a/packages/realm_generator/test/good_test_data/embedded_objects.expected +++ b/packages/realm_generator/test/good_test_data/embedded_objects.expected @@ -52,23 +52,17 @@ class Parent extends _Parent with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Parent value) => value.toEJson(); static Parent _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'child': EJsonValue child, - 'children': EJsonValue children, - } => - Parent( - child: fromEJson(child), - children: fromEJson(children), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return Parent( + child: fromEJson(ejson['child']), + children: fromEJson(ejson['children']), + ); } static final schema = () { RealmObjectBase.registerFactory(Parent._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Parent, 'Parent', [ + return const SchemaObject(ObjectType.realmObject, Parent, 'Parent', [ SchemaProperty('child', RealmPropertyType.object, optional: true, linkTarget: 'Child1'), SchemaProperty('children', RealmPropertyType.object, @@ -143,18 +137,16 @@ class Child1 extends _Child1 with RealmEntity, RealmObjectBase, EmbeddedObject { static EJsonValue _toEJson(Child1 value) => value.toEJson(); static Child1 _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'value': EJsonValue value, - 'child': EJsonValue child, - 'children': EJsonValue children, - 'linkToParent': EJsonValue linkToParent, } => Child1( fromEJson(value), - child: fromEJson(child), - children: fromEJson(children), - linkToParent: fromEJson(linkToParent), + child: fromEJson(ejson['child']), + children: fromEJson(ejson['children']), + linkToParent: fromEJson(ejson['linkToParent']), ), _ => raiseInvalidEJson(ejson), }; @@ -163,7 +155,7 @@ class Child1 extends _Child1 with RealmEntity, RealmObjectBase, EmbeddedObject { static final schema = () { RealmObjectBase.registerFactory(Child1._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.embeddedObject, Child1, 'Child1', [ + return const SchemaObject(ObjectType.embeddedObject, Child1, 'Child1', [ SchemaProperty('value', RealmPropertyType.string), SchemaProperty('child', RealmPropertyType.object, optional: true, linkTarget: 'Child2'), @@ -336,6 +328,7 @@ class Child2 extends _Child2 with RealmEntity, RealmObjectBase, EmbeddedObject { static EJsonValue _toEJson(Child2 value) => value.toEJson(); static Child2 _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'boolProp': EJsonValue boolProp, @@ -345,13 +338,6 @@ class Child2 extends _Child2 with RealmEntity, RealmObjectBase, EmbeddedObject { 'dateProp': EJsonValue dateProp, 'objectIdProp': EJsonValue objectIdProp, 'uuidProp': EJsonValue uuidProp, - 'nullableBoolProp': EJsonValue nullableBoolProp, - 'nullableIntProp': EJsonValue nullableIntProp, - 'nullableDoubleProp': EJsonValue nullableDoubleProp, - 'nullableStringProp': EJsonValue nullableStringProp, - 'nullableDateProp': EJsonValue nullableDateProp, - 'nullableObjectIdProp': EJsonValue nullableObjectIdProp, - 'nullableUuidProp': EJsonValue nullableUuidProp, } => Child2( fromEJson(boolProp), @@ -361,13 +347,13 @@ class Child2 extends _Child2 with RealmEntity, RealmObjectBase, EmbeddedObject { fromEJson(dateProp), fromEJson(objectIdProp), fromEJson(uuidProp), - nullableBoolProp: fromEJson(nullableBoolProp), - nullableIntProp: fromEJson(nullableIntProp), - nullableDoubleProp: fromEJson(nullableDoubleProp), - nullableStringProp: fromEJson(nullableStringProp), - nullableDateProp: fromEJson(nullableDateProp), - nullableObjectIdProp: fromEJson(nullableObjectIdProp), - nullableUuidProp: fromEJson(nullableUuidProp), + nullableBoolProp: fromEJson(ejson['nullableBoolProp']), + nullableIntProp: fromEJson(ejson['nullableIntProp']), + nullableDoubleProp: fromEJson(ejson['nullableDoubleProp']), + nullableStringProp: fromEJson(ejson['nullableStringProp']), + nullableDateProp: fromEJson(ejson['nullableDateProp']), + nullableObjectIdProp: fromEJson(ejson['nullableObjectIdProp']), + nullableUuidProp: fromEJson(ejson['nullableUuidProp']), ), _ => raiseInvalidEJson(ejson), }; @@ -376,7 +362,7 @@ class Child2 extends _Child2 with RealmEntity, RealmObjectBase, EmbeddedObject { static final schema = () { RealmObjectBase.registerFactory(Child2._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.embeddedObject, Child2, 'Child2', [ + return const SchemaObject(ObjectType.embeddedObject, Child2, 'Child2', [ SchemaProperty('boolProp', RealmPropertyType.bool), SchemaProperty('intProp', RealmPropertyType.int), SchemaProperty('doubleProp', RealmPropertyType.double), diff --git a/packages/realm_generator/test/good_test_data/indexable_types.expected b/packages/realm_generator/test/good_test_data/indexable_types.expected index 657a38fbd..48511ad84 100644 --- a/packages/realm_generator/test/good_test_data/indexable_types.expected +++ b/packages/realm_generator/test/good_test_data/indexable_types.expected @@ -185,24 +185,17 @@ class Indexable extends _Indexable static EJsonValue _toEJson(Indexable value) => value.toEJson(); static Indexable _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'aBool': EJsonValue aBool, - 'aNullableBool': EJsonValue aNullableBool, 'anInt': EJsonValue anInt, - 'aNullableInt': EJsonValue aNullableInt, 'aString': EJsonValue aString, - 'aNullableString': EJsonValue aNullableString, 'anObjectId': EJsonValue anObjectId, - 'aNullableObjectId': EJsonValue aNullableObjectId, 'anUuid': EJsonValue anUuid, - 'aNullableUuid': EJsonValue aNullableUuid, 'aDateTime': EJsonValue aDateTime, - 'aNullableDateTime': EJsonValue aNullableDateTime, - 'aRealmValue': EJsonValue aRealmValue, 'generalStringIndex': EJsonValue generalStringIndex, 'ftsStringValue': EJsonValue ftsStringValue, - 'nullableFtsStringValue': EJsonValue nullableFtsStringValue, } => Indexable( fromEJson(aBool), @@ -213,14 +206,14 @@ class Indexable extends _Indexable fromEJson(aDateTime), fromEJson(generalStringIndex), fromEJson(ftsStringValue), - aNullableBool: fromEJson(aNullableBool), - aNullableInt: fromEJson(aNullableInt), - aNullableString: fromEJson(aNullableString), - aNullableObjectId: fromEJson(aNullableObjectId), - aNullableUuid: fromEJson(aNullableUuid), - aNullableDateTime: fromEJson(aNullableDateTime), - aRealmValue: fromEJson(aRealmValue), - nullableFtsStringValue: fromEJson(nullableFtsStringValue), + aNullableBool: fromEJson(ejson['aNullableBool']), + aNullableInt: fromEJson(ejson['aNullableInt']), + aNullableString: fromEJson(ejson['aNullableString']), + aNullableObjectId: fromEJson(ejson['aNullableObjectId']), + aNullableUuid: fromEJson(ejson['aNullableUuid']), + aNullableDateTime: fromEJson(ejson['aNullableDateTime']), + aRealmValue: fromEJson(ejson['aRealmValue']), + nullableFtsStringValue: fromEJson(ejson['nullableFtsStringValue']), ), _ => raiseInvalidEJson(ejson), }; @@ -229,7 +222,7 @@ class Indexable extends _Indexable static final schema = () { RealmObjectBase.registerFactory(Indexable._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Indexable, 'Indexable', [ + return const SchemaObject(ObjectType.realmObject, Indexable, 'Indexable', [ SchemaProperty('aBool', RealmPropertyType.bool, indexType: RealmIndexType.regular), SchemaProperty('aNullableBool', RealmPropertyType.bool, diff --git a/packages/realm_generator/test/good_test_data/list_initialization.expected b/packages/realm_generator/test/good_test_data/list_initialization.expected index d0463f516..7850ccc07 100644 --- a/packages/realm_generator/test/good_test_data/list_initialization.expected +++ b/packages/realm_generator/test/good_test_data/list_initialization.expected @@ -140,39 +140,28 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Person value) => value.toEJson(); static Person _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'children': EJsonValue children, - 'initList': EJsonValue initList, - 'initListWithType': EJsonValue initListWithType, - 'initListConst': EJsonValue initListConst, - 'initSet': EJsonValue initSet, - 'initSetWithType': EJsonValue initSetWithType, - 'initSetConst': EJsonValue initSetConst, - 'initMap': EJsonValue initMap, - 'initMapWithType': EJsonValue initMapWithType, - 'initMapConst': EJsonValue initMapConst, - } => - Person( - children: fromEJson(children), - initList: fromEJson(initList), - initListWithType: fromEJson(initListWithType), - initListConst: fromEJson(initListConst), - initSet: fromEJson(initSet), - initSetWithType: fromEJson(initSetWithType), - initSetConst: fromEJson(initSetConst), - initMap: fromEJson(initMap), - initMapWithType: fromEJson(initMapWithType), - initMapConst: fromEJson(initMapConst), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return Person( + children: fromEJson(ejson['children']), + initList: fromEJson(ejson['initList'], defaultValue: const []), + initListWithType: + fromEJson(ejson['initListWithType'], defaultValue: const []), + initListConst: fromEJson(ejson['initListConst'], defaultValue: const []), + initSet: fromEJson(ejson['initSet'], defaultValue: const {}), + initSetWithType: + fromEJson(ejson['initSetWithType'], defaultValue: const {}), + initSetConst: fromEJson(ejson['initSetConst'], defaultValue: const {}), + initMap: fromEJson(ejson['initMap'], defaultValue: const {}), + initMapWithType: + fromEJson(ejson['initMapWithType'], defaultValue: const {}), + initMapConst: fromEJson(ejson['initMapConst'], defaultValue: const {}), + ); } static final schema = () { RealmObjectBase.registerFactory(Person._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('children', RealmPropertyType.object, linkTarget: 'Person', collectionType: RealmCollectionType.list), SchemaProperty('initList', RealmPropertyType.int, diff --git a/packages/realm_generator/test/good_test_data/map.expected b/packages/realm_generator/test/good_test_data/map.expected index 62c71fda1..4988d4084 100644 --- a/packages/realm_generator/test/good_test_data/map.expected +++ b/packages/realm_generator/test/good_test_data/map.expected @@ -148,41 +148,26 @@ class LotsOfMaps extends _LotsOfMaps static EJsonValue _toEJson(LotsOfMaps value) => value.toEJson(); static LotsOfMaps _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'persons': EJsonValue persons, - 'bools': EJsonValue bools, - 'dateTimes': EJsonValue dateTimes, - 'decimals': EJsonValue decimals, - 'doubles': EJsonValue doubles, - 'ints': EJsonValue ints, - 'objectIds': EJsonValue objectIds, - 'any': EJsonValue any, - 'strings': EJsonValue strings, - 'binary': EJsonValue binary, - 'uuids': EJsonValue uuids, - } => - LotsOfMaps( - persons: fromEJson(persons), - bools: fromEJson(bools), - dateTimes: fromEJson(dateTimes), - decimals: fromEJson(decimals), - doubles: fromEJson(doubles), - ints: fromEJson(ints), - objectIds: fromEJson(objectIds), - any: fromEJson(any), - strings: fromEJson(strings), - binary: fromEJson(binary), - uuids: fromEJson(uuids), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return LotsOfMaps( + persons: fromEJson(ejson['persons']), + bools: fromEJson(ejson['bools']), + dateTimes: fromEJson(ejson['dateTimes']), + decimals: fromEJson(ejson['decimals']), + doubles: fromEJson(ejson['doubles']), + ints: fromEJson(ejson['ints']), + objectIds: fromEJson(ejson['objectIds']), + any: fromEJson(ejson['any']), + strings: fromEJson(ejson['strings']), + binary: fromEJson(ejson['binary']), + uuids: fromEJson(ejson['uuids']), + ); } static final schema = () { RealmObjectBase.registerFactory(LotsOfMaps._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, LotsOfMaps, 'LotsOfMaps', [ + return const SchemaObject(ObjectType.realmObject, LotsOfMaps, 'LotsOfMaps', [ SchemaProperty('persons', RealmPropertyType.object, optional: true, linkTarget: 'Person', @@ -247,6 +232,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Person value) => value.toEJson(); static Person _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, @@ -261,7 +247,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Person._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string), ]); }(); diff --git a/packages/realm_generator/test/good_test_data/mapto.expected b/packages/realm_generator/test/good_test_data/mapto.expected index 8f6fcac83..680db1834 100644 --- a/packages/realm_generator/test/good_test_data/mapto.expected +++ b/packages/realm_generator/test/good_test_data/mapto.expected @@ -72,25 +72,19 @@ class Original extends $Original static EJsonValue _toEJson(Original value) => value.toEJson(); static Original _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'remapped primitive': EJsonValue primitiveProperty, - 'remapped object': EJsonValue objectProperty, - 'remapped list': EJsonValue listProperty, - } => - Original( - primitiveProperty: fromEJson(primitiveProperty), - objectProperty: fromEJson(objectProperty), - listProperty: fromEJson(listProperty), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return Original( + primitiveProperty: + fromEJson(ejson['remapped primitive'], defaultValue: 0), + objectProperty: fromEJson(ejson['remapped object']), + listProperty: fromEJson(ejson['remapped list']), + ); } static final schema = () { RealmObjectBase.registerFactory(Original._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Original, 'another type', [ + return const SchemaObject(ObjectType.realmObject, Original, 'another type', [ SchemaProperty('primitiveProperty', RealmPropertyType.int, mapTo: 'remapped primitive'), SchemaProperty('objectProperty', RealmPropertyType.object, diff --git a/packages/realm_generator/test/good_test_data/optional_argument.expected b/packages/realm_generator/test/good_test_data/optional_argument.expected index 5ca9325c2..d008f1637 100644 --- a/packages/realm_generator/test/good_test_data/optional_argument.expected +++ b/packages/realm_generator/test/good_test_data/optional_argument.expected @@ -41,21 +41,16 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Person value) => value.toEJson(); static Person _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'spouse': EJsonValue spouse, - } => - Person( - spouse: fromEJson(spouse), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return Person( + spouse: fromEJson(ejson['spouse']), + ); } static final schema = () { RealmObjectBase.registerFactory(Person._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('spouse', RealmPropertyType.object, optional: true, linkTarget: 'Person'), ]); diff --git a/packages/realm_generator/test/good_test_data/pinhole.expected b/packages/realm_generator/test/good_test_data/pinhole.expected index 494bbceb5..3e39b097b 100644 --- a/packages/realm_generator/test/good_test_data/pinhole.expected +++ b/packages/realm_generator/test/good_test_data/pinhole.expected @@ -47,21 +47,16 @@ class Foo extends _Foo with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Foo value) => value.toEJson(); static Foo _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'x': EJsonValue x, - } => - Foo( - x: fromEJson(x), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return Foo( + x: fromEJson(ejson['x'], defaultValue: 0), + ); } static final schema = () { RealmObjectBase.registerFactory(Foo._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Foo, 'Foo', [ + return const SchemaObject(ObjectType.realmObject, Foo, 'Foo', [ SchemaProperty('x', RealmPropertyType.int), ]); }(); diff --git a/packages/realm_generator/test/good_test_data/primary_key.expected b/packages/realm_generator/test/good_test_data/primary_key.expected index d60f5e3a1..11c724753 100644 --- a/packages/realm_generator/test/good_test_data/primary_key.expected +++ b/packages/realm_generator/test/good_test_data/primary_key.expected @@ -40,6 +40,7 @@ class IntPK extends _IntPK with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(IntPK value) => value.toEJson(); static IntPK _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, @@ -54,7 +55,7 @@ class IntPK extends _IntPK with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(IntPK._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, IntPK, 'IntPK', [ + return const SchemaObject(ObjectType.realmObject, IntPK, 'IntPK', [ SchemaProperty('id', RealmPropertyType.int, primaryKey: true), ]); }(); @@ -98,12 +99,13 @@ class NullableIntPK extends _NullableIntPK static EJsonValue _toEJson(NullableIntPK value) => value.toEJson(); static NullableIntPK _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, } => NullableIntPK( - fromEJson(id), + fromEJson(ejson['id']), ), _ => raiseInvalidEJson(ejson), }; @@ -112,7 +114,7 @@ class NullableIntPK extends _NullableIntPK static final schema = () { RealmObjectBase.registerFactory(NullableIntPK._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, NullableIntPK, 'NullableIntPK', [ SchemaProperty('id', RealmPropertyType.int, optional: true, primaryKey: true), @@ -157,6 +159,7 @@ class StringPK extends _StringPK static EJsonValue _toEJson(StringPK value) => value.toEJson(); static StringPK _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, @@ -171,7 +174,7 @@ class StringPK extends _StringPK static final schema = () { RealmObjectBase.registerFactory(StringPK._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, StringPK, 'StringPK', [ + return const SchemaObject(ObjectType.realmObject, StringPK, 'StringPK', [ SchemaProperty('id', RealmPropertyType.string, primaryKey: true), ]); }(); @@ -216,12 +219,13 @@ class NullableStringPK extends _NullableStringPK static EJsonValue _toEJson(NullableStringPK value) => value.toEJson(); static NullableStringPK _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, } => NullableStringPK( - fromEJson(id), + fromEJson(ejson['id']), ), _ => raiseInvalidEJson(ejson), }; @@ -230,7 +234,7 @@ class NullableStringPK extends _NullableStringPK static final schema = () { RealmObjectBase.registerFactory(NullableStringPK._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, NullableStringPK, 'NullableStringPK', [ SchemaProperty('id', RealmPropertyType.string, optional: true, primaryKey: true), @@ -275,6 +279,7 @@ class ObjectIdPK extends _ObjectIdPK static EJsonValue _toEJson(ObjectIdPK value) => value.toEJson(); static ObjectIdPK _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, @@ -289,7 +294,7 @@ class ObjectIdPK extends _ObjectIdPK static final schema = () { RealmObjectBase.registerFactory(ObjectIdPK._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, ObjectIdPK, 'ObjectIdPK', [ + return const SchemaObject(ObjectType.realmObject, ObjectIdPK, 'ObjectIdPK', [ SchemaProperty('id', RealmPropertyType.objectid, primaryKey: true), ]); }(); @@ -334,12 +339,13 @@ class NullableObjectIdPK extends _NullableObjectIdPK static EJsonValue _toEJson(NullableObjectIdPK value) => value.toEJson(); static NullableObjectIdPK _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, } => NullableObjectIdPK( - fromEJson(id), + fromEJson(ejson['id']), ), _ => raiseInvalidEJson(ejson), }; @@ -348,7 +354,7 @@ class NullableObjectIdPK extends _NullableObjectIdPK static final schema = () { RealmObjectBase.registerFactory(NullableObjectIdPK._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, NullableObjectIdPK, 'NullableObjectIdPK', [ SchemaProperty('id', RealmPropertyType.objectid, optional: true, primaryKey: true), @@ -392,6 +398,7 @@ class UuidPK extends _UuidPK with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(UuidPK value) => value.toEJson(); static UuidPK _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, @@ -406,7 +413,7 @@ class UuidPK extends _UuidPK with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(UuidPK._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, UuidPK, 'UuidPK', [ + return const SchemaObject(ObjectType.realmObject, UuidPK, 'UuidPK', [ SchemaProperty('id', RealmPropertyType.uuid, primaryKey: true), ]); }(); @@ -450,12 +457,13 @@ class NullableUuidPK extends _NullableUuidPK static EJsonValue _toEJson(NullableUuidPK value) => value.toEJson(); static NullableUuidPK _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'id': EJsonValue id, } => NullableUuidPK( - fromEJson(id), + fromEJson(ejson['id']), ), _ => raiseInvalidEJson(ejson), }; @@ -464,7 +472,7 @@ class NullableUuidPK extends _NullableUuidPK static final schema = () { RealmObjectBase.registerFactory(NullableUuidPK._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, NullableUuidPK, 'NullableUuidPK', [ SchemaProperty('id', RealmPropertyType.uuid, optional: true, primaryKey: true), diff --git a/packages/realm_generator/test/good_test_data/private_fields.expected b/packages/realm_generator/test/good_test_data/private_fields.expected index 5d727d62d..269e2b8c1 100644 --- a/packages/realm_generator/test/good_test_data/private_fields.expected +++ b/packages/realm_generator/test/good_test_data/private_fields.expected @@ -59,14 +59,14 @@ class WithPrivateFields extends _WithPrivateFields static EJsonValue _toEJson(WithPrivateFields value) => value.toEJson(); static WithPrivateFields _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { '_plain': EJsonValue _plain, - '_withDefault': EJsonValue _withDefault, } => WithPrivateFields( fromEJson(_plain), - withDefault: fromEJson(_withDefault), + withDefault: fromEJson(ejson['_withDefault'], defaultValue: 0), ), _ => raiseInvalidEJson(ejson), }; @@ -75,7 +75,7 @@ class WithPrivateFields extends _WithPrivateFields static final schema = () { RealmObjectBase.registerFactory(WithPrivateFields._); register(_toEJson, _fromEJson); - return SchemaObject( + return const SchemaObject( ObjectType.realmObject, WithPrivateFields, 'WithPrivateFields', [ SchemaProperty('_plain', RealmPropertyType.string), SchemaProperty('_withDefault', RealmPropertyType.int), diff --git a/packages/realm_generator/test/good_test_data/realm_set.expected b/packages/realm_generator/test/good_test_data/realm_set.expected index c5e4afc6f..f8b989938 100644 --- a/packages/realm_generator/test/good_test_data/realm_set.expected +++ b/packages/realm_generator/test/good_test_data/realm_set.expected @@ -40,6 +40,7 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Car value) => value.toEJson(); static Car _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'make': EJsonValue make, @@ -54,7 +55,7 @@ class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Car._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Car, 'Car', [ + return const SchemaObject(ObjectType.realmObject, Car, 'Car', [ SchemaProperty('make', RealmPropertyType.string, primaryKey: true), ]); }(); @@ -276,44 +277,29 @@ class RealmSets extends _RealmSets static EJsonValue _toEJson(RealmSets value) => value.toEJson(); static RealmSets _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'key': EJsonValue key, - 'boolSet': EJsonValue boolSet, - 'nullableBoolSet': EJsonValue nullableBoolSet, - 'intSet': EJsonValue intSet, - 'nullableintSet': EJsonValue nullableintSet, - 'stringSet': EJsonValue stringSet, - 'nullablestringSet': EJsonValue nullablestringSet, - 'doubleSet': EJsonValue doubleSet, - 'nullabledoubleSet': EJsonValue nullabledoubleSet, - 'dateTimeSet': EJsonValue dateTimeSet, - 'nullabledateTimeSet': EJsonValue nullabledateTimeSet, - 'objectIdSet': EJsonValue objectIdSet, - 'nullableobjectIdSet': EJsonValue nullableobjectIdSet, - 'uuidSet': EJsonValue uuidSet, - 'nullableuuidSet': EJsonValue nullableuuidSet, - 'realmValueSet': EJsonValue realmValueSet, - 'realmObjectsSet': EJsonValue realmObjectsSet, } => RealmSets( fromEJson(key), - boolSet: fromEJson(boolSet), - nullableBoolSet: fromEJson(nullableBoolSet), - intSet: fromEJson(intSet), - nullableintSet: fromEJson(nullableintSet), - stringSet: fromEJson(stringSet), - nullablestringSet: fromEJson(nullablestringSet), - doubleSet: fromEJson(doubleSet), - nullabledoubleSet: fromEJson(nullabledoubleSet), - dateTimeSet: fromEJson(dateTimeSet), - nullabledateTimeSet: fromEJson(nullabledateTimeSet), - objectIdSet: fromEJson(objectIdSet), - nullableobjectIdSet: fromEJson(nullableobjectIdSet), - uuidSet: fromEJson(uuidSet), - nullableuuidSet: fromEJson(nullableuuidSet), - realmValueSet: fromEJson(realmValueSet), - realmObjectsSet: fromEJson(realmObjectsSet), + boolSet: fromEJson(ejson['boolSet']), + nullableBoolSet: fromEJson(ejson['nullableBoolSet']), + intSet: fromEJson(ejson['intSet']), + nullableintSet: fromEJson(ejson['nullableintSet']), + stringSet: fromEJson(ejson['stringSet']), + nullablestringSet: fromEJson(ejson['nullablestringSet']), + doubleSet: fromEJson(ejson['doubleSet']), + nullabledoubleSet: fromEJson(ejson['nullabledoubleSet']), + dateTimeSet: fromEJson(ejson['dateTimeSet']), + nullabledateTimeSet: fromEJson(ejson['nullabledateTimeSet']), + objectIdSet: fromEJson(ejson['objectIdSet']), + nullableobjectIdSet: fromEJson(ejson['nullableobjectIdSet']), + uuidSet: fromEJson(ejson['uuidSet']), + nullableuuidSet: fromEJson(ejson['nullableuuidSet']), + realmValueSet: fromEJson(ejson['realmValueSet']), + realmObjectsSet: fromEJson(ejson['realmObjectsSet']), ), _ => raiseInvalidEJson(ejson), }; @@ -322,7 +308,7 @@ class RealmSets extends _RealmSets static final schema = () { RealmObjectBase.registerFactory(RealmSets._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, RealmSets, 'RealmSets', [ + return const SchemaObject(ObjectType.realmObject, RealmSets, 'RealmSets', [ SchemaProperty('key', RealmPropertyType.int, primaryKey: true), SchemaProperty('boolSet', RealmPropertyType.bool, collectionType: RealmCollectionType.set), diff --git a/packages/realm_generator/test/good_test_data/required_argument.expected b/packages/realm_generator/test/good_test_data/required_argument.expected index fb1478894..6a7a6a9d4 100644 --- a/packages/realm_generator/test/good_test_data/required_argument.expected +++ b/packages/realm_generator/test/good_test_data/required_argument.expected @@ -40,6 +40,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Person value) => value.toEJson(); static Person _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, @@ -54,7 +55,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Person._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string, primaryKey: true), ]); }(); diff --git a/packages/realm_generator/test/good_test_data/required_argument_with_default_value.expected b/packages/realm_generator/test/good_test_data/required_argument_with_default_value.expected index b0ad1b3a1..74d898c4c 100644 --- a/packages/realm_generator/test/good_test_data/required_argument_with_default_value.expected +++ b/packages/realm_generator/test/good_test_data/required_argument_with_default_value.expected @@ -47,21 +47,16 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Person value) => value.toEJson(); static Person _fromEJson(EJsonValue ejson) { - return switch (ejson) { - { - 'age': EJsonValue age, - } => - Person( - age: fromEJson(age), - ), - _ => raiseInvalidEJson(ejson), - }; + if (ejson is! Map) return raiseInvalidEJson(ejson); + return Person( + age: fromEJson(ejson['age'], defaultValue: 47), + ); } static final schema = () { RealmObjectBase.registerFactory(Person._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('age', RealmPropertyType.int), ]); }(); diff --git a/packages/realm_generator/test/good_test_data/user_defined_getter.expected b/packages/realm_generator/test/good_test_data/user_defined_getter.expected index 470f09635..0f46d9c1f 100644 --- a/packages/realm_generator/test/good_test_data/user_defined_getter.expected +++ b/packages/realm_generator/test/good_test_data/user_defined_getter.expected @@ -40,6 +40,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static EJsonValue _toEJson(Person value) => value.toEJson(); static Person _fromEJson(EJsonValue ejson) { + if (ejson is! Map) return raiseInvalidEJson(ejson); return switch (ejson) { { 'name': EJsonValue name, @@ -54,7 +55,7 @@ class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject { static final schema = () { RealmObjectBase.registerFactory(Person._); register(_toEJson, _fromEJson); - return SchemaObject(ObjectType.realmObject, Person, 'Person', [ + return const SchemaObject(ObjectType.realmObject, Person, 'Person', [ SchemaProperty('name', RealmPropertyType.string), ]); }();