Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Checkers.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "rpc/Errors.hpp"
23#include "rpc/common/ValidationHelpers.hpp"
24
25#include <boost/json/value.hpp>
26#include <boost/json/value_to.hpp>
27#include <fmt/format.h>
28
29#include <optional>
30#include <string>
31#include <string_view>
32#include <utility>
33#include <vector>
34
35namespace rpc::check {
39struct Warning {
46 Warning(WarningCode code, std::string message)
47 : warningCode(code), extraMessage(std::move(message))
48 {
49 }
50
51 bool
52 operator==(Warning const& other) const = default;
53
54 WarningCode warningCode;
55 std::string extraMessage;
56};
57using Warnings = std::vector<Warning>;
58
62template <typename... T>
64
68template <>
69class Deprecated<> final {
70public:
78 [[nodiscard]] static std::optional<Warning>
79 check(boost::json::value const& value, std::string_view key)
80 {
81 if (value.is_object() and value.as_object().contains(key))
82 return Warning{
83 WarningCode::WarnRpcDeprecated, fmt::format("Field '{}' is deprecated.", key)
84 };
85 return std::nullopt;
86 }
87};
88
93template <typename T>
94class Deprecated<T> final {
95 T value_;
96
97public:
103 Deprecated(T val) : value_(std::move(val))
104 {
105 }
106
114 [[nodiscard]] std::optional<Warning>
115 check(boost::json::value const& value, std::string_view key) const
116 {
117 if (value.is_object() and value.as_object().contains(key) and
118 validation::checkType<T>(value.as_object().at(key))) {
119 using boost::json::value_to;
120 auto const res = value_to<T>(value.as_object().at(key));
121 if (value_ == res) {
122 return Warning{
123 WarningCode::WarnRpcDeprecated,
124 fmt::format("Value '{}' for field '{}' is deprecated", value_, key)
125 };
126 }
127 }
128 return std::nullopt;
129 }
130};
131
135template <typename... T>
136Deprecated(T&&...) -> Deprecated<T...>;
137
138} // 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:115
Deprecated(T val)
Construct a new Deprecated object.
Definition Checkers.hpp:103
static std::optional< Warning > check(boost::json::value const &value, std::string_view key)
Check if a field is deprecated.
Definition Checkers.hpp:79
Check for a deprecated fields.
Definition Checkers.hpp:63
WarningCode
Warning codes that can be returned by clio.
Definition Errors.hpp:200
bool operator==(MaybeError const &lhs, MaybeError const &rhs)
Check if two MaybeError objects are equal.
Definition Types.hpp:65
Warning that checks can return.
Definition Checkers.hpp:39
Warning(WarningCode code, std::string message)
Construct a new Warning object.
Definition Checkers.hpp:46