diff --git a/include/glaze/ext/jsonrpc.hpp b/include/glaze/ext/jsonrpc.hpp index 5e8f95e80a..6b15d0da95 100644 --- a/include/glaze/ext/jsonrpc.hpp +++ b/include/glaze/ext/jsonrpc.hpp @@ -21,7 +21,7 @@ namespace glz::rpc internal = -32603, parse_error = -32700, }; - + inline constexpr std::string_view code_as_sv(const error_e error_code) noexcept { switch (error_code) { @@ -56,41 +56,41 @@ namespace glz::rpc error_e code{error_e::no_error}; std::optional data{}; // Optional detailed error information std::string message{code_as_sv(code)}; // string reflection of member variable code - + // TODO: remove all these constructors when MSVC is fixed error() = default; error(error_e code) : code(code) {} error(error_e code, const std::optional& data) : code(code), data(data) {} - error(error_e code, const std::optional& data, const std::string& message) : code(code), data(data), message(message) {} + error(error_e code, const std::optional& data, const std::string& message) + : code(code), data(data), message(message) + {} error(const error&) = default; error(error&&) = default; error& operator=(const error&) = default; error& operator=(error&&) = default; - + static error invalid(const parse_error& pe, auto& buffer) { std::string format_err{format_error(pe, buffer)}; - return {error_e::invalid_request, format_err.empty() ? std::nullopt : std::optional{format_err}, std::string(code_as_sv(error_e::invalid_request))}; + return {error_e::invalid_request, format_err.empty() ? std::nullopt : std::optional{format_err}, + std::string(code_as_sv(error_e::invalid_request))}; } static error version(std::string_view presumed_version) { - return {error_e::invalid_request, "Invalid version: " + std::string(presumed_version) + - " only supported version is " + std::string(rpc::supported_version), std::string(code_as_sv(error_e::invalid_request))}; + return {error_e::invalid_request, + "Invalid version: " + std::string(presumed_version) + " only supported version is " + + std::string(rpc::supported_version), + std::string(code_as_sv(error_e::invalid_request))}; } static error method(std::string_view presumed_method) { - return {error_e::method_not_found, "Method: '" + std::string(presumed_method) + "' not found", std::string(code_as_sv(error_e::method_not_found))}; - } - - operator bool() const noexcept - { - return code != rpc::error_e::no_error; + return {error_e::method_not_found, "Method: '" + std::string(presumed_method) + "' not found", + std::string(code_as_sv(error_e::method_not_found))}; } - bool operator==(const rpc::error_e err) const noexcept - { - return code == err; - } + operator bool() const noexcept { return code != rpc::error_e::no_error; } + + bool operator==(const rpc::error_e err) const noexcept { return code == err; } struct glaze { @@ -116,10 +116,10 @@ namespace glz::rpc "id", &T::id); }; }; - + template request_t(id_t&&, std::string_view, params_type&&) -> request_t>; - + using generic_request_t = request_t; template @@ -129,18 +129,14 @@ namespace glz::rpc response_t() = default; explicit response_t(rpc::error&& err) : error(std::move(err)) {} - response_t(id_t&& id, result_t&& result) - : id(std::move(id)), result(std::move(result)) - {} - response_t(id_t&& id, rpc::error&& err) - : id(std::move(id)), error(std::move(err)) - {} + response_t(id_t&& id, result_t&& result) : id(std::move(id)), result(std::move(result)) {} + response_t(id_t&& id, rpc::error&& err) : id(std::move(id)), error(std::move(err)) {} id_t id{}; std::optional result{}; // todo can this be instead expected std::optional error{}; std::string version{rpc::supported_version}; - + struct glaze { using T = response_t; @@ -149,7 +145,7 @@ namespace glz::rpc }; }; using generic_response_t = response_t; - + template struct method { @@ -157,12 +153,12 @@ namespace glz::rpc using params_t = params_type; using result_t = result_type; }; - + namespace concepts { template concept method_type = requires(T) { - T::name_v; + T::name_v; { std::same_as }; @@ -183,8 +179,9 @@ namespace glz::rpc using result_t = typename Method::result_t; using request_t = rpc::request_t; using response_t = rpc::response_t; - std::function(const params_t&)> callback{ - [](const auto&) { return glz::unexpected{rpc::error{rpc::error_e::internal, "Not implemented"}}; }}; + std::function(const params_t&)> callback{[](const auto&) { + return glz::unexpected{rpc::error{rpc::error_e::internal, "Not implemented"}}; + }}; }; template @@ -302,7 +299,7 @@ namespace glz::rpc if (auto parse_err{glz::validate_json(json_request)}) { return return_helper( - raw_response_t{rpc::error{error_e::parse_error, format_error(parse_err, json_request)}}); + raw_response_t{rpc::error{error_e::parse_error, format_error(parse_err, json_request)}}); } auto batch_requests{glz::read_json>(json_request)}; @@ -437,7 +434,8 @@ namespace glz::rpc if (!id_found) [[unlikely]] { if (!return_v) { if (std::holds_alternative(res.id)) { - return_v = rpc::error{error_e::internal, "id: '" + std::string(std::get(res.id)) + "' not found"}; + return_v = rpc::error{error_e::internal, + "id: '" + std::string(std::get(res.id)) + "' not found"}; } else { return_v = rpc::error{error_e::internal, "id: " + glz::write_json(res.id) + " not found"}; @@ -467,8 +465,7 @@ namespace glz::rpc ...); static_assert(method_params_match, "Method name and given params type do not match."); - rpc::request_t req{std::forward(id), method_name.view(), - std::forward(params)}; + rpc::request_t req{std::forward(id), method_name.view(), std::forward(params)}; if (std::holds_alternative(id)) { return {glz::write_json(std::move(req)), false}; diff --git a/include/glaze/thread/threadpool.hpp b/include/glaze/thread/threadpool.hpp index b786c09331..7567b2e388 100644 --- a/include/glaze/thread/threadpool.hpp +++ b/include/glaze/thread/threadpool.hpp @@ -5,9 +5,9 @@ #include #include -#include #include #include +#include #include #include #include @@ -26,7 +26,7 @@ namespace glz finish_work(); // finish any active work std::lock_guard lock(mtx); closed = false; - + threads.clear(); threads.reserve(n); for (size_t i = 0; i < n; ++i) { @@ -72,7 +72,7 @@ namespace glz return promise->get_future(); } - + // Takes a function whose input is the thread number (size_t) template requires std::invocable, size_t> @@ -123,10 +123,7 @@ namespace glz size_t size() const { return threads.size(); } - ~pool() - { - finish_work(); - } + ~pool() { finish_work(); } private: std::vector threads; @@ -137,7 +134,7 @@ namespace glz std::mutex mtx; std::condition_variable work_cv; std::condition_variable done_cv; - + void finish_work() { // Close the queue and finish all the remaining work diff --git a/tests/jsonrpc_test/jsonrpc_test.cpp b/tests/jsonrpc_test/jsonrpc_test.cpp index 72adb96d9f..c4c8302cff 100644 --- a/tests/jsonrpc_test/jsonrpc_test.cpp +++ b/tests/jsonrpc_test/jsonrpc_test.cpp @@ -21,15 +21,15 @@ ut::suite valid_vector_test_cases_server = [] { const std::array valid_requests = { std::pair(R"({"jsonrpc": "2.0","method": "add", "params": [1, 2, 3],"id": 1})", - R"({"jsonrpc": "2.0","result": 6,"id": 1})"), + R"({"jsonrpc": "2.0","result": 6,"id": 1})"), std::pair( // No id is valid R"({"jsonrpc": "2.0","method": "add", "params": [1, 2, 3]})", ""), std::pair(R"({"jsonrpc": "2.0","method": "add", "params": [1, 2, 3],"id": null})", ""), std::pair(R"({"jsonrpc": "2.0","method": "add", "params": [1, 2, 3],"id": 2.0})", - R"({"jsonrpc": "2.0","result": 6, "id": 2})"), + R"({"jsonrpc": "2.0","result": 6, "id": 2})"), std::pair(R"({"jsonrpc": "2.0","method": "add","params": [1, 2, 3],"id": "some_client_22"})", - R"({"jsonrpc": "2.0","result": 6, "id": "some_client_22"})")}; + R"({"jsonrpc": "2.0","result": 6, "id": "some_client_22"})")}; std::string raw_json; std::string resulting_request; @@ -62,14 +62,14 @@ ut::suite vector_test_cases = [] { ut::test("sum_result = 6") = [&server, &client] { bool called{}; - auto request_str{client.request<"summer">( - 1, std::vector{1, 2, 3}, [&called](glz::expected value, rpc::id_t id) -> void { - called = true; - ut::expect(value.has_value()); - ut::expect(value.value() == 6); - ut::expect(std::holds_alternative(id)); - ut::expect(std::get(id) == std::int64_t{1}); - })}; + auto request_str{client.request<"summer">(1, std::vector{1, 2, 3}, + [&called](glz::expected value, rpc::id_t id) -> void { + called = true; + ut::expect(value.has_value()); + ut::expect(value.value() == 6); + ut::expect(std::holds_alternative(id)); + ut::expect(std::get(id) == std::int64_t{1}); + })}; ut::expect(request_str.first == R"({"jsonrpc":"2.0","method":"summer","params":[1,2,3],"id":1})"); [[maybe_unused]] auto& requests = client.get_request_map<"summer">(); @@ -138,10 +138,8 @@ struct glz::meta }; ut::suite struct_test_cases = [] { - rpc::server, rpc::method<"bar", bar_params, bar_result>> - server; - rpc::client, rpc::method<"bar", bar_params, bar_result>> - client; + rpc::server, rpc::method<"bar", bar_params, bar_result>> server; + rpc::client, rpc::method<"bar", bar_params, bar_result>> client; ut::test("valid foo request") = [&server, &client] { bool called{}; @@ -284,7 +282,8 @@ ut::suite struct_test_cases = [] { R"([{"jsonrpc":"2.0","method":"invalid_method_name","params":{},"id":"uuid"},{"jsonrpc":"2.0","method":"invalid_method_name","params":]")"); ut::expect(response_vec.size() == 1); auto s = glz::write_json(response_vec); - ut::expect(s == + ut::expect( + s == R"([{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error","data":"1:132: syntax_error\n [{\"jsonrpc\":\"2.0\",\"method\":\"invalid_method_name\",\"params\":{},\"id\":\"uuid\"},{\"jsonrpc\":\"2.0\",\"method\":\"invalid_method_name\",\"params\":]\"\n ^\n"},"id":null}])"); ut::expect(response_vec.at(0).error.has_value()); ut::expect(response_vec.at(0).error->code == rpc::error_e::parse_error); @@ -297,8 +296,7 @@ ut::suite struct_test_cases = [] { ut::expect(response_vec.size() == 1); auto s = glz::write_json(response_vec); - ut::expect(s == - R"([{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":null}])"); + ut::expect(s == R"([{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":null}])"); ut::expect(response_vec.at(0).error.has_value()); ut::expect(response_vec.at(0).error->code == rpc::error_e::invalid_request); };