![]() |
dfx 0.1.0
Linux-based dynamic dataflow executor
|
Small helper base used to register UnixRouter commands with JSON params validation. More...
#include <dfx-server/command-handlers/BaseCommandHandler.hpp>
Public Member Functions | |
| BaseCommandHandler ()=default | |
| Default constructor. | |
Protected Member Functions | |
| template<typename PMF> | |
| void | registerNewCommand (UnixRouter &router, std::string command, nlohmann::json const &schema, PMF handler) |
| Register a new command in the router with an associated params schema and member handler. | |
| bool | validateParams (std::string_view command, nlohmann::json const ¶ms, std::string_view id, UnixSessionPtr session) const |
| Validate params for a command and reply with errors on failure. | |
Small helper base used to register UnixRouter commands with JSON params validation.
This class is primarily a convenience layer over dfx::Server::UnixRouter.
It provides two things:
Despite being a base class, it is not meant for runtime polymorphism. It is a CRTP-like registration helper used by concrete command handler classes.
The member function signature must match the router handler signature:
Example (real usage pattern):
session->replyError(errors, id) and the concrete handler is not called.Validation is performed with the JSON pointer /params as the validation root, so error paths are reported relative to the params object.
|
default |
Default constructor.
Constructs an empty helper with its own Utils::JsonValidator. Concrete handlers typically register their commands during construction.
|
inlineprotected |
Register a new command in the router with an associated params schema and member handler.
This helper enforces at compile time that handler is a pointer to a member function of a class derived from BaseCommandHandler (and publicly so).
Internally, it:
schema into this instance's internal validator under the key command,The registered router handler has the signature: bool (UnixSessionPtr session, nlohmann::json params, std::string id).
| PMF | Pointer-to-member-function type. |
| router | Router that will dispatch incoming requests. |
| command | Command name as used in the JSON request envelope. |
| schema | JSON schema used to validate the request params for this command. |
| handler | Pointer to a member function of the derived handler class. |
|
protected |
Validate params for a command and reply with errors on failure.
Performs schema validation using: Utils::JsonValidator::validate(command, params, "/params"_json_pointer).
On failure, replies with all validation errors via: session->replyError(validation.errors, id).
| command | Command name used as the schema key. |
| params | Parameters object to validate. |
| id | Request identifier to echo back in the error reply. |
| session | Session used to send error replies. |
| true | Parameters are valid. |
| false | Parameters are invalid (an error reply has been sent). |