Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nullable types in collections #708

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 55 additions & 25 deletions common/lib/src/realm_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,65 @@ import 'dart:typed_data';
import 'package:objectid/objectid.dart';
import 'package:sane_uuid/uuid.dart';

/// All supported `Realm` property types.
/// {@category Configuration}
/// @nodoc
abstract class RealmAccessorMarker {
T getValue<T>(RealmObjectMarker object, String propertyName);
T? getObject<T>(RealmObjectMarker object, String propertyName);
List<T> getList<T>(RealmObjectMarker object, String propertyName);
void set<T>(RealmObjectMarker object, String propertyName, T value, {bool isDefault = false, bool update = false});
}

Type _typeOf<T>() => T; // TODO(kasper): Replace with public version once realm_common contains all

/// @nodoc
class Mapping<T> {
const Mapping();

// Types
Type get type => T;
Type get nullableType => _typeOf<T?>();
Type get listType => List<T>;
Type get listOfNullablesType => List<T?>;

// Factories
T? getObject(RealmAccessorMarker accessor, RealmObjectMarker object, String propertyName) => accessor.getObject<T>(object, propertyName);
T getValue(RealmAccessorMarker accessor, RealmObjectMarker object, String propertyName) => accessor.getValue<T>(object, propertyName);
T? getNullableValue(RealmAccessorMarker accessor, RealmObjectMarker object, String propertyName) => accessor.getValue<T?>(object, propertyName);
List<T> getList(RealmAccessorMarker accessor, RealmObjectMarker object, String propertyName) => accessor.getList<T>(object, propertyName);
List<T?> getListOfNullables(RealmAccessorMarker accessor, RealmObjectMarker object, String propertyName) => accessor.getList<T?>(object, propertyName);
}

const _intMapping = Mapping<int>();
const _boolMapping = Mapping<bool>();
const _doubleMapping = Mapping<double>();

/// @nodoc
enum RealmPropertyType {
int,
bool,
string,
// ignore: unused_field, constant_identifier_names
_3,
binary,
// ignore: unused_field, constant_identifier_names
_5,
mixed,
// ignore: unused_field, constant_identifier_names
_7,
timestamp,
float,
double,
int(_intMapping),
bool(_boolMapping),
string(Mapping<String>()),
_3, // ignore: unused_field, constant_identifier_names
binary(Mapping<Uint8List>()),
_5, // ignore: unused_field, constant_identifier_names
mixed(Mapping<RealmAny>()),
_7, // ignore: unused_field, constant_identifier_names
timestamp(Mapping<DateTime>()),
float(Mapping<Float>()),
double(_doubleMapping),
decimal128,
object,
// ignore: unused_field, constant_identifier_names
_13,
object(Mapping<RealmObjectMarker>()),
_13, // ignore: unused_field, constant_identifier_names
linkingObjects,
objectid,
// ignore: unused_field, constant_identifier_names
_16,
uuid,
objectid(Mapping<ObjectId>()),
_16, // ignore: unused_field, constant_identifier_names
uuid(Mapping<Uuid>());

const RealmPropertyType([this.mapping = const Mapping<Never>()]);

final Mapping<dynamic> mapping;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it will be working if the mapping member is a getter or method that to create the relevant Mapping instance? On this way it will lock simple in the API doc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both RealmPropertyType and RealmCollectionType should be /// @nodoc. These are internal enums the user should never concern themselves with. I have pushed a commit to address this.

}

/// All supported `Realm` collection types.
/// {@category Configuration}
/// @nodoc
enum RealmCollectionType {
none,
list,
Expand Down Expand Up @@ -78,6 +107,7 @@ class RealmUnsupportedSetError extends UnsupportedError implements RealmError {
class RealmStateError extends StateError implements RealmError {
RealmStateError(super.message);
}

/// @nodoc
class Decimal128 {} // TODO Support decimal128 datatype https://github.com/realm/realm-dart/issues/725

Expand Down
128 changes: 69 additions & 59 deletions example/bin/myapp.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading