22#include "rpc/common/APIVersion.hpp"
23#include "util/log/Logger.hpp"
24#include "util/newconfig/Error.hpp"
25#include "util/newconfig/Types.hpp"
28#include <fmt/format.h>
41namespace util::config {
48static constexpr std::array<char const*, 7> kLOG_LEVELS = {
61static constexpr std::array<char const*, 5> kLOG_TAGS = {
72static constexpr std::array<char const*, 3> kLOAD_CACHE_MODE = {
81static constexpr std::array<char const*, 1> kDATABASE_TYPE = {
"cassandra"};
86static constexpr std::array<char const*, 2> kPROCESSING_POLICY = {
"parallel",
"sequent"};
105 if (
auto const maybeError =
checkTypeImpl(val); maybeError.has_value())
120 template <std::
size_t ArrSize>
121 constexpr std::string
122 makeErrorMsg(std::string_view key, Value
const& value, std::array<char const*, ArrSize> arr)
const
125 auto const valueStr = std::visit([](
auto const& v) {
return fmt::format(
"{}", v); }, value);
129 R
"(You provided value "{}". Key "{}"'s value must be one of the following: {})",
142 virtual std::optional<Error>
151 virtual std::optional<Error>
160 print(std::ostream& stream)
const = 0;
191 [[nodiscard]] std::optional<Error>
192 checkTypeImpl(Value
const& port)
const override;
200 [[nodiscard]] std::optional<Error>
201 checkValueImpl(Value
const& port)
const override;
209 print(std::ostream& stream)
const override
211 stream << fmt::format(
"The minimum value is `{}`. The maximum value is `{}`.", kPORT_MIN, kPORT_MAX);
214 static constexpr uint32_t kPORT_MIN = 1;
215 static constexpr uint32_t kPORT_MAX = 65535;
232 [[nodiscard]] std::optional<Error>
233 checkTypeImpl(Value
const& ip)
const override;
241 [[nodiscard]] std::optional<Error>
242 checkValueImpl(Value
const& ip)
const override;
250 print(std::ostream& stream)
const override
252 stream <<
"The value must be a valid IP address.";
261template <std::
size_t ArrSize>
270 constexpr OneOf(std::string_view key, std::array<char const*, ArrSize> arr) : key_{key}, arr_{arr}
274 constexpr ~OneOf() noexcept override = default;
283 [[nodiscard]] std::optional<
Error>
284 checkTypeImpl(Value const& val)
const override
286 if (!std::holds_alternative<std::string>(val))
287 return Error{fmt::format(R
"(Key "{}"'s value must be a string)", key_)};
297 [[nodiscard]] std::optional<Error>
298 checkValueImpl(Value
const& val)
const override
300 namespace rg = std::ranges;
301 auto const check = [&val](std::string_view name) {
return std::get<std::string>(val) == name; };
302 if (rg::any_of(arr_, check))
314 print(std::ostream& stream)
const override
316 std::string valuesStream;
317 std::ranges::for_each(arr_, [&valuesStream](std::string
const& elem) {
318 valuesStream += fmt::format(
" `{}`,", elem);
321 valuesStream.back() =
'.';
322 stream << fmt::format(
"The value must be one of the following:{}", valuesStream);
325 std::string_view key_;
326 std::array<char const*, ArrSize> arr_;
332template <
typename NumType>
354 [[nodiscard]] std::optional<
Error>
355 checkTypeImpl(Value const& num)
const override
357 if (!std::holds_alternative<int64_t>(num))
358 return Error{
"Number must be of type integer"};
368 [[nodiscard]] std::optional<Error>
369 checkValueImpl(Value
const& num)
const override
371 auto const numValue = std::get<int64_t>(num);
372 if (numValue >=
static_cast<int64_t
>(min_) && numValue <=
static_cast<int64_t
>(max_))
374 return Error{fmt::format(
"Number must be between {} and {}", min_, max_)};
383 print(std::ostream& stream)
const override
385 stream << fmt::format(
"The minimum value is `{}`. The maximum value is `{}`.", min_, max_);
406 [[nodiscard]] std::optional<Error>
407 checkTypeImpl(Value
const& num)
const override;
415 [[nodiscard]] std::optional<Error>
416 checkValueImpl(Value
const& num)
const override;
424 print(std::ostream& stream)
const override
426 stream <<
"The value must be a positive double number.";
431static constinit ValidIPConstraint gValidateIp{};
433static constinit OneOf gValidateChannelName{
"channel", Logger::kCHANNELS};
434static constinit OneOf gValidateLogLevelName{
"log_level", kLOG_LEVELS};
435static constinit OneOf gValidateCassandraName{
"database.type", kDATABASE_TYPE};
436static constinit OneOf gValidateLoadMode{
"cache.load", kLOAD_CACHE_MODE};
437static constinit OneOf gValidateLogTag{
"log_tag_style", kLOG_TAGS};
438static constinit OneOf gValidateProcessingPolicy{
"server.processing_policy", kPROCESSING_POLICY};
440static constinit PositiveDouble gValidatePositiveDouble{};
442static constinit NumberValueConstraint<uint16_t> gValidateNumMarkers{1, 256};
443static constinit NumberValueConstraint<uint16_t> gValidateNumCursors{0, std::numeric_limits<uint16_t>::max()};
446static constinit NumberValueConstraint<uint16_t> gValidateReplicationFactor{0, std::numeric_limits<uint16_t>::max()};
448static constinit NumberValueConstraint<uint16_t> gValidateUint16{1, std::numeric_limits<uint16_t>::max()};
450static constinit NumberValueConstraint<uint32_t> gValidateUint32{1, std::numeric_limits<uint32_t>::max()};
An interface to enforce constraints on certain values within ClioConfigDefinition.
Definition ConfigConstraints.hpp:91
friend std::ostream & operator<<(std::ostream &stream, Constraint const &cons)
Custom output stream for constraint.
Definition ConfigConstraints.hpp:170
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.
std::optional< Error > checkConstraint(Value const &val) const
Check if the value meets the specific constraint.
Definition ConfigConstraints.hpp:103
constexpr std::string makeErrorMsg(std::string_view key, Value const &value, std::array< char const *, ArrSize > arr) const
Creates an error message for all constraints that must satisfy certain hard-coded values.
Definition ConfigConstraints.hpp:122
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:333
constexpr NumberValueConstraint(NumType min, NumType max)
Constructs a constraint where the number must be between min_ and max_.
Definition ConfigConstraints.hpp:341
A constraint class to ensure the provided value is one of the specified values in an array.
Definition ConfigConstraints.hpp:262
constexpr OneOf(std::string_view key, std::array< char const *, ArrSize > arr)
Constructs a constraint where the value must be one of the values in the provided array.
Definition ConfigConstraints.hpp:270
A constraint to ensure the port number is within a valid range.
Definition ConfigConstraints.hpp:180
A constraint to ensure a double number is positive.
Definition ConfigConstraints.hpp:395
A constraint to ensure the IP address is valid.
Definition ConfigConstraints.hpp:221
static constexpr uint32_t kAPI_VERSION_MIN
Minimum API version supported by this build.
Definition APIVersion.hpp:38
static constexpr uint32_t kAPI_VERSION_MAX
Maximum API version supported by this build.
Definition APIVersion.hpp:43
Displays the different errors when parsing user config.
Definition Error.hpp:31