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

Add support for string based/dynamic API on RealmObject #853

Merged
merged 6 commits into from
Aug 22, 2022

Conversation

nirinchev
Copy link
Member

@nirinchev nirinchev commented Aug 18, 2022

Adds a string-based API to read properties of a RealmObject. Right now we only support reading as that's required to get migrations working, but it can be extended in the future to also support setting.

final obj = realm.dynamic.find("Foo", 123);

// Get simple primitive
final str = obj.dynamic.get<String>("stringProp");

// Get another object
final bar = obj.dynamic.get<RealmObject>("bar");
final barString = bar.dynamic.get<String>("stringProp");

// Get a list of objects
final bars = obj.dynamic.getList<RealmObject>("bars");

// Iterate the list as normal
for (final bar in bars) {
  print(bar.dynamic.get<DateTime>("dateProp");
}

Additionally, implements noSuchMethod on RealmObject in order to support dynamic invocation:

dynamic obj = realm.dynamic.find("Foo", 123);

// Get simple primitive
String str = obj.stringProp;

// Get another object
dynamic bar = obj.bar;
String barString = bar.stringProp;

// Get a list of objects
final bars = obj.bars;

// Iterate the list as normal
for (final bar in bars) {
  print(bar.dateProp);
}

Part of #70.

Note: This only adds support for reading properties, but not for setting them. It won't be difficult to extend the API and support setting of properties, but is out of scope for this PR.

@@ -30,7 +30,7 @@ import 'results.dart';
/// added to or deleted from the collection or from the Realm.
///
/// {@category Realm}
abstract class RealmList<T extends Object> with RealmEntity implements List<T>, Finalizable {
abstract class RealmList<T extends Object?> with RealmEntity implements List<T>, Finalizable {
Copy link
Member Author

Choose a reason for hiding this comment

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

This liberalizes the type args on RealmList to support nullable objects. It doesn't actually add support for lists of nullables - that is done in #708, but is necessary to simplify a lot of the generic constraints elsewhere.

Comment on lines -2056 to +2058
return ref.values.boolean != 0;
return ref.values.boolean;
Copy link
Member Author

Choose a reason for hiding this comment

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

This must be a change with the new ffi and seems like it was a bug - not sure how/why our tests didn't catch it, but likely warrants an investigation.

Copy link
Contributor

Choose a reason for hiding this comment

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

added a test and fixed it in #854. This can be left here as well

for (var isDynamic in [true, false]) {
Realm getDynamicRealm(Realm original) {
Realm _getDynamicRealm(Realm original) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The changes in this file are mostly whitespace due to grouping tests together. I recommend reviewing with ?w=1.

@nirinchev nirinchev self-assigned this Aug 18, 2022
Comment on lines -2056 to +2058
return ref.values.boolean != 0;
return ref.values.boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

added a test and fixed it in #854. This can be left here as well

lib/src/realm_object.dart Outdated Show resolved Hide resolved

import 'list.dart';
import 'native/realm_core.dart';
import 'realm_class.dart';

typedef DartDynamic = dynamic;
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest we don't hide this under our own name

Copy link
Member Author

Choose a reason for hiding this comment

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

This is just a typedef - it's not going to affect users. The reason why I need to typedef it is to avoid a collision with the dynamic property on RealmObject.

Copy link
Contributor

Choose a reason for hiding this comment

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

Won't users be hit by the same?

Copy link
Contributor

@blagoev blagoev left a comment

Choose a reason for hiding this comment

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

I guess we can add more tests for the new validations

@nirinchev nirinchev merged commit 19ebea1 into master Aug 22, 2022
@nirinchev nirinchev deleted the ni/dynamic-realm-object-2 branch August 22, 2022 13:35
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants