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

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
115 changes: 115 additions & 0 deletions universal/include/userver/formats/parse/try_parse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#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 {
bool CheckInBounds(const auto& x, const auto& min, const auto& max) {
if (x < min || x > max) {
return false;
};
return true;
Comment on lines +19 to +22
Copy link
Member

Choose a reason for hiding this comment

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

This can simplified. The other changes may (or may not) render this comment obsolete, though

Copy link
Member

Choose a reason for hiding this comment

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

return x >= min && x <= max

};

} // namespace impl

template <typename Value, typename T>
inline constexpr std::optional<T> TryParse(Value&& value, To<T>) {
if(!value.IsObject()) {
return std::nullopt;
}
return value.template As<T>();
}

template <typename Value>
inline constexpr std::optional<bool> TryParse(Value&& value, To<bool>) {
if(!value.IsBool()) {
return std::nullopt;
}
return value.template As<bool>();
}
template <typename Value>
inline constexpr std::optional<double> TryParse(Value&& value, To<double>) {
if(!value.IsDouble()) {
return std::nullopt;
}
return value.template As<double>();
}
template <typename Value>
inline constexpr std::optional<std::string> TryParse(Value&& value, To<std::string>) {
if(!value.IsString()) {
return std::nullopt;
}
return value.template As<std::string>();
}

template <typename Value, typename T>
inline constexpr std::optional<T> TryParse(Value&& value, To<T>) requires meta::kIsInteger<T> {
constexpr auto f = [](auto response){
return impl::CheckInBounds(response, std::numeric_limits<T>::min(), std::numeric_limits<T>::max()) ? std::optional{static_cast<T>(response)} : std::nullopt;
};
if constexpr(std::is_unsigned_v<T>) {
if(!value.IsUInt64()) {
return std::nullopt;
}
auto response = value.template As<std::uint64_t>();
return f(std::move(response));
} else {
if(!value.IsInt64()) {
return std::nullopt;
}
auto response = value.template As<std::int64_t>();
return f(std::move(response));
}
}
template <typename Value, typename T>
inline constexpr std::optional<std::vector<T>> TryParse(Value&& value, To<std::vector<T>>) {
if(!value.IsArray()) {
return std::nullopt;
}
std::vector<T> response;
response.reserve(value.GetSize());
for(const auto& item : value) {
auto insert = TryParse(item, To<T>{});
if(!insert) {
return std::nullopt;
}
response.push_back(std::move(*insert));
}
return response;
}

template <typename T, typename Value>
constexpr inline std::optional<std::optional<T>> TryParse(Value&& value, To<std::optional<T>>) {
if (value.IsMissing() || value.IsNull()) {
return std::optional<T>{std::nullopt};
}
return TryParse(value, To<T>{});
}

template <typename Value>
constexpr inline std::optional<float> TryParse(Value&& value, 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;
}



} // namespace formats::parse

USERVER_NAMESPACE_END
Loading
Loading