Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Processors.hpp
1#pragma once
2
3#include "rpc/common/Concepts.hpp"
4#include "rpc/common/Types.hpp"
5#include "util/UnsupportedType.hpp"
6
7#include <boost/json/value.hpp>
8
9namespace rpc::impl {
10
11template <SomeHandler HandlerType>
12struct DefaultProcessor final {
13 [[nodiscard]] ReturnType
14 operator()(
15 HandlerType const& handler,
16 boost::json::value const& value,
17 Context const& ctx
18 ) const
19 {
20 using boost::json::value_from;
21 using boost::json::value_to;
23 // first we run validation against specified API version
24
25 auto const spec = handler.spec(ctx.apiVersion);
26 auto warnings = spec.check(value);
27 auto input = value; // copy here, spec require mutable data
28
29 if (auto const ret = spec.process(input); not ret)
30 return ReturnType{Error{ret.error()}, std::move(warnings)}; // forward Status
31
32 auto const inData = value_to<typename HandlerType::Input>(input);
33 auto ret = handler.process(inData, ctx);
34
35 // real handler is given expected Input, not json
36 if (!ret) {
37 return ReturnType{
38 Error{std::move(ret).error()}, std::move(warnings)
39 }; // forward Status
40 }
41 return ReturnType{value_from(std::move(ret).value()), std::move(warnings)};
42 } else if constexpr (SomeHandlerWithoutInput<HandlerType>) {
43 // no input to pass, ignore the value
44 auto const ret = handler.process(ctx);
45 if (not ret) {
46 return ReturnType{Error{ret.error()}}; // forward Status
47 }
48 return ReturnType{value_from(ret.value())};
49 } else {
50 // when concept SomeHandlerWithInput and SomeHandlerWithoutInput not cover all Handler
51 // case
52 static_assert(util::Unsupported<HandlerType>);
53 }
54 }
55};
56
57} // namespace rpc::impl
Specifies what a Handler with Input must provide.
Definition Concepts.hpp:70
Specifies what a Handler without Input must provide.
Definition Concepts.hpp:78
std::unexpected< Status > Error
The type that represents just the error part of MaybeError.
Definition Types.hpp:56
static constexpr bool Unsupported
used for compile time checking of unsupported types
Definition UnsupportedType.hpp:7
Context of an RPC call.
Definition Types.hpp:99
The final return type out of RPC engine.
Definition Types.hpp:67
Definition Processors.hpp:12