22
33#include " rpc/common/Concepts.hpp"
44#include " rpc/common/Types.hpp"
5- #include " util/UnsupportedType.hpp"
65
76#include < boost/json/value.hpp>
7+ #include < rpcspec/WarningsToJson.hpp>
8+
9+ #include < utility>
810
911namespace rpc ::impl {
1012
@@ -19,37 +21,61 @@ struct DefaultProcessor final {
1921 {
2022 using boost::json::value_from;
2123 using boost::json::value_to;
22- if constexpr (SomeHandlerWithInput<HandlerType>) {
23- // first we run validation against specified API version
2424
25+ static_assert (
26+ kIsSingleInputPath <HandlerType>,
27+ " handler satisfies both the legacy and the typed input path; dispatch would be "
28+ " decided by the order of the branches below rather than by the handler"
29+ );
30+ static_assert (
31+ SomeHandlerWithTypedInput<HandlerType> or SomeHandlerWithInput<HandlerType> or
32+ SomeHandlerWithoutInput<HandlerType>,
33+ " handler matches none of the branches below"
34+ );
35+
36+ // New `rpc-spec`-based handler
37+ if constexpr (SomeHandlerWithTypedInput<HandlerType>) {
38+ auto input = HandlerType::parseInput (value, ctx.apiVersion );
39+ auto warnings = rpc::spec::toJsonArray (HandlerType::spec (ctx.apiVersion ).check (value));
40+
41+ if (not input.has_value ())
42+ return ReturnType{Error{std::move (input).error ()}, std::move (warnings)};
43+
44+ auto ret = handler.process (*input, ctx);
45+
46+ if (not ret.has_value ())
47+ return ReturnType{Error{std::move (ret).error ()}, std::move (warnings)};
48+
49+ return ReturnType{value_from (std::move (ret).value ()), std::move (warnings)};
50+ }
51+
52+ if constexpr (SomeHandlerWithInput<HandlerType>) {
53+ // Old spec-based handler: first we run validation against specified API version
54+ // TODO: This will be eventually removed once fully migraded to new rpc-spec system.
2555 auto const spec = handler.spec (ctx.apiVersion );
2656 auto warnings = spec.check (value);
2757 auto input = value; // copy here, spec require mutable data
2858
29- if (auto const ret = spec.process (input); not ret)
59+ if (auto const ret = spec.process (input); not ret. has_value () )
3060 return ReturnType{Error{ret.error ()}, std::move (warnings)}; // forward Status
3161
3262 auto const inData = value_to<typename HandlerType::Input>(input);
3363 auto ret = handler.process (inData, ctx);
3464
3565 // 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- }
66+ if (not ret.has_value ())
67+ return ReturnType{Error{std::move (ret).error ()}, std::move (warnings)};
68+
4169 return ReturnType{value_from (std::move (ret).value ()), std::move (warnings)};
42- } else if constexpr (SomeHandlerWithoutInput<HandlerType>) {
70+ }
71+
72+ if constexpr (SomeHandlerWithoutInput<HandlerType>) {
4373 // no input to pass, ignore the value
4474 auto const ret = handler.process (ctx);
45- if (not ret) {
75+ if (not ret. has_value ())
4676 return ReturnType{Error{ret.error ()}}; // forward Status
47- }
77+
4878 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>);
5379 }
5480 }
5581};
0 commit comments