4#include "rpc/common/Types.hpp"
5#include "util/JsonUtils.hpp"
7#include <boost/json/value.hpp>
8#include <boost/json/value_to.hpp>
9#include <xrpl/protocol/ErrorCodes.h>
17namespace rpc::modifiers {
22template <
typename Type>
34 explicit Clamp(Type min, Type max) : min_{min}, max_{max}
46 modify(boost::json::value& value, std::string_view key)
const
48 using boost::json::value_to;
50 if (not value.is_object() or not value.as_object().contains(key))
54 auto const oldValue = value_to<Type>(value.as_object().at(key));
55 value.as_object()[key] = std::clamp<Type>(oldValue, min_, max_);
75 modify(boost::json::value& value, std::string_view key)
77 if (not value.is_object() or not value.as_object().contains(key))
80 if (not value.as_object().at(key).is_string())
83 value.as_object()[key] =
84 util::toLower(boost::json::value_to<std::string>(value.as_object().at(key)));
103 modify(boost::json::value& value, std::string_view key)
105 if (not value.is_object() or not value.as_object().contains(key))
108 if (not value.as_object().at(key).is_string())
111 auto const strInt = boost::json::value_to<std::string>(value.as_object().at(key));
112 if (strInt.find(
'.') != std::string::npos)
113 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
116 value.as_object()[key] = std::stoi(strInt);
117 }
catch (std::exception& e) {
118 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
128 std::function<
MaybeError(boost::json::value&, std::string_view)> modifier_;
137 template <
typename Fn>
138 requires std::invocable<Fn, boost::json::value&, std::string_view>
152 modify(boost::json::value& value, std::string_view key)
const
154 if (not value.is_object() or not value.as_object().contains(key))
157 return modifier_(value.as_object().at(key), key);
MaybeError modify(boost::json::value &value, std::string_view key) const
Clamp the value to stored min and max values.
Definition Modifiers.hpp:46
Clamp(Type min, Type max)
Construct the modifier storing min and max values.
Definition Modifiers.hpp:34
CustomModifier(Fn &&fn)
Constructs a custom modifier from any supported callable.
Definition Modifiers.hpp:139
MaybeError modify(boost::json::value &value, std::string_view key) const
Modify the JSON value according to the custom modifier function stored.
Definition Modifiers.hpp:152
std::expected< void, Status > MaybeError
Return type used for Validators that can return error but don't have specific value to return.
Definition Types.hpp:36
std::unexpected< Status > Error
The type that represents just the error part of MaybeError.
Definition Types.hpp:56
std::string toLower(std::string str)
Convert a string to lowercase.
Definition JsonUtils.hpp:29
A status returned from any RPC handler.
Definition Errors.hpp:65
Convert input string to lower case.
Definition Modifiers.hpp:66
static MaybeError modify(boost::json::value &value, std::string_view key)
Update the input string to lower case.
Definition Modifiers.hpp:75
Convert input string to integer.
Definition Modifiers.hpp:94
static MaybeError modify(boost::json::value &value, std::string_view key)
Update the input string to integer if it can be converted to integer by stoi.
Definition Modifiers.hpp:103