Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Checkers.hpp
1#pragma once
2
3#include "rpc/Errors.hpp"
4#include "rpc/common/ValidationHelpers.hpp"
5
6#include <boost/json/value.hpp>
7#include <boost/json/value_to.hpp>
8#include <fmt/format.h>
9
10#include <optional>
11#include <string>
12#include <string_view>
13#include <utility>
14#include <vector>
15
16namespace rpc::check {
20struct Warning {
27 Warning(WarningCode code, std::string message)
28 : warningCode(code), extraMessage(std::move(message))
29 {
30 }
31
32 bool
33 operator==(Warning const& other) const = default;
34
35 WarningCode warningCode;
36 std::string extraMessage;
37};
38using Warnings = std::vector<Warning>;
39
43template <typename... T>
45
49template <>
50class Deprecated<> final {
51public:
59 [[nodiscard]] static std::optional<Warning>
60 check(boost::json::value const& value, std::string_view key)
61 {
62 if (value.is_object() and value.as_object().contains(key)) {
63 return Warning{
64 WarningCode::WarnRpcDeprecated, fmt::format("Field '{}' is deprecated.", key)
65 };
66 }
67 return std::nullopt;
68 }
69};
70
75template <typename T>
76class Deprecated<T> final {
77 T value_;
78
79public:
85 Deprecated(T val) : value_(std::move(val))
86 {
87 }
88
96 [[nodiscard]] std::optional<Warning>
97 check(boost::json::value const& value, std::string_view key) const
98 {
99 if (value.is_object() and value.as_object().contains(key) and
100 validation::checkType<T>(value.as_object().at(key))) {
101 using boost::json::value_to;
102 auto const res = value_to<T>(value.as_object().at(key));
103 if (value_ == res) {
104 return Warning{
105 WarningCode::WarnRpcDeprecated,
106 fmt::format("Value '{}' for field '{}' is deprecated", value_, key)
107 };
108 }
109 }
110 return std::nullopt;
111 }
112};
113
117template <typename... T>
118Deprecated(T&&...) -> Deprecated<T...>;
119
120} // namespace rpc::check
std::optional< Warning > check(boost::json::value const &value, std::string_view key) const
Check if a value of a field is deprecated.
Definition Checkers.hpp:97
Deprecated(T val)
Construct a new Deprecated object.
Definition Checkers.hpp:85
static std::optional< Warning > check(boost::json::value const &value, std::string_view key)
Check if a field is deprecated.
Definition Checkers.hpp:60
Check for a deprecated fields.
Definition Checkers.hpp:44
WarningCode
Warning codes that can be returned by clio.
Definition Errors.hpp:181
bool operator==(MaybeError const &lhs, MaybeError const &rhs)
Check if two MaybeError objects are equal.
Definition Types.hpp:46
Warning that checks can return.
Definition Checkers.hpp:20
Warning(WarningCode code, std::string message)
Construct a new Warning object.
Definition Checkers.hpp:27