Construct Results with object type name only (#959)

This commit is contained in:
Yavor Georgiev 2017-04-18 13:41:44 +03:00 committed by GitHub
parent 8b652b4e8a
commit aa506935a5
2 changed files with 17 additions and 10 deletions

View File

@ -244,9 +244,7 @@ public:
return name; return name;
} }
static const ObjectSchema& validated_object_schema_for_value(ContextType ctx, const SharedRealm &realm, const ValueType &value) { static const ObjectSchema& validated_object_schema_for_value(ContextType ctx, const SharedRealm &realm, const ValueType &value, std::string& object_type) {
std::string object_type;
if (Value::is_constructor(ctx, value)) { if (Value::is_constructor(ctx, value)) {
FunctionType constructor = Value::to_constructor(ctx, value); FunctionType constructor = Value::to_constructor(ctx, value);
@ -264,6 +262,9 @@ public:
} }
else { else {
object_type = Value::validated_to_string(ctx, value, "objectType"); object_type = Value::validated_to_string(ctx, value, "objectType");
if (object_type.empty()) {
throw std::runtime_error("objectType cannot be empty");
}
} }
auto &schema = realm->schema(); auto &schema = realm->schema();
@ -543,9 +544,10 @@ void RealmClass<T>::objects(ContextType ctx, FunctionType, ObjectType this_objec
validate_argument_count(argc, 1); validate_argument_count(argc, 1);
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object); SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
auto &object_schema = validated_object_schema_for_value(ctx, realm, arguments[0]); std::string object_type;
validated_object_schema_for_value(ctx, realm, arguments[0], object_type);
return_value.set(ResultsClass<T>::create_instance(ctx, realm, object_schema)); return_value.set(ResultsClass<T>::create_instance(ctx, realm, object_type));
} }
template<typename T> template<typename T>
@ -553,7 +555,8 @@ void RealmClass<T>::object_for_primary_key(ContextType ctx, FunctionType, Object
validate_argument_count(argc, 2); validate_argument_count(argc, 2);
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object); SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
auto &object_schema = validated_object_schema_for_value(ctx, realm, arguments[0]); std::string object_type;
auto &object_schema = validated_object_schema_for_value(ctx, realm, arguments[0], object_type);
auto realm_object = realm::Object::get_for_primary_key(ctx, realm, object_schema, arguments[1]); auto realm_object = realm::Object::get_for_primary_key(ctx, realm, object_schema, arguments[1]);
if (realm_object.is_valid()) { if (realm_object.is_valid()) {
@ -569,7 +572,8 @@ void RealmClass<T>::create(ContextType ctx, FunctionType, ObjectType this_object
validate_argument_count(argc, 2, 3); validate_argument_count(argc, 2, 3);
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object); SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
auto &object_schema = validated_object_schema_for_value(ctx, realm, arguments[0]); std::string object_type;
auto &object_schema = validated_object_schema_for_value(ctx, realm, arguments[0], object_type);
ObjectType object = Value::validated_to_object(ctx, arguments[1], "properties"); ObjectType object = Value::validated_to_object(ctx, arguments[1], "properties");
if (Value::is_array(ctx, arguments[1])) { if (Value::is_array(ctx, arguments[1])) {

View File

@ -55,7 +55,7 @@ struct ResultsClass : ClassDefinition<T, realm::js::Results<T>, CollectionClass<
using ReturnValue = js::ReturnValue<T>; using ReturnValue = js::ReturnValue<T>;
static ObjectType create_instance(ContextType, realm::Results); static ObjectType create_instance(ContextType, realm::Results);
static ObjectType create_instance(ContextType, SharedRealm, const ObjectSchema &); static ObjectType create_instance(ContextType, SharedRealm, const std::string &object_type);
template<typename U> template<typename U>
static ObjectType create_filtered(ContextType, const U &, size_t, const ValueType[]); static ObjectType create_filtered(ContextType, const U &, size_t, const ValueType[]);
@ -101,8 +101,11 @@ typename T::Object ResultsClass<T>::create_instance(ContextType ctx, realm::Resu
} }
template<typename T> template<typename T>
typename T::Object ResultsClass<T>::create_instance(ContextType ctx, SharedRealm realm, const ObjectSchema &object_schema) { typename T::Object ResultsClass<T>::create_instance(ContextType ctx, SharedRealm realm, const std::string &object_type) {
auto table = ObjectStore::table_for_object_type(realm->read_group(), object_schema.name); auto table = ObjectStore::table_for_object_type(realm->read_group(), object_type);
if (!table) {
throw std::runtime_error("Table does not exist. Object type: " + object_type);
}
return create_object<T, ResultsClass<T>>(ctx, new realm::js::Results<T>(realm, *table)); return create_object<T, ResultsClass<T>>(ctx, new realm::js::Results<T>(realm, *table));
} }