Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ConfigConstraints.hpp
1#pragma once
2
3#include "rpc/common/APIVersion.hpp"
4#include "util/config/Error.hpp"
5#include "util/config/Types.hpp"
6#include "util/log/Logger.hpp"
7
8#include <fmt/format.h>
9#include <fmt/ranges.h>
10
11#include <algorithm>
12#include <array>
13#include <cstddef>
14#include <cstdint>
15#include <limits>
16#include <optional>
17#include <ostream>
18#include <string>
19#include <string_view>
20#include <variant>
21
22namespace util::config {
23class ValueView;
24class ConfigValue;
25
29static constexpr std::array<std::string_view, 6> kLogLevels = {
30 "trace",
31 "debug",
32 "info",
33 "warning",
34 "error",
35 "fatal",
36};
37
41static constexpr std::array<std::string_view, 5> kLogTags = {
42 "int",
43 "uint",
44 "null",
45 "none",
46 "uuid",
47};
48
52static constexpr std::array<std::string_view, 3> kLoadCacheMode = {
53 "sync",
54 "async",
55 "none",
56};
57
61static constexpr std::array<std::string_view, 1> kDatabaseType = {"cassandra"};
62
66static constexpr std::array<std::string_view, 2> kProcessingPolicy = {"parallel", "sequent"};
67
71static constexpr std::array<std::string_view, 2> kProvider = {"cassandra", "aws_keyspace"};
72
77public:
78 constexpr virtual ~Constraint() noexcept = default;
79
86 [[nodiscard]]
87 std::optional<Error>
88 checkConstraint(Value const& val) const
89 {
90 if (auto const maybeError = checkTypeImpl(val); maybeError.has_value())
91 return maybeError;
92 return checkValueImpl(val);
93 }
94
95protected:
106 template <std::size_t ArrSize>
107 [[nodiscard]] constexpr std::string
109 std::string_view key,
110 Value const& value,
111 std::array<std::string_view, ArrSize> arr
112 ) const
113 {
114 // Extract the value from the variant
115 auto const valueStr = std::visit([](auto const& v) { return fmt::format("{}", v); }, value);
116
117 // Create the error message
118 return fmt::format(
119 R"(You provided value "{}". Key "{}"'s value must be one of the following: {})",
120 valueStr,
121 key,
122 fmt::join(arr, ", ")
123 );
124 }
125
132 [[nodiscard]] virtual std::optional<Error>
133 checkTypeImpl(Value const& val) const = 0;
134
141 [[nodiscard]] virtual std::optional<Error>
142 checkValueImpl(Value const& val) const = 0;
143
149 virtual void
150 print(std::ostream& stream) const = 0;
151
159 friend std::ostream&
160 operator<<(std::ostream& stream, Constraint const& cons)
161 {
162 cons.print(stream);
163 return stream;
164 }
165};
166
170class PortConstraint final : public Constraint {
171public:
172 constexpr ~PortConstraint() noexcept override = default;
173
174private:
181 [[nodiscard]] std::optional<Error>
182 checkTypeImpl(Value const& port) const override;
183
190 [[nodiscard]] std::optional<Error>
191 checkValueImpl(Value const& port) const override;
192
198 void
199 print(std::ostream& stream) const override
200 {
201 stream << fmt::format(
202 "The minimum value is `{}`. The maximum value is `{}`.", kPortMin, kPortMax
203 );
204 }
205
206 static constexpr uint32_t kPortMin = 1;
207 static constexpr uint32_t kPortMax = 65535;
208};
209
213class ValidIPConstraint final : public Constraint {
214public:
215 constexpr ~ValidIPConstraint() noexcept override = default;
216
217private:
224 [[nodiscard]] std::optional<Error>
225 checkTypeImpl(Value const& ip) const override;
226
233 [[nodiscard]] std::optional<Error>
234 checkValueImpl(Value const& ip) const override;
235
241 void
242 print(std::ostream& stream) const override
243 {
244 stream << "The value must be a valid IP address.";
245 }
246};
247
254template <std::size_t ArrSize>
255class OneOf final : public Constraint {
256public:
264 constexpr OneOf(std::string_view key, std::array<std::string_view, ArrSize> arr)
265 : key_{key}, arr_{arr}
266 {
267 }
268
269 constexpr ~OneOf() noexcept override = default;
270
271private:
278 [[nodiscard]] std::optional<Error>
279 checkTypeImpl(Value const& val) const override
280 {
281 if (!std::holds_alternative<std::string>(val))
282 return Error{fmt::format(R"(Key "{}"'s value must be a string)", key_)};
283 return std::nullopt;
284 }
285
292 [[nodiscard]] std::optional<Error>
293 checkValueImpl(Value const& val) const override
294 {
295 namespace rg = std::ranges;
296 auto const check = [&val](std::string_view name) {
297 return std::get<std::string>(val) == name;
298 };
299 if (rg::any_of(arr_, check))
300 return std::nullopt;
301
302 return Error{makeErrorMsg(key_, val, arr_)};
303 }
304
310 void
311 print(std::ostream& stream) const override
312 {
313 std::string valuesStream;
314 std::ranges::for_each(arr_, [&valuesStream](std::string_view elem) {
315 valuesStream += fmt::format(" `{}`,", elem);
316 });
317 // replace the last "," with "."
318 valuesStream.back() = '.';
319 stream << fmt::format("The value must be one of the following:{}", valuesStream);
320 }
321
322 std::string_view key_;
323 std::array<std::string_view, ArrSize> arr_;
324};
325
331template <typename NumType>
332class NumberValueConstraint final : public Constraint {
333public:
340 constexpr NumberValueConstraint(NumType min, NumType max) : min_{min}, max_{max}
341 {
342 }
343
344 constexpr ~NumberValueConstraint() noexcept override = default;
345
346private:
353 [[nodiscard]] std::optional<Error>
354 checkTypeImpl(Value const& num) const override
355 {
356 if (!std::holds_alternative<int64_t>(num))
357 return Error{"Number must be of type integer"};
358 return std::nullopt;
359 }
360
367 [[nodiscard]] std::optional<Error>
368 checkValueImpl(Value const& num) const override
369 {
370 auto const numValue = std::get<int64_t>(num);
371 if (numValue >= static_cast<int64_t>(min_) && numValue <= static_cast<int64_t>(max_))
372 return std::nullopt;
373 return Error{fmt::format("Number must be between {} and {}", min_, max_)};
374 }
375
381 void
382 print(std::ostream& stream) const override
383 {
384 stream << fmt::format("The minimum value is `{}`. The maximum value is `{}`.", min_, max_);
385 }
386
387 NumType min_;
388 NumType max_;
389};
390
394class PositiveDouble final : public Constraint {
395public:
396 constexpr ~PositiveDouble() noexcept override = default;
397
398private:
405 [[nodiscard]] std::optional<Error>
406 checkTypeImpl(Value const& num) const override;
407
414 [[nodiscard]] std::optional<Error>
415 checkValueImpl(Value const& num) const override;
416
422 void
423 print(std::ostream& stream) const override
424 {
425 stream << "The value must be a positive double number.";
426 }
427};
428
432class RpcNameConstraint final : public Constraint {
433private:
440 [[nodiscard]] std::optional<Error>
441 checkTypeImpl(Value const& value) const override;
442
449 [[nodiscard]] std::optional<Error>
450 checkValueImpl(Value const& value) const override;
451
457 void
458 print(std::ostream& stream) const override
459 {
460 stream << "Checks whether provided RPC name is valid";
461 }
462};
463
464static constinit PortConstraint gValidatePort{};
465static constinit ValidIPConstraint gValidateIp{};
466
470static constinit OneOf gValidateChannelName{"channel", Logger::kChannels};
474static constinit OneOf gValidateLogLevelName{"log.level", kLogLevels};
478static constinit OneOf gValidateCassandraName{"database.type", kDatabaseType};
482static constinit OneOf gValidateLoadMode{"cache.load", kLoadCacheMode};
486static constinit OneOf gValidateLogTag{"log.tag_style", kLogTags};
490static constinit OneOf gValidateProcessingPolicy{"server.processing_policy", kProcessingPolicy};
494static constinit OneOf gValidateProvider{"database.cassandra.provider", kProvider};
495
497
506 0,
507 std::numeric_limits<uint16_t>::max()
508};
509
514 0,
515 std::numeric_limits<uint16_t>::max()
516};
517
522 1,
523 std::numeric_limits<uint16_t>::max()
524};
525
530 1,
531 std::numeric_limits<uint32_t>::max()
532};
533
537 0,
538 std::numeric_limits<uint32_t>::max()
539};
540
547
552} // namespace util::config
static constexpr std::array< std::string_view, 8 > kChannels
The predefined log channels; loggers for other channels are created dynamically.
Definition Logger.hpp:168
Represents the config values for Json/Yaml config.
Definition ConfigValue.hpp:28
An interface to enforce constraints on certain values within ClioConfigDefinition.
Definition ConfigConstraints.hpp:76
friend std::ostream & operator<<(std::ostream &stream, Constraint const &cons)
Custom output stream for constraint.
Definition ConfigConstraints.hpp:160
virtual void print(std::ostream &stream) const =0
Prints to the output stream for this specific constraint.
virtual std::optional< Error > checkValueImpl(Value const &val) const =0
Check if the value is within the constraint.
constexpr std::string makeErrorMsg(std::string_view key, Value const &value, std::array< std::string_view, ArrSize > arr) const
Creates an error message for all constraints that must satisfy certain hard-coded values.
Definition ConfigConstraints.hpp:108
std::optional< Error > checkConstraint(Value const &val) const
Check if the value meets the specific constraint.
Definition ConfigConstraints.hpp:88
virtual std::optional< Error > checkTypeImpl(Value const &val) const =0
Check if the value is of a correct type for the constraint.
A constraint class to ensure an integer value is between two numbers (inclusive).
Definition ConfigConstraints.hpp:332
constexpr NumberValueConstraint(NumType min, NumType max)
Constructs a constraint where the number must be between min_ and max_.
Definition ConfigConstraints.hpp:340
A constraint class to ensure the provided value is one of the specified values in an array.
Definition ConfigConstraints.hpp:255
constexpr OneOf(std::string_view key, std::array< std::string_view, ArrSize > arr)
Constructs a constraint where the value must be one of the values in the provided array.
Definition ConfigConstraints.hpp:264
A constraint to ensure the port number is within a valid range.
Definition ConfigConstraints.hpp:170
A constraint to ensure a number (int or double) is non-negative.
Definition ConfigConstraints.hpp:394
A constraint to ensure the value is a valid RPC command name.
Definition ConfigConstraints.hpp:432
A constraint to ensure the value is a valid IP address or hostname.
Definition ConfigConstraints.hpp:213
Provides view into ConfigValues that represents values in Clio Config.
Definition ValueView.hpp:27
std::unexpected< CassandraError > Error
A database error.
Definition Types.hpp:67
static constexpr uint32_t kApiVersionMin
Minimum API version supported by this build.
Definition APIVersion.hpp:19
static constexpr uint32_t kApiVersionMax
Maximum API version supported by this build.
Definition APIVersion.hpp:24
Clio configuration: definitions of the supported keys, their types, constraints and defaults,...
Definition HandlerRegistry.hpp:24
static constexpr std::array< std::string_view, 3 > kLoadCacheMode
specific values that are accepted for cache loading in config.
Definition ConfigConstraints.hpp:52
static constinit NumberValueConstraint< uint32_t > gValidateNonNegativeUint32
Validates a non-negative uint32 value (0 is allowed).
Definition ConfigConstraints.hpp:536
static constinit OneOf gValidateLoadMode
Validates the cache load mode.
Definition ConfigConstraints.hpp:482
static constinit RpcNameConstraint gRpcNameConstraint
Validates that a value is a valid RPC command name.
Definition ConfigConstraints.hpp:551
static constinit OneOf gValidateProcessingPolicy
Validates the server request processing policy.
Definition ConfigConstraints.hpp:490
static constinit OneOf gValidateProvider
Validates the Cassandra-compatible database provider.
Definition ConfigConstraints.hpp:494
static constinit NumberValueConstraint< uint16_t > gValidateNumMarkers
Validates the number of markers used for the initial ledger load; must be in [1, 256].
Definition ConfigConstraints.hpp:501
static constinit OneOf gValidateChannelName
Validates a log channel name.
Definition ConfigConstraints.hpp:470
std::variant< int64_t, std::string, bool, double > Value
Represents the supported Config Values.
Definition Types.hpp:38
static constinit PortConstraint gValidatePort
Validates a network port number.
Definition ConfigConstraints.hpp:464
static constinit NumberValueConstraint< uint32_t > gValidateUint32
Validates a positive (non-zero) uint32 value.
Definition ConfigConstraints.hpp:529
static constexpr std::array< std::string_view, 1 > kDatabaseType
specific values that are accepted for database type in config.
Definition ConfigConstraints.hpp:61
static constexpr std::array< std::string_view, 2 > kProcessingPolicy
specific values that are accepted for server's processing_policy in config.
Definition ConfigConstraints.hpp:66
static constinit OneOf gValidateLogLevelName
Validates a log level name.
Definition ConfigConstraints.hpp:474
static constexpr std::array< std::string_view, 2 > kProvider
specific values that are accepted for database provider in config.
Definition ConfigConstraints.hpp:71
static constinit NumberValueConstraint< uint32_t > gValidateApiVersion
Validates an API version; must be within the supported range of API versions.
Definition ConfigConstraints.hpp:543
static constexpr std::array< std::string_view, 6 > kLogLevels
specific values that are accepted for logger levels in config.
Definition ConfigConstraints.hpp:29
static constinit NumberValueConstraint< uint16_t > gValidateReplicationFactor
Validates the database replication factor; 0 is allowed.
Definition ConfigConstraints.hpp:513
static constinit ValidIPConstraint gValidateIp
Validates an IP address or hostname.
Definition ConfigConstraints.hpp:465
static constinit NumberValueConstraint< uint16_t > gValidateUint16
Validates a positive (non-zero) uint16 value.
Definition ConfigConstraints.hpp:521
static constinit PositiveDouble gValidatePositiveDouble
Validates a number is non-negative.
Definition ConfigConstraints.hpp:496
static constinit OneOf gValidateCassandraName
Validates the database type.
Definition ConfigConstraints.hpp:478
static constinit NumberValueConstraint< uint16_t > gValidateNumCursors
Validates a number of cache loading cursors; any uint16 value, including 0.
Definition ConfigConstraints.hpp:505
static constinit OneOf gValidateLogTag
Validates the log tag style.
Definition ConfigConstraints.hpp:486
static constexpr std::array< std::string_view, 5 > kLogTags
specific values that are accepted for logger tag style in config.
Definition ConfigConstraints.hpp:41
Displays the different errors when parsing user config.
Definition Error.hpp:14