Add objectSchema method to Realm Object (#1055)

* Add objectSchema property to Realm Object

* fix
This commit is contained in:
Adam Fish 2017-06-20 02:42:43 -07:00 committed by Yavor Georgiev
parent 28fe678a1b
commit 37dba6bb36
3 changed files with 26 additions and 3 deletions

View File

@ -30,6 +30,7 @@ export default class RealmObject {
// Non-mutating methods:
createMethods(RealmObject.prototype, objectTypes.OBJECT, [
'isValid',
'objectSchema'
]);
export function clearRegisteredConstructors() {

View File

@ -18,12 +18,13 @@
#pragma once
#include "object_accessor.hpp"
#include "object_store.hpp"
#include "js_class.hpp"
#include "js_types.hpp"
#include "js_util.hpp"
#include "object_accessor.hpp"
#include "object_store.hpp"
#include "js_schema.hpp"
namespace realm {
namespace js {
@ -49,6 +50,7 @@ struct RealmObjectClass : ClassDefinition<T, realm::Object> {
static std::vector<String> get_property_names(ContextType, ObjectType);
static void is_valid(ContextType, FunctionType, ObjectType, size_t, const ValueType [], ReturnValue &);
static void get_object_schema(ContextType, FunctionType, ObjectType, size_t, const ValueType [], ReturnValue &);
const std::string name = "RealmObject";
@ -60,6 +62,7 @@ struct RealmObjectClass : ClassDefinition<T, realm::Object> {
MethodMap<T> const methods = {
{"isValid", wrap<is_valid>},
{"objectSchema", wrap<get_object_schema>},
};
};
@ -68,6 +71,12 @@ void RealmObjectClass<T>::is_valid(ContextType ctx, FunctionType, ObjectType thi
return_value.set(get_internal<T, RealmObjectClass<T>>(this_object)->is_valid());
}
template<typename T>
void RealmObjectClass<T>::get_object_schema(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
auto object = get_internal<T, RealmObjectClass<T>>(this_object);
return_value.set(Schema<T>::object_for_object_schema(ctx, object->get_object_schema()));
}
template<typename T>
typename T::Object RealmObjectClass<T>::create_instance(ContextType ctx, realm::Object realm_object) {
static String prototype_string = "prototype";

View File

@ -471,6 +471,19 @@ module.exports = {
});
},
testObjectSchema: function() {
var realm = new Realm({schema: [schemas.TestObject]});
var obj;
realm.write(function() {
obj = realm.create('TestObject', {doubleCol: 1});
});
const schema = obj.objectSchema();
TestCase.assertEqual(schema.name, schemas.TestObject.name);
TestCase.assertArraysEqual(Object.keys(schema.properties), Object.keys(schemas.TestObject.properties));
TestCase.assertEqual(schema.properties.doubleCol.type, 'double');
},
testIgnoredProperties: function() {
var realm = new Realm({schema: [schemas.TestObject]});
var obj;