Skip to content

Commit

Permalink
RealmValue interface changes to support collections in mixed
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Aug 18, 2023
1 parent 3d01052 commit 717d5ba
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 20 deletions.
71 changes: 53 additions & 18 deletions common/lib/src/realm_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,27 +188,45 @@ class RealmValue {
const RealmValue.decimal128(Decimal128 decimal) : this._(decimal);
const RealmValue.uuid(Uuid uuid) : this._(uuid);
const RealmValue.uint8List(Uint8List binary) : this._(binary);
const RealmValue.dictionary(Map<String, RealmValue> dictionary) : this._(dictionary);
const RealmValue.list(Iterable<RealmValue> list) : this._(list);
const RealmValue.set(Set<RealmValue> set) : this._(set);

/// Will throw [ArgumentError]
factory RealmValue.from(Object? object) {
if (object == null ||
object is bool ||
object is String ||
object is int ||
object is Float ||
object is double ||
object is RealmObjectMarker ||
object is DateTime ||
object is ObjectId ||
object is Decimal128 ||
object is Uuid ||
object is Uint8List) {
return RealmValue._(object);
} else {
throw ArgumentError.value(object, 'object', 'Unsupported type');
}
/// Will throw [ArgumentError] if the type is not supported
static RealmValue from<T>(T object) {
return switch (object) {
null => const RealmValue.nullValue(),
bool b => RealmValue.bool(b),
String s => RealmValue.string(s),
int i => RealmValue.int(i),
double d => RealmValue.double(d),
RealmObjectMarker o => RealmValue.realmObject(o),
DateTime dt => RealmValue.dateTime(dt),
ObjectId id => RealmValue.objectId(id),
Decimal128 decimal => RealmValue.decimal128(decimal),
Uuid uuid => RealmValue.uuid(uuid),
Uint8List binary => RealmValue.uint8List(binary),
Map<String, RealmValue> d => RealmValue.dictionary(d),
Map<String, dynamic> d => RealmValue.dictionary(Map.fromEntries(d.entries.map((e) => MapEntry(e.key, RealmValue.from(e.value))))),
List<RealmValue> l => RealmValue.list(l),
List<dynamic> l => RealmValue.list(l.map((o) => RealmValue.from(o)).toList()),
Set<RealmValue> s => RealmValue.set(s),
Set<dynamic> s => RealmValue.set(s.map((o) => RealmValue.from(o)).toSet()),
_ => throw ArgumentError.value(object.runtimeType, 'object', 'Unsupported type'),
};
}

RealmValue? operator [](Object index) {
return switch (index) {
int i => as<List<RealmValue>>().elementAtOrNull(i),
String s => as<Map<String, RealmValue>>()[s],
RealmValue v => as<Set<RealmValue>>().lookup(v),
_ => throw ArgumentError.value(index, 'index', 'Unsupported type'),
};
}

T call<T>() => as<T>(); // is this useful?

@override
operator ==(Object? other) {
if (other is RealmValue) {
Expand All @@ -224,3 +242,20 @@ class RealmValue {
@override
String toString() => 'RealmValue($value)';
}

void demo() {
final any = RealmValue.from([
1,
2,
{
'x': {1, 2}
},
]);

final x = any[2]?['x']?[1]?.as<int>();
assert(x == 1);

// or, if you are sure
int y = any[2]!['x']![1]!();
assert(y == 1);
}
7 changes: 5 additions & 2 deletions test/realm_value_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ Future<void> main([List<String>? args]) async {
ObjectId.fromTimestamp(now),
Uuid.v4(),
Decimal128.fromDouble(128.128),
Uint8List.fromList([1, 2, 0])
Uint8List.fromList([1, 2, 0]),
[1, 2, 3],
{1, 2, 3},
{'a': 1, 'b': 2},
];

for (final x in values) {
Expand All @@ -80,7 +83,7 @@ Future<void> main([List<String>? args]) async {
test('Illegal value', () {
final config = Configuration.local([AnythingGoes.schema, Stuff.schema, TuckedIn.schema]);
final realm = getRealm(config);
expect(() => realm.write(() => realm.add(AnythingGoes(oneAny: RealmValue.from(<int>[1, 2])))), throwsArgumentError);
expect(() => realm.write(() => realm.add(AnythingGoes(oneAny: RealmValue.from((1, 2))))), throwsArgumentError); // records not supported
});

test('Embedded object not allowed in RealmValue', () {
Expand Down

0 comments on commit 717d5ba

Please sign in to comment.