Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat formats: automatic serialization/deserialization based on Boost.PFR #469

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
97 changes: 97 additions & 0 deletions universal/include/userver/formats/parse/try_parse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#pragma once
#include <type_traits>
#include <string>
#include <cstdint>
#include <limits>
#include <userver/formats/parse/to.hpp>
#include <userver/utils/meta.hpp>
#include <userver/utils/impl/type_list.hpp>
#include <userver/formats/common/meta.hpp>
#include <userver/formats/parse/common_containers.hpp>


USERVER_NAMESPACE_BEGIN

namespace formats::parse {

namespace impl {

template <typename T, typename Value>
constexpr inline bool Is(Value&& value) {
if constexpr(std::is_same_v<T, bool>) {
return value.IsBool();
} else if constexpr(meta::kIsInteger<T>) {
return (std::is_unsigned_v<T> && sizeof(T) == sizeof(std::uint64_t)) ? value.IsUInt64() : value.IsInt64();
} else if constexpr(std::is_convertible_v<T, std::string>) {
return value.IsString();
} else if constexpr(std::is_convertible_v<T, double>) {
return value.IsDouble();
} else if constexpr(meta::kIsRange<T>) {
return value.IsArray();
} else {
return value.IsObject();
}
}
bool CheckInBounds(const auto& x, const auto& min, const auto& max) {
if (x < min || x > max) {
return false;
};
return true;
};
inline constexpr utils::impl::TypeList<bool, double, std::string> kBaseTypes;

} // namespace impl


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

template <typename T, typename Value>
constexpr inline std::optional<std::optional<T>> TryParse(Value&& value, userver::formats::parse::To<std::optional<T>>) {
return TryParse(std::forward<Value>(value), userver::formats::parse::To<T>{});
Anton3 marked this conversation as resolved.
Show resolved Hide resolved
}
template <typename Value>
constexpr inline std::optional<float> TryParse(Value&& value, userver::formats::parse::To<float>) {
auto response = value.template As<double>();
if(impl::CheckInBounds(response, std::numeric_limits<float>::lowest(),
std::numeric_limits<float>::max())) {
return static_cast<float>(response);
};
return std::nullopt;
};

template <typename T, typename Value>
constexpr inline std::enable_if_t<meta::kIsRange<T> && !meta::kIsMap<T> &&
!std::is_same_v<T, boost::uuids::uuid> &&
!utils::impl::AnyOf(utils::impl::IsConvertableCarried<T>(), impl::kBaseTypes) &&
!std::is_convertible_v<
T&, utils::impl::strong_typedef::StrongTypedefTag&>,
std::optional<T>>
TryParse(Value&& from, To<T>) {
T response;
auto inserter = std::inserter(response, response.end());
using ValueType = meta::RangeValueType<T>;
for(const auto& item : from) {
auto insert = TryParse(item, userver::formats::parse::To<ValueType>{});
if(!insert) {
return std::nullopt;
};
*inserter = *insert;
++inserter;
};
return response;
}



} // namespace formats::parse

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

#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 {
linuxnyasha marked this conversation as resolved.
Show resolved Hide resolved
Anton3 marked this conversation as resolved.
Show resolved Hide resolved
static constexpr auto kValue = Value;
};

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

template <std::size_t Value>
struct MaxItems {
static constexpr auto kValue = Value;
};

template <std::size_t Value>
struct MinItems {
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;
};

template <auto... Checks>
struct Items {
static constexpr auto kChecks = std::make_tuple(Checks...);
};

struct Additional {};


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

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

template <typename Field, auto... Checks>
constexpr inline std::enable_if_t<!meta::kIsOptional<Field>, bool>
Check(const Field& field, Items<Checks...>) {
for(const auto& element : field) {
if(!(Check(element, Checks) && ...)) {
return false;
};
};
return true;
};


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 {
for(const auto& element : field) {
if(!(Value <= field)) {
return false;
};
};
return true;
};

template <typename Key, auto Value>
constexpr inline auto Check(const std::vector<Key>& field, Min<Value>) noexcept {
for(const auto& element : field) {
if(!(Value >= field)) {
return false;
};
};
return true;
};

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;

template <auto... Params>
inline constexpr impl::Items<Params...> Items;


inline constexpr impl::Additional Additional;

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

template <std::size_t Value>
inline constexpr impl::MaxItems<Value> MaxItems;

template <std::size_t Value>
inline constexpr impl::MinItems<Value> MinItems;

} // namespace formats::universal

USERVER_NAMESPACE_END

Loading
Loading