23#include "rpc/common/Types.hpp"
24#include "util/JsonUtils.hpp"
26#include <boost/json/value.hpp>
27#include <boost/json/value_to.hpp>
28#include <xrpl/protocol/ErrorCodes.h>
36namespace rpc::modifiers {
41template <
typename Type>
53 explicit Clamp(Type min, Type max) : min_{min}, max_{max}
65 modify(boost::json::value& value, std::string_view key)
const
67 using boost::json::value_to;
69 if (not value.is_object() or not value.as_object().contains(key))
73 auto const oldValue = value_to<Type>(value.as_object().at(key));
74 value.as_object()[key] = std::clamp<Type>(oldValue, min_, max_);
94 modify(boost::json::value& value, std::string_view key)
96 if (not value.is_object() or not value.as_object().contains(key))
99 if (not value.as_object().at(key).is_string())
102 value.as_object()[key] =
util::toLower(boost::json::value_to<std::string>(value.as_object().at(key)));
121 modify(boost::json::value& value, std::string_view key)
123 if (not value.is_object() or not value.as_object().contains(key))
126 if (not value.as_object().at(key).is_string())
129 auto const strInt = boost::json::value_to<std::string>(value.as_object().at(key));
130 if (strInt.find(
'.') != std::string::npos)
131 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
134 value.as_object()[key] = std::stoi(strInt);
135 }
catch (std::exception& e) {
136 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
146 std::function<
MaybeError(boost::json::value&, std::string_view)> modifier_;
155 template <
typename Fn>
156 requires std::invocable<Fn, boost::json::value&, std::string_view>
169 modify(boost::json::value& value, std::string_view key)
const
171 if (not value.is_object() or not value.as_object().contains(key))
174 return modifier_(value.as_object().at(key), key);
Clamp value between min and max.
Definition Modifiers.hpp:42
MaybeError modify(boost::json::value &value, std::string_view key) const
Clamp the value to stored min and max values.
Definition Modifiers.hpp:65
Clamp(Type min, Type max)
Construct the modifier storing min and max values.
Definition Modifiers.hpp:53
Customised modifier allowing user define how to modify input in provided callable.
Definition Modifiers.hpp:145
CustomModifier(Fn &&fn)
Constructs a custom modifier from any supported callable.
Definition Modifiers.hpp:157
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:169
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:55
std::unexpected< Status > Error
The type that represents just the error part of MaybeError.
Definition Types.hpp:75
std::string toLower(std::string str)
Convert a string to lowercase.
Definition JsonUtils.hpp:41
A status returned from any RPC handler.
Definition Errors.hpp:82
Convert input string to lower case.
Definition Modifiers.hpp:85
static MaybeError modify(boost::json::value &value, std::string_view key)
Update the input string to lower case.
Definition Modifiers.hpp:94
Convert input string to integer.
Definition Modifiers.hpp:112
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:121