Skip to content
8 changes: 8 additions & 0 deletions universal/include/userver/formats/common/meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ template <typename Value, typename T>
using HasConvert =
decltype(Convert(std::declval<const Value&>(), parse::To<T>{}));

template <typename Value, typename T>
using HasTryParse =
decltype(TryParse(std::declval<const Value&>(), parse::To<T>{}));

template <typename Value>
using IsFormatValue = typename Value::ParseException;


template <class Value, class T>
constexpr inline bool kHasParse = meta::kIsDetected<HasParse, Value, T>;

Expand All @@ -44,6 +49,9 @@ constexpr inline bool kHasSerialize = meta::kIsDetected<HasSerialize, Value, T>;
template <class Value, class T>
constexpr inline bool kHasConvert = meta::kIsDetected<HasConvert, Value, T>;

template <class Value, class T>
constexpr inline bool kHasTryParse = meta::kIsDetected<HasTryParse, Value, T>;

} // namespace impl

/// Used in `Parse` overloads that are templated on `Value`, avoids clashing
Expand Down
61 changes: 61 additions & 0 deletions universal/include/userver/formats/parse/try_parse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once
#include <type_traits>
#include <string>
#include <cstdint>
#include <userver/formats/parse/to.hpp>
#include <userver/utils/meta.hpp>
#include <userver/utils/impl/type_list.hpp>
#include <userver/formats/common/meta.hpp>

USERVER_NAMESPACE_BEGIN

namespace formats::parse {

namespace impl {

template <typename T, typename Value>
constexpr inline bool Is(Value&& value) {
if constexpr(std::is_convertible_v<T, std::int64_t>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double and bool are convertible to int64_t

return value.IsInt();
} else if constexpr(std::is_convertible_v<T, std::string>) {
return value.IsString();
} else if constexpr(requires {std::begin(std::declval<T>());}) {
return value.IsArray();
} else if constexpr(std::is_convertible_v<bool, T>) {
return value.IsBool();
} else {
return value.IsObject();
}
}

inline constexpr utils::impl::TypeList<bool, std::int64_t, std::uint64_t, double, std::string> kBaseTypes;


} // namespace impl


template <typename T, typename Value>
constexpr inline std::enable_if_t<utils::impl::AnyOf(utils::impl::IsConvertableCarried<T>(), impl::kBaseTypes), std::optional<T>>
TryParse(Value&& value, userver::formats::parse::To<T>) {
if(!impl::Is<T>(value)) {
return std::nullopt;
}
auto obj = value.template As<std::optional<T>>();
if(obj) {
return obj;
}
return std::nullopt;
}

template <typename T, typename Value>
constexpr inline std::optional<std::optional<T>> TryParse(Value&& value, userver::formats::parse::To<std::optional<T>>) {
auto object = TryParse(std::forward<Value>(value), userver::formats::parse::To<T>{});
if(object) {
return object;
}
return std::nullopt;
}

} // namespace formats::parse

USERVER_NAMESPACE_END
139 changes: 139 additions & 0 deletions universal/include/userver/formats/universal/common_checks.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@

#pragma once
#include <userver/formats/universal/universal.hpp>
#include <userver/utils/regex.hpp>
#include <fmt/format.h>
#include <map>

USERVER_NAMESPACE_BEGIN
namespace formats::universal::impl {


template <auto Value>
struct Min {
static constexpr auto kValue = Value;
};

template <auto Value>
struct Max {
static constexpr auto kValue = Value;
};

template <auto Value>
struct Default {
static constexpr auto kValue = Value;
};

template <utils::ConstexprString Regex>
struct Pattern {
static constexpr auto kValue = Regex;
};

struct Additional {};

template <typename Field, auto Value>
constexpr inline std::enable_if_t<!meta::kIsOptional<Field>, bool>
Check(const Field& field, Max<Value>) noexcept {
return Value >= field;
};

template <typename Field, auto Value>
constexpr inline std::enable_if_t<!meta::kIsOptional<Field>, bool>
Check(const Field& field, Min<Value>) noexcept {
return field >= Value;
};

template <typename Field, auto Value>
constexpr inline auto Check(const Field&, Default<Value>) noexcept {
return true;
};

template <utils::ConstexprString Regex>
static const userver::utils::regex kRegex(Regex);

template <utils::ConstexprString Regex>
constexpr inline auto Check(const std::string& field, Pattern<Regex>) noexcept {
return utils::regex_match(field, kRegex<Regex>);
};

template <typename Field, auto Value>
constexpr inline auto Check(const std::optional<Field>&, Default<Value>) noexcept {
return true;
};

template <typename Field>
constexpr inline
std::enable_if_t<!meta::kIsOptional<Field>, bool>
Check(const Field&, Additional) noexcept {
return true;
};

template <typename Key, auto Value>
constexpr inline auto Check(const std::vector<Key>& field, Max<Value>) noexcept {
return Value >= field.size();
};

template <typename Key, typename Tp, auto Value>
constexpr inline auto Check(const std::map<Key, Tp>& field, Max<Value>) noexcept {
return Value >= field.size();
};

template <typename Key, typename Tp, auto Value>
constexpr inline auto Check(const std::unordered_map<Key, Tp>& field, Max<Value>) noexcept {
return Value >= field.size();
};

template <typename Field, typename CheckT>
constexpr inline bool Check(const std::optional<Field>& field, CheckT check) noexcept {
if(field.has_value()) {
return Check(*field, check);
};
return true;
};

template <typename T, auto I, typename Key, typename Value, auto Maximum>
constexpr inline auto ErrorMessage(const std::unordered_map<Key, Value>& field, Max<Maximum>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything that is not used by the user of the library, should be moved into ::impl

Everything that IS used, should be documented. Let's at least add some brief single-phrase /// docs for now to make sure that for every entity, we decide if it is public API

return fmt::format("Error with field {0} Map size: {1} Maximum Size: {2}", boost::pfr::get_name<I, T>(), field.size(), Maximum);
};

template <typename T, auto I, typename Field, template <auto> typename Check, auto Value>
constexpr inline auto ErrorMessage(const Field& field, Check<Value>) {
return fmt::format("Error with field {0} Field value: {1} Check Value: {2}", boost::pfr::get_name<I, T>(), field, Value);
};


template <typename T, auto I, typename Value, typename Check>
constexpr inline auto ErrorMessage(const std::unordered_map<std::string, Value>&, Additional) {
return "Error";
};

template <typename T, auto I, typename Field, typename Check>
constexpr inline auto ErrorMessage(const Field&, Check) {
return "Error";
};

template <typename T, auto I, typename Field, template <auto> typename Check, auto Value>
constexpr inline auto ErrorMessage(const std::optional<Field>& field, Check<Value> check) {
return ErrorMessage<T, I>(*field, check);
};

} // namespace formats::universal::impl
namespace formats::universal {
template <auto Value>
inline constexpr impl::Min<Value> Min;

template <auto Value>
inline constexpr impl::Max<Value> Max;

template <auto Value>
inline constexpr impl::Default<Value> Default;

inline constexpr impl::Additional Additional;

template <utils::ConstexprString Regex>
inline constexpr impl::Pattern<Regex> Pattern;

} // namespace formats::universal

USERVER_NAMESPACE_END

Loading