Skip to content

Commit

Permalink
Committing clang-format changes
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenberry committed Aug 24, 2023
1 parent 6e80ce8 commit c87fa90
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 61 deletions.
67 changes: 32 additions & 35 deletions include/glaze/ext/jsonrpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -56,41 +56,41 @@ namespace glz::rpc
error_e code{error_e::no_error};
std::optional<std::string> 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<std::string>& data) : code(code), data(data) {}
error(error_e code, const std::optional<std::string>& data, const std::string& message) : code(code), data(data), message(message) {}
error(error_e code, const std::optional<std::string>& 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
{
Expand All @@ -116,10 +116,10 @@ namespace glz::rpc
"id", &T::id);
};
};

template <class params_type>
request_t(id_t&&, std::string_view, params_type&&) -> request_t<std::decay_t<params_type>>;

using generic_request_t = request_t<glz::raw_json_view>;

template <class result_type>
Expand All @@ -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_t> result{}; // todo can this be instead expected<result_t, error>
std::optional<rpc::error> error{};
std::string version{rpc::supported_version};

struct glaze
{
using T = response_t;
Expand All @@ -149,20 +145,20 @@ namespace glz::rpc
};
};
using generic_response_t = response_t<glz::raw_json_view>;

template <basic_fixed_string name, class params_type, class result_type>
struct method
{
static constexpr std::string_view name_v{name};
using params_t = params_type;
using result_t = result_type;
};

namespace concepts
{
template <class T>
concept method_type = requires(T) {
T::name_v;
T::name_v;
{
std::same_as<decltype(T::name_v), std::string_view>
};
Expand All @@ -183,8 +179,9 @@ namespace glz::rpc
using result_t = typename Method::result_t;
using request_t = rpc::request_t<params_t>;
using response_t = rpc::response_t<result_t>;
std::function<expected<result_t, rpc::error>(const params_t&)> callback{
[](const auto&) { return glz::unexpected{rpc::error{rpc::error_e::internal, "Not implemented"}}; }};
std::function<expected<result_t, rpc::error>(const params_t&)> callback{[](const auto&) {
return glz::unexpected{rpc::error{rpc::error_e::internal, "Not implemented"}};
}};
};

template <concepts::method_type Method>
Expand Down Expand Up @@ -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<std::vector<glz::raw_json_view>>(json_request)};
Expand Down Expand Up @@ -437,7 +434,8 @@ namespace glz::rpc
if (!id_found) [[unlikely]] {
if (!return_v) {
if (std::holds_alternative<std::string_view>(res.id)) {
return_v = rpc::error{error_e::internal, "id: '" + std::string(std::get<std::string_view>(res.id)) + "' not found"};
return_v = rpc::error{error_e::internal,
"id: '" + std::string(std::get<std::string_view>(res.id)) + "' not found"};
}
else {
return_v = rpc::error{error_e::internal, "id: " + glz::write_json(res.id) + " not found"};
Expand Down Expand Up @@ -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<decltype(id)>(id), method_name.view(),
std::forward<decltype(params)>(params)};
rpc::request_t req{std::forward<decltype(id)>(id), method_name.view(), std::forward<decltype(params)>(params)};

if (std::holds_alternative<glz::json_t::null_t>(id)) {
return {glz::write_json(std::move(req)), false};
Expand Down
13 changes: 5 additions & 8 deletions include/glaze/thread/threadpool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

#include <atomic>
#include <condition_variable>
#include <list>
#include <functional>
#include <future>
#include <list>
#include <mutex>
#include <thread>
#include <vector>
Expand All @@ -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) {
Expand Down Expand Up @@ -72,7 +72,7 @@ namespace glz

return promise->get_future();
}

// Takes a function whose input is the thread number (size_t)
template <class F>
requires std::invocable<std::decay_t<F>, size_t>
Expand Down Expand Up @@ -123,10 +123,7 @@ namespace glz

size_t size() const { return threads.size(); }

~pool()
{
finish_work();
}
~pool() { finish_work(); }

private:
std::vector<std::thread> threads;
Expand All @@ -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
Expand Down
34 changes: 16 additions & 18 deletions tests/jsonrpc_test/jsonrpc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<int, rpc::error> value, rpc::id_t id) -> void {
called = true;
ut::expect(value.has_value());
ut::expect(value.value() == 6);
ut::expect(std::holds_alternative<std::int64_t>(id));
ut::expect(std::get<std::int64_t>(id) == std::int64_t{1});
})};
auto request_str{client.request<"summer">(1, std::vector{1, 2, 3},
[&called](glz::expected<int, rpc::error> value, rpc::id_t id) -> void {
called = true;
ut::expect(value.has_value());
ut::expect(value.value() == 6);
ut::expect(std::holds_alternative<std::int64_t>(id));
ut::expect(std::get<std::int64_t>(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">();
Expand Down Expand Up @@ -138,10 +138,8 @@ struct glz::meta<bar_result>
};

ut::suite struct_test_cases = [] {
rpc::server<rpc::method<"foo", foo_params, foo_result>, rpc::method<"bar", bar_params, bar_result>>
server;
rpc::client<rpc::method<"foo", foo_params, foo_result>, rpc::method<"bar", bar_params, bar_result>>
client;
rpc::server<rpc::method<"foo", foo_params, foo_result>, rpc::method<"bar", bar_params, bar_result>> server;
rpc::client<rpc::method<"foo", foo_params, foo_result>, rpc::method<"bar", bar_params, bar_result>> client;

ut::test("valid foo request") = [&server, &client] {
bool called{};
Expand Down Expand Up @@ -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);
Expand All @@ -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);
};
Expand Down

0 comments on commit c87fa90

Please sign in to comment.