From 3354a43d53165bc812534472f7d7984ca056cdc6 Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Thu, 13 Jun 2024 11:50:00 -0400 Subject: [PATCH] remaining changes --- .../dart/test/serialization_test.realm.dart | 325 ++++++++++++++++++ .../flutter/realm-database/serialization.txt | 93 ++++- 2 files changed, 400 insertions(+), 18 deletions(-) create mode 100644 examples/dart/test/serialization_test.realm.dart diff --git a/examples/dart/test/serialization_test.realm.dart b/examples/dart/test/serialization_test.realm.dart new file mode 100644 index 0000000000..c6398f6b33 --- /dev/null +++ b/examples/dart/test/serialization_test.realm.dart @@ -0,0 +1,325 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'serialization_test.dart'; + +// ************************************************************************** +// RealmObjectGenerator +// ************************************************************************** + +// ignore_for_file: type=lint +class Address extends _Address + with RealmEntity, RealmObjectBase, EmbeddedObject { + Address( + String street, + String city, + String state, + String country, + ) { + RealmObjectBase.set(this, 'street', street); + RealmObjectBase.set(this, 'city', city); + RealmObjectBase.set(this, 'state', state); + RealmObjectBase.set(this, 'country', country); + } + + Address._(); + + @override + String get street => RealmObjectBase.get(this, 'street') as String; + @override + set street(String value) => RealmObjectBase.set(this, 'street', value); + + @override + String get city => RealmObjectBase.get(this, 'city') as String; + @override + set city(String value) => RealmObjectBase.set(this, 'city', value); + + @override + String get state => RealmObjectBase.get(this, 'state') as String; + @override + set state(String value) => RealmObjectBase.set(this, 'state', value); + + @override + String get country => RealmObjectBase.get(this, 'country') as String; + @override + set country(String value) => RealmObjectBase.set(this, 'country', value); + + @override + Stream> get changes => + RealmObjectBase.getChanges
(this); + + @override + Address freeze() => RealmObjectBase.freezeObject
(this); + + EJsonValue toEJson() { + return { + 'street': street.toEJson(), + 'city': city.toEJson(), + 'state': state.toEJson(), + 'country': country.toEJson(), + }; + } + + static EJsonValue _toEJson(Address value) => value.toEJson(); + static Address _fromEJson(EJsonValue ejson) { + return switch (ejson) { + { + 'street': EJsonValue street, + 'city': EJsonValue city, + 'state': EJsonValue state, + 'country': EJsonValue country, + } => + Address( + fromEJson(street), + fromEJson(city), + fromEJson(state), + fromEJson(country), + ), + _ => raiseInvalidEJson(ejson), + }; + } + + static final schema = () { + RealmObjectBase.registerFactory(Address._); + register(_toEJson, _fromEJson); + return SchemaObject(ObjectType.embeddedObject, Address, 'Address', [ + SchemaProperty('street', RealmPropertyType.string), + SchemaProperty('city', RealmPropertyType.string), + SchemaProperty('state', RealmPropertyType.string), + SchemaProperty('country', RealmPropertyType.string), + ]); + }(); + + @override + SchemaObject get objectSchema => RealmObjectBase.getSchema(this) ?? schema; +} + +class SerializeModel extends _SerializeModel + with RealmEntity, RealmObjectBase, RealmObject { + static var _defaultsSet = false; + + SerializeModel( + ObjectId id, + Uuid myId, + Uint8List requiredBinaryProperty, + DateTime dateLastServiced, + String licensePlate, { + Iterable listOfStrings = const [], + Set setOfInts = const {}, + Map mapOfMixedAnyValues = const {}, + bool isElectric = false, + Address? address, + double milesDriven = 126.0, + int number32 = 900, + int number64 = 922393736854775807, + int? a, + }) { + if (!_defaultsSet) { + _defaultsSet = RealmObjectBase.setDefaults({ + 'isElectric': false, + 'milesDriven': 126.0, + 'number32': 900, + 'number64': 922393736854775807, + }); + } + RealmObjectBase.set(this, 'id', id); + RealmObjectBase.set(this, 'myId', myId); + RealmObjectBase.set>( + this, 'listOfStrings', RealmList(listOfStrings)); + RealmObjectBase.set>( + this, 'setOfInts', RealmSet(setOfInts)); + RealmObjectBase.set>( + this, 'mapOfMixedAnyValues', RealmMap(mapOfMixedAnyValues)); + RealmObjectBase.set(this, 'requiredBinaryProperty', requiredBinaryProperty); + RealmObjectBase.set(this, 'isElectric', isElectric); + RealmObjectBase.set(this, 'dateLastServiced', dateLastServiced); + RealmObjectBase.set(this, 'address', address); + RealmObjectBase.set(this, 'milesDriven', milesDriven); + RealmObjectBase.set(this, 'number32', number32); + RealmObjectBase.set(this, 'number64', number64); + RealmObjectBase.set(this, 'licensePlate', licensePlate); + RealmObjectBase.set(this, 'a', a); + } + + SerializeModel._(); + + @override + ObjectId get id => RealmObjectBase.get(this, 'id') as ObjectId; + @override + set id(ObjectId value) => RealmObjectBase.set(this, 'id', value); + + @override + Uuid get myId => RealmObjectBase.get(this, 'myId') as Uuid; + @override + set myId(Uuid value) => RealmObjectBase.set(this, 'myId', value); + + @override + RealmList get listOfStrings => + RealmObjectBase.get(this, 'listOfStrings') as RealmList; + @override + set listOfStrings(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + RealmSet get setOfInts => + RealmObjectBase.get(this, 'setOfInts') as RealmSet; + @override + set setOfInts(covariant RealmSet value) => + throw RealmUnsupportedSetError(); + + @override + RealmMap get mapOfMixedAnyValues => + RealmObjectBase.get(this, 'mapOfMixedAnyValues') as RealmMap; + @override + set mapOfMixedAnyValues(covariant RealmMap value) => + throw RealmUnsupportedSetError(); + + @override + Uint8List get requiredBinaryProperty => + RealmObjectBase.get(this, 'requiredBinaryProperty') + as Uint8List; + @override + set requiredBinaryProperty(Uint8List value) => + RealmObjectBase.set(this, 'requiredBinaryProperty', value); + + @override + bool get isElectric => RealmObjectBase.get(this, 'isElectric') as bool; + @override + set isElectric(bool value) => RealmObjectBase.set(this, 'isElectric', value); + + @override + DateTime get dateLastServiced => + RealmObjectBase.get(this, 'dateLastServiced') as DateTime; + @override + set dateLastServiced(DateTime value) => + RealmObjectBase.set(this, 'dateLastServiced', value); + + @override + Address? get address => + RealmObjectBase.get
(this, 'address') as Address?; + @override + set address(covariant Address? value) => + RealmObjectBase.set(this, 'address', value); + + @override + double get milesDriven => + RealmObjectBase.get(this, 'milesDriven') as double; + @override + set milesDriven(double value) => + RealmObjectBase.set(this, 'milesDriven', value); + + @override + int get number32 => RealmObjectBase.get(this, 'number32') as int; + @override + set number32(int value) => RealmObjectBase.set(this, 'number32', value); + + @override + int get number64 => RealmObjectBase.get(this, 'number64') as int; + @override + set number64(int value) => RealmObjectBase.set(this, 'number64', value); + + @override + String get licensePlate => + RealmObjectBase.get(this, 'licensePlate') as String; + @override + set licensePlate(String value) => + RealmObjectBase.set(this, 'licensePlate', value); + + @override + int? get a => RealmObjectBase.get(this, 'a') as int?; + @override + set a(int? value) => RealmObjectBase.set(this, 'a', value); + + @override + Stream> get changes => + RealmObjectBase.getChanges(this); + + @override + SerializeModel freeze() => RealmObjectBase.freezeObject(this); + + EJsonValue toEJson() { + return { + 'id': id.toEJson(), + 'myId': myId.toEJson(), + 'listOfStrings': listOfStrings.toEJson(), + 'setOfInts': setOfInts.toEJson(), + 'mapOfMixedAnyValues': mapOfMixedAnyValues.toEJson(), + 'requiredBinaryProperty': requiredBinaryProperty.toEJson(), + 'isElectric': isElectric.toEJson(), + 'dateLastServiced': dateLastServiced.toEJson(), + 'address': address.toEJson(), + 'milesDriven': milesDriven.toEJson(), + 'number32': number32.toEJson(), + 'number64': number64.toEJson(), + 'licensePlate': licensePlate.toEJson(), + 'a': a.toEJson(), + }; + } + + static EJsonValue _toEJson(SerializeModel value) => value.toEJson(); + static SerializeModel _fromEJson(EJsonValue ejson) { + return switch (ejson) { + { + 'id': EJsonValue id, + 'myId': EJsonValue myId, + 'listOfStrings': EJsonValue listOfStrings, + 'setOfInts': EJsonValue setOfInts, + 'mapOfMixedAnyValues': EJsonValue mapOfMixedAnyValues, + 'requiredBinaryProperty': EJsonValue requiredBinaryProperty, + 'isElectric': EJsonValue isElectric, + 'dateLastServiced': EJsonValue dateLastServiced, + 'address': EJsonValue address, + 'milesDriven': EJsonValue milesDriven, + 'number32': EJsonValue number32, + 'number64': EJsonValue number64, + 'licensePlate': EJsonValue licensePlate, + 'a': EJsonValue a, + } => + SerializeModel( + fromEJson(id), + fromEJson(myId), + fromEJson(requiredBinaryProperty), + fromEJson(dateLastServiced), + fromEJson(licensePlate), + listOfStrings: fromEJson(listOfStrings), + setOfInts: fromEJson(setOfInts), + mapOfMixedAnyValues: fromEJson(mapOfMixedAnyValues), + isElectric: fromEJson(isElectric), + address: fromEJson(address), + milesDriven: fromEJson(milesDriven), + number32: fromEJson(number32), + number64: fromEJson(number64), + a: fromEJson(a), + ), + _ => raiseInvalidEJson(ejson), + }; + } + + static final schema = () { + RealmObjectBase.registerFactory(SerializeModel._); + register(_toEJson, _fromEJson); + return SchemaObject( + ObjectType.realmObject, SerializeModel, 'SerializeModel', [ + SchemaProperty('id', RealmPropertyType.objectid, primaryKey: true), + SchemaProperty('myId', RealmPropertyType.uuid), + SchemaProperty('listOfStrings', RealmPropertyType.string, + collectionType: RealmCollectionType.list), + SchemaProperty('setOfInts', RealmPropertyType.int, + collectionType: RealmCollectionType.set), + SchemaProperty('mapOfMixedAnyValues', RealmPropertyType.int, + collectionType: RealmCollectionType.map), + SchemaProperty('requiredBinaryProperty', RealmPropertyType.binary), + SchemaProperty('isElectric', RealmPropertyType.bool), + SchemaProperty('dateLastServiced', RealmPropertyType.timestamp), + SchemaProperty('address', RealmPropertyType.object, + optional: true, linkTarget: 'Address'), + SchemaProperty('milesDriven', RealmPropertyType.double), + SchemaProperty('number32', RealmPropertyType.int), + SchemaProperty('number64', RealmPropertyType.int), + SchemaProperty('licensePlate', RealmPropertyType.string), + SchemaProperty('a', RealmPropertyType.int, optional: true), + ]); + }(); + + @override + SchemaObject get objectSchema => RealmObjectBase.getSchema(this) ?? schema; +} diff --git a/source/sdk/flutter/realm-database/serialization.txt b/source/sdk/flutter/realm-database/serialization.txt index 0397b3d20c..29e191d0b0 100644 --- a/source/sdk/flutter/realm-database/serialization.txt +++ b/source/sdk/flutter/realm-database/serialization.txt @@ -24,24 +24,81 @@ Serialization allows for easier integration with MongoDB APIs. Supported Data Types for Serialization -------------------------------------- -The Flutter SDK supports serializing the following data types: - -- Array -- Binary -- Boolean -- Date -- Document -- Decimal128 -- Double -- Int32 -- Int64 -- MaxKey -- MinKey -- ObjectID -- String -- Symbol -- Null -- Undefined +Flutter SDK supports the following data types: + +.. list-table:: + :header-rows: 1 + :widths: 20 20 60 + + * - Realm Data Type + - BSON Data Type + - Example + + * - Integer (min: -2147483648, max: +2147483647) + - Int32 + - ``int number32 = 900`` serializes to ``number32: {"$numberInt": 900}`` + + * - Integer (min: -9223372036854775808, max: +9223372036854775807) + - Int64 + - ``int number64 = 922393736854775807`` serializes to ``number64: {$numberLong: 922393736854775807}`` + + * - Boolean + - Boolean + - ``bool isElectric = false`` serializes to ``isElectric: false`` + + * - Double + - Double + - ``double milesDriven = 126.0`` serializes to ``milesDriven: {$numberDouble: 126.0}`` + + * - String + - String + - ``String licensePlate = "690ZXWYZ"`` serializes to ``licensePlate: 690ZXWYZ`` + + * - DateTime + - Date + - birthDate: {$date: {$numberLong: 1712707200000}} + + * - RealmList + - Array + - ``List listOfStrings = [food, water]`` serializes to ``listOfStrings: [food, water]`` + + * - RealmMap + - Array + - ``Map mapOfMixedAnyValues = {'first': 123 , 'second': 567}`` serializes to ``mapOfValues: {first: {$numberInt: 123}, second: {$numberInt: 567}}`` + + * - RealmSet + - Array + - ``Set setOfInts = {0, 1, 2, 3}`` serializes to ``setOfInts: [{$numberInt: 0}, {$numberInt: 1}, {$numberInt: 2}, {$numberInt: 3}]`` + + * - ObjectId + - ObjectId + - ``ObjectId id = ObjectId()`` serializes to ``{id: {$oid: 666a6fd54978af08e54a8d52}`` + + * - UUID + - Binary + - ``Uuid myId = Uuid.v4()`` serializes to ``myId: {$binary: {base64: 6TvsMWxDRWa1jSC6gxiM3A==, subType: 04}}`` + + * - Decimal128 + - + - `` `` serializes to ```` + + * - RealmValue + - + - `` `` serializes to ```` + + * - Uint8List + - Binary + - ``Uint8List aBinaryProperty = Uint8List.fromList([1, 2])`` serializes to ``aBinaryProperty: {$binary: {base64: AQI=, subType: 00}}`` + + * - Embedded Object + - Document + - ``Address address = Address("500 Dean Street", "Brooklyn", "NY", "USA")`` serializes to ``address: {street: 500 Dean Street, city: Brooklyn, state: NY, country: USA}`` + + * - Null + - Null + - ``int? apples`` serializes to ``apples: null`` + + For more information and examples on the serialization for each of these types, see :manual:`BSON Data Types and Associated Representations `.