|
| 1 | + |
| 2 | +#pragma once |
| 3 | +#include <userver/formats/universal/universal.hpp> |
| 4 | +#include <userver/utils/regex.hpp> |
| 5 | +#include <fmt/format.h> |
| 6 | +#include <map> |
| 7 | + |
| 8 | +USERVER_NAMESPACE_BEGIN |
| 9 | +namespace formats::universal { |
| 10 | + |
| 11 | +template <auto Value> |
| 12 | +struct Min { |
| 13 | + static constexpr auto kValue = Value; |
| 14 | +}; |
| 15 | + |
| 16 | +template <auto Value> |
| 17 | +struct Max { |
| 18 | + static constexpr auto kValue = Value; |
| 19 | +}; |
| 20 | + |
| 21 | +template <auto Value> |
| 22 | +struct Default { |
| 23 | + static constexpr auto kValue = Value; |
| 24 | +}; |
| 25 | + |
| 26 | +template <utils::ConstexprString Regex> |
| 27 | +struct Pattern { |
| 28 | + static constexpr auto kValue = Regex; |
| 29 | +}; |
| 30 | + |
| 31 | +struct Additional {}; |
| 32 | + |
| 33 | +template <typename Field, auto Value> |
| 34 | +constexpr inline auto Check(const Field& field, Max<Value>) noexcept { |
| 35 | + return Value >= field; |
| 36 | +}; |
| 37 | + |
| 38 | +template <typename Field, auto Value> |
| 39 | +constexpr inline auto Check(const Field& field, Min<Value>) noexcept { |
| 40 | + return field >= Value; |
| 41 | +}; |
| 42 | + |
| 43 | +template <typename Field, auto Value> |
| 44 | +constexpr inline auto Check(const Field&, Default<Value>) noexcept { |
| 45 | + return true; |
| 46 | +}; |
| 47 | + |
| 48 | +template <utils::ConstexprString Regex> |
| 49 | +static const userver::utils::regex kRegex(Regex); |
| 50 | + |
| 51 | +template <utils::ConstexprString Regex> |
| 52 | +constexpr inline auto Check(const std::string& field, Pattern<Regex>) noexcept { |
| 53 | + return utils::regex_match(field, kRegex<Regex>); |
| 54 | +}; |
| 55 | + |
| 56 | +template <typename Field, auto Value> |
| 57 | +constexpr inline auto Check(const std::optional<Field>&, Default<Value>) noexcept { |
| 58 | + return true; |
| 59 | +}; |
| 60 | + |
| 61 | +template <typename Field> |
| 62 | +constexpr inline auto Check(const Field&, Additional) noexcept { |
| 63 | + return true; |
| 64 | +}; |
| 65 | + |
| 66 | +template <typename Key, auto Value> |
| 67 | +constexpr inline auto Check(const std::vector<Key>& field, Max<Value>) noexcept { |
| 68 | + return Value >= field.size(); |
| 69 | +}; |
| 70 | + |
| 71 | +template <typename Key, typename Tp, auto Value> |
| 72 | +constexpr inline auto Check(const std::map<Key, Tp>& field, Max<Value>) noexcept { |
| 73 | + return Value >= field.size(); |
| 74 | +}; |
| 75 | + |
| 76 | +template <typename Key, typename Tp, auto Value> |
| 77 | +constexpr inline auto Check(const std::unordered_map<Key, Tp>& field, Max<Value>) noexcept { |
| 78 | + return Value >= field.size(); |
| 79 | +}; |
| 80 | + |
| 81 | +template <typename Field, typename CheckT> |
| 82 | +constexpr inline bool Check(const std::optional<Field>& field, CheckT check) noexcept { |
| 83 | + if(field.has_value()) { |
| 84 | + return Check(*field, check); |
| 85 | + }; |
| 86 | + return true; |
| 87 | +}; |
| 88 | + |
| 89 | +template <typename T, auto I, typename Key, typename Value, auto Maximum> |
| 90 | +constexpr inline auto ErrorMessage(const std::unordered_map<Key, Value>& field, Max<Maximum>) { |
| 91 | + return fmt::format("Error with field {0} Map size: {1} Maximum Size: {2}", boost::pfr::get_name<I, T>(), field.size(), Maximum); |
| 92 | +}; |
| 93 | + |
| 94 | +template <typename T, auto I, typename Field, template <auto> typename Check, auto Value> |
| 95 | +constexpr inline auto ErrorMessage(const Field& field, Check<Value>) { |
| 96 | + return fmt::format("Error with field {0} Field value: {1} Check Value: {2}", boost::pfr::get_name<I, T>(), field, Value); |
| 97 | +}; |
| 98 | + |
| 99 | + |
| 100 | +template <typename T, auto I, typename Value, typename Check> |
| 101 | +constexpr inline auto ErrorMessage(const std::unordered_map<std::string, Value>&, Additional) { |
| 102 | + return "Error"; |
| 103 | +}; |
| 104 | + |
| 105 | +template <typename T, auto I, typename Field, typename Check> |
| 106 | +constexpr inline auto ErrorMessage(const Field&, Check) { |
| 107 | + return "Error"; |
| 108 | +}; |
| 109 | + |
| 110 | +template <typename T, auto I, typename Field, template <auto> typename Check, auto Value> |
| 111 | +constexpr inline auto ErrorMessage(const std::optional<Field>& field, Check<Value> check) { |
| 112 | + return ErrorMessage<T, I>(*field, check); |
| 113 | +}; |
| 114 | + |
| 115 | +} // namespace formats::universal |
| 116 | + |
| 117 | +USERVER_NAMESPACE_END |
| 118 | + |
0 commit comments