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
1 change: 1 addition & 0 deletions universal/include/userver/formats/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <userver/formats/json/serialize.hpp>
#include <userver/formats/json/value.hpp>
#include <userver/formats/json/value_builder.hpp>
#include <userver/formats/json/universal.hpp>
Anton3 marked this conversation as resolved.
Show resolved Hide resolved

USERVER_NAMESPACE_BEGIN

Expand Down
27 changes: 27 additions & 0 deletions universal/include/userver/formats/json/universal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

linuxnyasha marked this conversation as resolved.
Show resolved Hide resolved
#pragma once
#include <userver/formats/universal/universal.hpp>
#include <userver/formats/json.hpp>
linuxnyasha marked this conversation as resolved.
Show resolved Hide resolved
#include <type_traits>

USERVER_NAMESPACE_BEGIN
namespace formats::json {

template <typename T>
inline constexpr
std::enable_if_t<!std::is_same_v<decltype(universal::kSerialization<std::remove_cvref_t<T>>), const universal::detail::Disabled>, Value>
Serialize(T&& obj,
serialize::To<Value>) {
using Config = std::remove_const_t<decltype(universal::kSerialization<std::remove_cvref_t<T>>)>;
using Type = std::remove_cvref_t<T>;
return [&]<typename... Params>
(universal::SerializationConfig<Type, Params...>){
ValueBuilder builder;
(universal::detail::UniversalSerializeField(Params{}, builder, obj), ...);
return builder.ExtractValue();
}(Config{});
};


} // namespace formats::json
USERVER_NAMESPACE_END
1 change: 1 addition & 0 deletions universal/include/userver/formats/parse/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <userver/utils/datetime/from_string_saturating.hpp>
#include <userver/utils/meta.hpp>
#include <userver/utils/string_to_duration.hpp>
#include <userver/formats/parse/try_parse.hpp>
linuxnyasha marked this conversation as resolved.
Show resolved Hide resolved

USERVER_NAMESPACE_BEGIN

Expand Down
53 changes: 53 additions & 0 deletions universal/include/userver/formats/parse/try_parse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once
#include <type_traits>
#include <string>
#include <cstdint>
#include <userver/formats/parse/to.hpp>
#include <userver/utils/meta.hpp>
#include <userver/utils/type_list.hpp>
#include <userver/formats/common/meta.hpp>

USERVER_NAMESPACE_BEGIN

namespace formats::parse {

namespace impl {

template <typename T, typename Value>
inline bool Is(Value&& value) {
if constexpr(std::is_convertible_v<T, std::int64_t>) {
linuxnyasha marked this conversation as resolved.
Show resolved Hide resolved
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::TypeList<bool, int, std::int64_t, std::uint64_t, double, std::string> kBaseTypes;


} // namespace impl


template <typename T, typename Value>
inline std::enable_if_t<utils::AnyOf(utils::IsSameCarried<T>(), impl::kBaseTypes), std::optional<T>>
Anton3 marked this conversation as resolved.
Show resolved Hide resolved
TryParse(Value&& value, userver::formats::parse::To<T>) {
if(value.IsMissing() || value.IsNull() || !impl::Is<T>(value)) {
Anton3 marked this conversation as resolved.
Show resolved Hide resolved
return std::nullopt;
}
return value.template As<T>();
}

template <typename T, typename Value>
inline 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
};

} // namespace formats::parse

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

#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 {

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 <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 auto Check(const Field& field, Max<Value>) noexcept {
return Value >= field;
};

template <typename Field, auto Value>
constexpr inline auto 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 auto 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

USERVER_NAMESPACE_END

Loading
Loading