Skip to content

Commit

Permalink
support uuids
Browse files Browse the repository at this point in the history
  • Loading branch information
blagoev committed Apr 12, 2022
1 parent 8d04e3f commit 3de998a
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 13 deletions.
3 changes: 2 additions & 1 deletion common/lib/realm_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
export 'src/realm_common_base.dart';
export 'src/realm_types.dart';

export 'package:objectid/objectid.dart' show ObjectId;
export 'package:objectid/objectid.dart' show ObjectId;
export 'package:uuid/uuid.dart' show UuidValue;
6 changes: 2 additions & 4 deletions common/lib/src/realm_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import 'dart:ffi';
import 'dart:typed_data';
import 'package:objectid/objectid.dart';
import 'package:uuid/uuid.dart';

/// All supported `Realm` property types.
/// {@category Configuration}
Expand Down Expand Up @@ -78,9 +79,6 @@ class RealmStateError extends StateError implements RealmError {
RealmStateError(String message) : super(message);
}

/// @nodoc
class Uuid {} // TODO!

/// @nodoc
class Decimal128 {} // TODO!

Expand Down Expand Up @@ -108,5 +106,5 @@ class RealmAny {
const RealmAny.dateTime(DateTime timestamp) : this._(timestamp);
const RealmAny.objectId(ObjectId id) : this._(id);
const RealmAny.decimal128(Decimal128 decimal) : this._(decimal);
const RealmAny.uuid(Uuid uuid) : this._(uuid);
const RealmAny.uuid(UuidValue uuid) : this._(uuid);
}
1 change: 1 addition & 0 deletions common/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ environment:

dependencies:
objectid: ^2.1.0
uuid: ^3.0.6

