diff --git a/src/rpc.cpp b/src/rpc.cpp index 896fad8a..41787f4d 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -34,6 +34,7 @@ using namespace realm::rpc; using Accessor = realm::js::NativeAccessor; +namespace { static const char * const RealmObjectTypesData = "data"; static const char * const RealmObjectTypesDate = "date"; 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 RealmObjectTypesUndefined = "undefined"; -static RPCServer*& get_rpc_server(JSGlobalContextRef ctx) { +json serialize_object_schema(const realm::ObjectSchema &object_schema) { + std::vector 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 +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 s_map; return s_map[ctx]; } +} RPCWorker::RPCWorker() { m_thread = std::thread([this]() { @@ -144,15 +175,15 @@ RPCServer::RPCServer() { if (!realm_constructor) { throw std::runtime_error("Realm constructor not found!"); } - + JSObjectRef sync_constructor = (JSObjectRef)jsc::Object::get_property(m_context, realm_constructor, "Sync"); JSObjectRef user_constructor = (JSObjectRef)jsc::Object::get_property(m_context, sync_constructor, "User"); JSObjectRef create_user_method = (JSObjectRef)jsc::Object::get_property(m_context, user_constructor, "createUser"); - + json::array_t args = dict["arguments"]; size_t arg_count = args.size(); JSValueRef arg_values[arg_count]; - + for (size_t i = 0; i < arg_count; i++) { arg_values[i] = deserialize_json_value(args[i]); } @@ -213,11 +244,11 @@ RPCServer::RPCServer() { if (!realm_constructor) { throw std::runtime_error("Realm constructor not found!"); } - + JSObjectRef sync_constructor = (JSObjectRef)jsc::Object::get_property(m_context, realm_constructor, "Sync"); JSObjectRef user_constructor = (JSObjectRef)jsc::Object::get_property(m_context, sync_constructor, "User"); JSValueRef value = jsc::Object::get_property(m_context, user_constructor, "all"); - + return (json){{"result", serialize_json_value(value)}}; }; m_requests["/clear_test_state"] = [this](const json dict) { @@ -360,7 +391,7 @@ json RPCServer::serialize_json_value(JSValueRef js_value) { {"type", RealmObjectTypesList}, {"id", store_object(js_object)}, {"size", list->size()}, - {"schema", serialize_object_schema(list->get_object_schema())} + {"schema", get_type(*list)}, }; } else if (jsc::Object::is_instance>(m_context, js_object)) { @@ -369,7 +400,7 @@ json RPCServer::serialize_json_value(JSValueRef js_value) { {"type", RealmObjectTypesResults}, {"id", store_object(js_object)}, {"size", results->size()}, - {"schema", serialize_object_schema(results->get_object_schema())} + {"schema", get_type(*results)}, }; } else if (jsc::Object::is_instance>(m_context, js_object)) { @@ -455,23 +486,6 @@ json RPCServer::serialize_json_value(JSValueRef js_value) { assert(0); } -json RPCServer::serialize_object_schema(const realm::ObjectSchema &object_schema) { - std::vector 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) { json oid = dict["id"]; if (oid.is_number()) { diff --git a/src/rpc.hpp b/src/rpc.hpp index ca253e0a..ed286d05 100644 --- a/src/rpc.hpp +++ b/src/rpc.hpp @@ -81,8 +81,6 @@ class RPCServer { json serialize_json_value(JSValueRef value); JSValueRef deserialize_json_value(const json dict); - - json serialize_object_schema(const ObjectSchema &objectSchema); }; } // rpc