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

Get Realm.Object property type #3938

Merged
merged 4 commits into from
Sep 13, 2021
Merged

Get Realm.Object property type #3938

merged 4 commits into from
Sep 13, 2021

Conversation

zepedr0
Copy link
Contributor

@zepedr0 zepedr0 commented Sep 1, 2021

What, How & Why?

Since all numeric types (int, long, double) are represented as a number when read from our SDK, it makes sense to implement a method to get the underlying type of the property value, if your application (such as MongoDB Realm Studio) cares about this. Throws an exception if a property with the given name does not exist.

E.g.

realmObject.getPropertyType(propertyName);

This closes #3646

☑️ ToDos

  • 📝 Changelog entry
  • [ ] 📝 Compatibility label is updated or copied from previous entry
  • 🚦 Tests
  • 📝 Public documentation PR created or is not necessary
  • [ ] 💥 Breaking label has been applied or is not necessary

If this PR adds or changes public API's:

  • typescript definitions file is updated
  • jsdoc files updated
  • Chrome debug API is updated if API is available on React Native

Copy link
Member

@kraenhansen kraenhansen left a comment

Choose a reason for hiding this comment

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

Great job! Feel free to merge as soon as the tests are green.


testGetPropertyType: function () {
const realm = new Realm({
schema: [schemas.AllTypes, schemas.TestObject, schemas.LinkToAllTypes],
Copy link
Member

@kraenhansen kraenhansen Sep 1, 2021

Choose a reason for hiding this comment

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

Oh actually: I would love to see a test using a property of type mixed as well. To verify that it's the underlying type and not simply the type from the schema which gets returned.

@bmunkholm
Copy link
Contributor

This also needs an updated changelog.md

Copy link
Contributor

@takameyer takameyer left a comment

Choose a reason for hiding this comment

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

👍🏼

throw std::invalid_argument(util::format("No such property: %1", property_name));
}

return_value.set(prop->type_string());
Copy link
Contributor

Choose a reason for hiding this comment

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

To get the JavaScript names, you need to call TypeErrorException::type_string(prop) (https://github.com/realm/realm-js/blob/master/src/js_realm.cpp#L78).

If prop->type_string() == "mixed", you need to unwrap the property. It is something like:

auto obj = realm_object->obj();
DataType typ = obj.get().get_type();
// convert typ to string

@kneth kneth force-pushed the jose/get-property-type branch from 794a028 to f9fb1dd Compare September 10, 2021 13:12
Copy link
Contributor

@takameyer takameyer left a comment

Choose a reason for hiding this comment

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

👍🏼

@kneth kneth merged commit e85bfee into master Sep 13, 2021
@kneth kneth deleted the jose/get-property-type branch September 13, 2021 11:05
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mixed: Get underlying type of a mixed property
5 participants