userver: userver/formats/parse/common_containers.hpp Source File
Loading...
Searching...
No Matches
common_containers.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/formats/parse/common_containers.hpp
4/// @brief Parsers and converters for Standard Library containers and
5/// std::optional
6///
7/// @ingroup userver_universal userver_formats_parse
8
9#include <algorithm>
10#include <cstddef>
11#include <iterator>
12#include <optional>
13#include <string_view>
14#include <type_traits>
15
16#include <userver/formats/common/meta.hpp>
17#include <userver/formats/parse/to.hpp>
18#include <userver/utils/meta.hpp>
19
20/// @brief Boost.UUID helpers referenced by format parsers and serializers.
21namespace boost::uuids {
22struct uuid;
23}
24
25USERVER_NAMESPACE_BEGIN
26
27namespace utils::impl::strong_typedef {
28struct StrongTypedefTag;
29}
30
31namespace formats::parse {
32
33namespace impl {
34
35template <typename T, class Value>
36inline T AsExtractor(const Value& value) {
37 return value.template As<T>();
38}
39
40template <typename T, class Value>
41inline T ConvertToExtractor(const Value& value) {
42 return value.template ConvertTo<T>();
43}
44
45template <typename Container, typename Value>
46void ReserveForParse(Container& container, const Value& value) {
47 if constexpr (requires { container.reserve(value.GetSize()); }) {
48 auto size = value.GetSize();
49 if constexpr (requires { container.max_size(); }) {
50 size = std::min<std::size_t>(size, container.max_size());
51 }
52 container.reserve(size);
53 }
54}
55
56template <typename ArrayType, class Value, typename ExtractFunc>
57ArrayType ParseArray(const Value& value, ExtractFunc extract_func) {
58 value.CheckArrayOrNull();
59 ArrayType response;
60 const auto end = value.end();
61 impl::ReserveForParse(response, value);
62 auto inserter = std::inserter(response, response.end());
63
64 for (auto it = value.begin(); it != end; ++it) {
65 *inserter = extract_func(*it);
66 ++inserter;
67 }
68
69 return response;
70}
71
72template <typename ObjectType, class Value, typename ExtractFunc>
73ObjectType ParseObject(const Value& value, ExtractFunc extract_func) {
74 using KeyType = typename ObjectType::key_type;
75
76 value.CheckObjectOrNull();
77 ObjectType result;
78
79 const auto end = value.end();
80 impl::ReserveForParse(result, value);
81 for (auto it = value.begin(); it != end; ++it) {
82 if constexpr (std::is_constructible_v<KeyType, std::string>) {
83 result.emplace(it.GetName(), extract_func(*it));
84 } else {
85 result.emplace(Parse(std::string_view(it.GetName()), To<KeyType>{}), extract_func(*it));
86 }
87 }
88
89 return result;
90}
91
92template <typename T>
93concept RangeNotMap =
94 meta::IsRange<T> && !meta::IsMap<T> && !std::is_same_v<T, boost::uuids::uuid> &&
95 !std::is_convertible_v<T&, utils::impl::strong_typedef::StrongTypedefTag&>;
96
97} // namespace impl
98
99template <impl::RangeNotMap T, common::IsFormatValue Value>
100T Parse(const Value& value, To<T>) {
101 return impl::ParseArray<T>(value, &impl::AsExtractor<meta::RangeValueType<T>, Value>);
102}
103
104template <meta::IsMap T, common::IsFormatValue Value>
105T Parse(const Value& value, To<T>) {
106 return impl::ParseObject<T>(value, &impl::AsExtractor<typename T::mapped_type, Value>);
107}
108
109template <typename T, typename Value>
110std::optional<decltype(Parse(std::declval<Value>(), To<T>{}))> Parse(const Value& value, To<std::optional<T>>) {
111 if (value.IsMissing() || value.IsNull()) {
112 return std::nullopt;
113 }
114 return value.template As<T>();
115}
116
117template <class Value>
118std::optional<std::nullptr_t> Parse(const Value&, To<std::optional<std::nullptr_t>>) {
119 static_assert(!sizeof(Value), "optional<nullptr_t> is forbidden, check IsNull() instead");
120 return nullptr;
121}
122
123template <impl::RangeNotMap T, typename Value>
124T Convert(const Value& value, To<T>) {
125 if (value.IsMissing()) {
126 return {};
127 }
128 return impl::ParseArray<T>(value, &impl::ConvertToExtractor<meta::RangeValueType<T>, Value>);
129}
130
131template <meta::IsMap T, typename Value>
132T Convert(const Value& value, To<T>) {
133 if (value.IsMissing()) {
134 return {};
135 }
136 return impl::ParseObject<T>(value, &impl::ConvertToExtractor<typename T::mapped_type, Value>);
137}
138
139template <typename T, typename Value>
140std::optional<T> Convert(const Value& value, To<std::optional<T>>) {
141 if (value.IsMissing() || value.IsNull()) {
142 return std::nullopt;
143 }
144 return value.template ConvertTo<T>();
145}
146
147template <class Value>
148std::optional<std::nullptr_t> Convert(const Value&, To<std::optional<std::nullptr_t>>) {
149 static_assert(!sizeof(Value), "optional<nullptr_t> is forbidden, check IsNull() instead");
150 return nullptr;
151}
152
153} // namespace formats::parse
154
155USERVER_NAMESPACE_END