-
Notifications
You must be signed in to change notification settings - Fork 300
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
linuxnyasha
wants to merge
15
commits into
userver-framework:develop
Choose a base branch
from
linuxnyasha:feature_reflection_serialize_2
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,317
−0
Open
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
82003c1
Add Universal Serialization
linuxnyasha b49d32c
Fixes
linuxnyasha f1260f2
Fixes
linuxnyasha cf62dc4
Fixes
linuxnyasha ebb0d3e
Fixes
linuxnyasha d3a97d4
Fixes
linuxnyasha aee757f
Fixes
linuxnyasha 8986d53
Fixes
linuxnyasha 8e942ae
Fixes
linuxnyasha 30b890b
Fixes
linuxnyasha c3323b9
Fixes
linuxnyasha 1af8915
Fixes
linuxnyasha fc62f92
Fixes
linuxnyasha 30a7b78
Fixes
linuxnyasha 4c2bd65
Fixes
linuxnyasha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#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; | ||
}; | ||
|
||
} // 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> { | ||
if constexpr(std::is_unsigned_v<T>) { | ||
if(!((sizeof(T) == sizeof(std::uint64_t)) ? value.IsUInt64() : value.IsInt64())) { | ||
linuxnyasha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return std::nullopt; | ||
} | ||
auto response = value.template As<std::uint64_t>(); | ||
return impl::CheckInBounds(response, std::numeric_limits<T>::min(), std::numeric_limits<T>::max()) ? std::optional{static_cast<T>(response)} : std::nullopt; | ||
} else { | ||
if(!value.IsInt64()) { | ||
return std::nullopt; | ||
} | ||
auto response = value.template As<std::int64_t>(); | ||
return impl::CheckInBounds(response, std::numeric_limits<T>::min(), std::numeric_limits<T>::max()) ? std::optional{static_cast<T>(response)} : std::nullopt; | ||
linuxnyasha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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