Fix assertion failures when serializing collections for RPC

This commit is contained in:
Thomas Goyne 2017-09-26 11:34:38 -07:00
parent fdfd89e7e7
commit c7e44cd01f
2 changed files with 39 additions and 27 deletions

View File

@ -34,6 +34,7 @@ using namespace realm::rpc;
using Accessor = realm::js::NativeAccessor<jsc::Types>; using Accessor = realm::js::NativeAccessor<jsc::Types>;
namespace {
static const char * const RealmObjectTypesData = "data"; static const char * const RealmObjectTypesData = "data";
static const char * const RealmObjectTypesDate = "date"; static const char * const RealmObjectTypesDate = "date";
static const char * const RealmObjectTypesDictionary = "dict"; static const char * const RealmObjectTypesDictionary = "dict";
@ -46,10 +47,40 @@ static const char * const RealmObjectTypesUser = "user";
static const char * const RealmObjectTypesSession = "session"; static const char * const RealmObjectTypesSession = "session";
static const char * const RealmObjectTypesUndefined = "undefined"; static const char * const RealmObjectTypesUndefined = "undefined";
static RPCServer*& get_rpc_server(JSGlobalContextRef ctx) { json serialize_object_schema(const realm::ObjectSchema &object_schema) {
std::vector<std::string> properties;
for (auto &prop : object_schema.persisted_properties) {
properties.push_back(prop.name);
}
for (auto &prop : object_schema.computed_properties) {
properties.push_back(prop.name);
}
return {
{"name", object_schema.name},
{"properties", properties},
};
}
template<typename Container>
json get_type(Container const& c) {
auto type = c.get_type();
if (type == realm::PropertyType::Object) {
return serialize_object_schema(c.get_object_schema());
}
return {
{"type", string_for_property_type(type)},
{"optional", is_nullable(type)}
};
}
RPCServer*& get_rpc_server(JSGlobalContextRef ctx) {
static std::map<JSGlobalContextRef, RPCServer*> s_map; static std::map<JSGlobalContextRef, RPCServer*> s_map;
return s_map[ctx]; return s_map[ctx];
} }
}
RPCWorker::RPCWorker() { RPCWorker::RPCWorker() {
m_thread = std::thread([this]() { m_thread = std::thread([this]() {
@ -360,7 +391,7 @@ json RPCServer::serialize_json_value(JSValueRef js_value) {
{"type", RealmObjectTypesList}, {"type", RealmObjectTypesList},
{"id", store_object(js_object)}, {"id", store_object(js_object)},
{"size", list->size()}, {"size", list->size()},
{"schema", serialize_object_schema(list->get_object_schema())} {"schema", get_type(*list)},
}; };
} }
else if (jsc::Object::is_instance<js::ResultsClass<jsc::Types>>(m_context, js_object)) { else if (jsc::Object::is_instance<js::ResultsClass<jsc::Types>>(m_context, js_object)) {
@ -369,7 +400,7 @@ json RPCServer::serialize_json_value(JSValueRef js_value) {
{"type", RealmObjectTypesResults}, {"type", RealmObjectTypesResults},
{"id", store_object(js_object)}, {"id", store_object(js_object)},
{"size", results->size()}, {"size", results->size()},
{"schema", serialize_object_schema(results->get_object_schema())} {"schema", get_type(*results)},
}; };
} }
else if (jsc::Object::is_instance<js::RealmClass<jsc::Types>>(m_context, js_object)) { else if (jsc::Object::is_instance<js::RealmClass<jsc::Types>>(m_context, js_object)) {
@ -455,23 +486,6 @@ json RPCServer::serialize_json_value(JSValueRef js_value) {
assert(0); assert(0);
} }
json RPCServer::serialize_object_schema(const realm::ObjectSchema &object_schema) {
std::vector<std::string> properties;
for (auto &prop : object_schema.persisted_properties) {
properties.push_back(prop.name);
}
for (auto &prop : object_schema.computed_properties) {
properties.push_back(prop.name);
}
return {
{"name", object_schema.name},
{"properties", properties},
};
}
JSValueRef RPCServer::deserialize_json_value(const json dict) { JSValueRef RPCServer::deserialize_json_value(const json dict) {
json oid = dict["id"]; json oid = dict["id"];
if (oid.is_number()) { if (oid.is_number()) {

View File

@ -81,8 +81,6 @@ class RPCServer {
json serialize_json_value(JSValueRef value); json serialize_json_value(JSValueRef value);
JSValueRef deserialize_json_value(const json dict); JSValueRef deserialize_json_value(const json dict);
json serialize_object_schema(const ObjectSchema &objectSchema);
}; };
} // rpc } // rpc