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/core.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) : warningCode(code), extraMessage(std::move(message))
47 {
48 }
49
50 bool
51 operator==(Warning const& other) const = default;
52
53 WarningCode warningCode;
54 std::string extraMessage;
55};
56using Warnings = std::vector<Warning>;
57
61template <typename... T>
63
67template <>
68class Deprecated<> final {
69public:
77 [[nodiscard]] static std::optional<Warning>
78 check(boost::json::value const& value, std::string_view key)
79 {
80 if (value.is_object() and value.as_object().contains(key))
81 return Warning{WarningCode::WarnRpcDeprecated, fmt::format("Field '{}' is deprecated.", key)};
82 return std::nullopt;
83 }
84};
85
90template <typename T>
91class Deprecated<T> final {
92 T value_;
93
94public:
100 Deprecated(T val) : value_(std::move(val))
101 {
102 }
103
111 [[nodiscard]] std::optional<Warning>
112 check(boost::json::value const& value, std::string_view key) const
113 {
114 if (value.is_object() and value.as_object().contains(key) and
115 validation::checkType<T>(value.as_object().at(key))) {
116 using boost::json::value_to;
117 auto const res = value_to<T>(value.as_object().at(key));
118 if (value_ == res) {
119 return Warning{
120 WarningCode::WarnRpcDeprecated, fmt::format("Value '{}' for field '{}' is deprecated", value_, key)
121 };
122 }
123 }
124 return std::nullopt;
125 }
126};
127
131template <typename... T>
132Deprecated(T&&...) -> Deprecated<T...>;
133
134} // 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:112
Deprecated(T val)
Construct a new Deprecated object.
Definition Checkers.hpp:100
static std::optional< Warning > check(boost::json::value const &value, std::string_view key)
Check if a field is deprecated.
Definition Checkers.hpp:78
Check for a deprecated fields.
Definition Checkers.hpp:62
WarningCode
Warning codes that can be returned by clio.
Definition Errors.hpp:187
Warning that checks can return.
Definition Checkers.hpp:39
Warning(WarningCode code, std::string message)
Construct a new Warning object.
Definition Checkers.hpp:46