3#include "rpc/common/APIVersion.hpp"
4#include "util/config/Error.hpp"
5#include "util/config/Types.hpp"
6#include "util/log/Logger.hpp"
22namespace util::config {
29static constexpr std::array<std::string_view, 6> kLogLevels = {
41static constexpr std::array<std::string_view, 5> kLogTags = {
52static constexpr std::array<std::string_view, 3> kLoadCacheMode = {
61static constexpr std::array<std::string_view, 1> kDatabaseType = {
"cassandra"};
66static constexpr std::array<std::string_view, 2> kProcessingPolicy = {
"parallel",
"sequent"};
71static constexpr std::array<std::string_view, 2> kProvider = {
"cassandra",
"aws_keyspace"};
90 if (
auto const maybeError =
checkTypeImpl(val); maybeError.has_value())
106 template <std::
size_t ArrSize>
107 [[nodiscard]]
constexpr std::string
109 std::string_view key,
111 std::array<std::string_view, ArrSize> arr
115 auto const valueStr = std::visit([](
auto const& v) {
return fmt::format(
"{}", v); }, value);
119 R
"(You provided value "{}". Key "{}"'s value must be one of the following: {})",
132 [[nodiscard]]
virtual std::optional<Error>
141 [[nodiscard]]
virtual std::optional<Error>
150 print(std::ostream& stream)
const = 0;
181 [[nodiscard]] std::optional<Error>
182 checkTypeImpl(Value
const& port)
const override;
190 [[nodiscard]] std::optional<Error>
191 checkValueImpl(Value
const& port)
const override;
199 print(std::ostream& stream)
const override
201 stream << fmt::format(
202 "The minimum value is `{}`. The maximum value is `{}`.", kPortMin, kPortMax
206 static constexpr uint32_t kPortMin = 1;
207 static constexpr uint32_t kPortMax = 65535;
224 [[nodiscard]] std::optional<Error>
225 checkTypeImpl(Value
const& ip)
const override;
233 [[nodiscard]] std::optional<Error>
234 checkValueImpl(Value
const& ip)
const override;
242 print(std::ostream& stream)
const override
244 stream <<
"The value must be a valid IP address.";
254template <std::
size_t ArrSize>
264 constexpr OneOf(std::string_view key, std::array<std::string_view, ArrSize> arr)
265 : key_{key}, arr_{arr}
269 constexpr ~OneOf() noexcept override = default;
278 [[nodiscard]] std::optional<
Error>
279 checkTypeImpl(Value const& val)
const override
281 if (!std::holds_alternative<std::string>(val))
282 return Error{fmt::format(R
"(Key "{}"'s value must be a string)", key_)};
292 [[nodiscard]] std::optional<Error>
293 checkValueImpl(Value
const& val)
const override
295 namespace rg = std::ranges;
296 auto const check = [&val](std::string_view name) {
297 return std::get<std::string>(val) == name;
299 if (rg::any_of(arr_, check))
311 print(std::ostream& stream)
const override
313 std::string valuesStream;
314 std::ranges::for_each(arr_, [&valuesStream](std::string_view elem) {
315 valuesStream += fmt::format(
" `{}`,", elem);
318 valuesStream.back() =
'.';
319 stream << fmt::format(
"The value must be one of the following:{}", valuesStream);
322 std::string_view key_;
323 std::array<std::string_view, ArrSize> arr_;
329template <
typename NumType>
351 [[nodiscard]] std::optional<
Error>
352 checkTypeImpl(Value const& num)
const override
354 if (!std::holds_alternative<int64_t>(num))
355 return Error{
"Number must be of type integer"};
365 [[nodiscard]] std::optional<Error>
366 checkValueImpl(Value
const& num)
const override
368 auto const numValue = std::get<int64_t>(num);
369 if (numValue >=
static_cast<int64_t
>(min_) && numValue <=
static_cast<int64_t
>(max_))
371 return Error{fmt::format(
"Number must be between {} and {}", min_, max_)};
380 print(std::ostream& stream)
const override
382 stream << fmt::format(
"The minimum value is `{}`. The maximum value is `{}`.", min_, max_);
403 [[nodiscard]] std::optional<Error>
404 checkTypeImpl(Value
const& num)
const override;
412 [[nodiscard]] std::optional<Error>
413 checkValueImpl(Value
const& num)
const override;
421 print(std::ostream& stream)
const override
423 stream <<
"The value must be a positive double number.";
438 [[nodiscard]] std::optional<Error>
439 checkTypeImpl(Value
const& value)
const override;
447 [[nodiscard]] std::optional<Error>
448 checkValueImpl(Value
const& value)
const override;
456 print(std::ostream& stream)
const override
458 stream <<
"Checks whether provided RPC name is valid";
463static constinit ValidIPConstraint gValidateIp{};
465static constinit OneOf gValidateChannelName{
"channel", Logger::kChannels};
466static constinit OneOf gValidateLogLevelName{
"log.level", kLogLevels};
467static constinit OneOf gValidateCassandraName{
"database.type", kDatabaseType};
468static constinit OneOf gValidateLoadMode{
"cache.load", kLoadCacheMode};
469static constinit OneOf gValidateLogTag{
"log.tag_style", kLogTags};
470static constinit OneOf gValidateProcessingPolicy{
"server.processing_policy", kProcessingPolicy};
471static constinit OneOf gValidateProvider{
"database.cassandra.provider", kProvider};
478 std::numeric_limits<uint16_t>::max()
484 std::numeric_limits<uint16_t>::max()
489 std::numeric_limits<uint16_t>::max()
494 std::numeric_limits<uint32_t>::max()
498 std::numeric_limits<uint32_t>::max()
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:330
constexpr NumberValueConstraint(NumType min, NumType max)
Constructs a constraint where the number must be between min_ and max_.
Definition ConfigConstraints.hpp:338
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 double number is positive.
Definition ConfigConstraints.hpp:392
A constraint to ensure the value is a valid RPC command name.
Definition ConfigConstraints.hpp:430
A constraint to ensure the IP address is valid.
Definition ConfigConstraints.hpp:213
Provides view into ConfigValues that represents values in Clio Config.
Definition ValueView.hpp:27
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
Displays the different errors when parsing user config.
Definition Error.hpp:12