dev_dependencies:
lints: ^1.0.1
2 changes: 1 addition & 1 deletion generator/lib/src/dart_type_ex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ extension DartTypeEx on DartType {
if (isRealmModel) return RealmPropertyType.object;
if (isRealmBacklink) return RealmPropertyType.linkingObjects;
if (isExactly<ObjectId>()) return RealmPropertyType.objectid;
if (isExactly<Uuid>()) return RealmPropertyType.uuid;
if (isExactly<UuidValue>()) return RealmPropertyType.uuid;

return null;
}
Expand Down
6 changes: 3 additions & 3 deletions generator/lib/src/field_element_ex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ extension FieldElementEx on FieldElement {
if (type.isDartCoreSet || //
type.isDartCoreMap ||
type.isRealmAny ||
type.isExactly<Decimal128>() ||
type.isExactly<Uuid>()) {
type.isExactly<Decimal128>()) {
throw RealmInvalidGenerationSourceError(
'Field type not supported yet',
element: this,
Expand Down Expand Up @@ -145,7 +144,8 @@ extension FieldElementEx on FieldElement {

// Validate indexes
if ((primaryKey != null || indexed != null) &&
(![RealmPropertyType.string, RealmPropertyType.int, RealmPropertyType.objectid].contains(type.realmType) || type.isRealmCollection)) {
(![RealmPropertyType.string, RealmPropertyType.int, RealmPropertyType.objectid, RealmPropertyType.uuid].contains(type.realmType) ||
type.isRealmCollection)) {
final file = span!.file;
final annotation = (primaryKey ?? indexed)!.annotation;

Expand Down
6 changes: 6 additions & 0 deletions generator/test/good_test_data/primary_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ class _StringPK {
class _ObjectIdPK {
@PrimaryKey()
late ObjectId id;
}

@RealmModel()
class _UuidPK {
@PrimaryKey()
late UuidValue id;
}
28 changes: 28 additions & 0 deletions generator/test/good_test_data/primary_key.expected
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,32 @@ class ObjectIdPK extends _ObjectIdPK with RealmEntity, RealmObject {
SchemaProperty('id', RealmPropertyType.objectid, primaryKey: true),
]);
}
}

class UuidPK extends _UuidPK with RealmEntity, RealmObject {
UuidPK(
UuidValue id,
) {
RealmObject.set(this, 'id', id);
}

UuidPK._();

@override
UuidValue get id => RealmObject.get<UuidValue>(this, 'id') as UuidValue;
@override
set id(UuidValue value) => throw RealmUnsupportedSetError();

@override
Stream<RealmObjectChanges<UuidPK>> get changes =>
RealmObject.getChanges<UuidPK>(this);

static SchemaObject get schema => _schema ??= _initSchema();
static SchemaObject? _schema;
static SchemaObject _initSchema() {
RealmObject.registerFactory(UuidPK._);
return const SchemaObject(UuidPK, [
SchemaProperty('id', RealmPropertyType.uuid, primaryKey: true),
]);
}
}
11 changes: 8 additions & 3 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import 'dart:typed_data';
// Hide StringUtf8Pointer.toNativeUtf8 and StringUtf16Pointer since these allows silently allocating memory. Use toUtf8Ptr instead
import 'package:ffi/ffi.dart' hide StringUtf8Pointer, StringUtf16Pointer;

import '../application.dart';
import '../collections.dart';
import '../init.dart';
import '../list.dart';
Expand Down Expand Up @@ -1020,9 +1019,15 @@ void _intoRealmValue(Object? value, Pointer<realm_value_t> realm_value, Allocato
for (var i = 0; i < 12; i++) {
realm_value.ref.values.object_id.bytes[i] = bytes[i];
}

realm_value.ref.type = realm_value_type.RLM_TYPE_OBJECT_ID;
break;
case UuidValue:
final bytes = (value as UuidValue).toBytes();
for (var i = 0; i < 16; i++) {
realm_value.ref.values.uuid.bytes[i] = bytes[i];
}
realm_value.ref.type = realm_value_type.RLM_TYPE_UUID;
break;
default:
throw RealmException("Property type ${value.runtimeType} not supported");
}
Expand Down Expand Up @@ -1062,7 +1067,7 @@ extension on Pointer<realm_value_t> {
case realm_value_type.RLM_TYPE_OBJECT_ID:
return ObjectId.fromBytes(cast<Uint8>().asTypedList(12));
case realm_value_type.RLM_TYPE_UUID:
throw Exception("Not implemented");
return UuidValue.fromList(cast<Uint8>().asTypedList(16));
default:
throw RealmException("realm_value_type ${ref.type} not supported");
}
Expand Down
14 changes: 13 additions & 1 deletion lib/src/realm_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,19 @@ import 'results.dart';
// always expose with `show` to explicitly control the public API surface
export 'application.dart' show ApplicationConfiguration, MetadataPersistenceMode;
export 'package:realm_common/realm_common.dart'
show Ignored, Indexed, MapTo, PrimaryKey, RealmError, RealmModel, RealmUnsupportedSetError, RealmStateError, RealmCollectionType, RealmPropertyType, ObjectId;
show
Ignored,
Indexed,
MapTo,
PrimaryKey,
RealmError,
RealmModel,
RealmUnsupportedSetError,
RealmStateError,
RealmCollectionType,
RealmPropertyType,
ObjectId,
UuidValue;
export "configuration.dart" show Configuration, RealmSchema, SchemaObject;
export 'list.dart' show RealmList, RealmListOfObject, RealmListChanges;
export 'realm_object.dart' show RealmEntity, RealmException, RealmObject, RealmObjectChanges;
Expand Down
14 changes: 14 additions & 0 deletions test/realm_object_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class _StringPrimaryKey {
late String id;
}

@RealmModel()
class _UuidPrimaryKey {
@PrimaryKey()
late UuidValue id;
}

Future<void> main([List<String>? args]) async {
print("Current PID $pid");

Expand Down Expand Up @@ -300,4 +306,12 @@ Future<void> main([List<String>? args]) async {
for (final pk in objectIds) {
testPrimaryKey(ObjectIdPrimaryKey.schema, () => ObjectIdPrimaryKey(pk), pk);
}

final uuids = [
UuidValue('0f1dea4d-074e-4c72-b505-e2e8a727602f'),
UuidValue('00000000-0000-0000-0000-000000000000'),
];
for (final pk in uuids) {
testPrimaryKey(UuidPrimaryKey.schema, () => UuidPrimaryKey(pk), pk);
}
}
28 changes: 28 additions & 0 deletions test/realm_object_test.g.dart

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

0 comments on commit 3de998a

Please sign in to comment.