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 <optional>
10#include <string_view>
11#include <type_traits>
12
13#include <userver/formats/common/meta.hpp>
14#include <userver/formats/parse/to.hpp>
15#include <userver/utils/meta.hpp>
16
17/// @brief Boost.UUID helpers referenced by format parsers and serializers.
18namespace boost::uuids {
19struct uuid;
20}
21
22USERVER_NAMESPACE_BEGIN
23
24namespace utils::impl::strong_typedef {
25struct StrongTypedefTag;
26}
27
28namespace formats::parse {
29
30namespace impl {
31
32template <typename T, class Value>
33inline T AsExtractor(const Value& value) {
34 return value.template As<T>();
35}
36
37template <typename T, class Value>
38inline T ConvertToExtractor(const Value& value) {
39 return value.template ConvertTo<T>();
40}
41
42template <typename ArrayType, class Value, typename ExtractFunc>
43ArrayType ParseArray(const Value& value, ExtractFunc extract_func) {
44 value.CheckArrayOrNull();
45 ArrayType response;
46 auto inserter = std::inserter(response, response.end());
47
48 for (const auto& subitem : value) {
49 *inserter = extract_func(subitem);
50 ++inserter;
51 }
52
53 return response;
54}
55
56template <typename ObjectType, class Value, typename ExtractFunc>
57ObjectType ParseObject(const Value& value, ExtractFunc extract_func) {
58 using KeyType = typename ObjectType::key_type;
59
60 value.CheckObjectOrNull();
61 ObjectType result;
62
63 const auto end = value.end();
64 for (auto it = value.begin(); it != end; ++it) {
65 if constexpr (std::is_constructible_v<KeyType, std::string>) {
66 result.emplace(it.GetName(), extract_func(*it));
67 } else {
68 result.emplace(Parse(std::string_view(it.GetName()), To<KeyType>{}), extract_func(*it));
69 }
70 }
71
72 return result;
73}
74
75template <typename T>
76concept RangeNotMap =
77 meta::kIsRange<T> && !meta::kIsMap<T> && !std::is_same_v<T, boost::uuids::uuid> &&
78 !std::is_convertible_v<T&, utils::impl::strong_typedef::StrongTypedefTag&>;
79
80} // namespace impl
81
82template <impl::RangeNotMap T, common::IsFormatValue Value>
83T Parse(const Value& value, To<T>) {
84 return impl::ParseArray<T>(value, &impl::AsExtractor<meta::RangeValueType<T>, Value>);
85}
86
87template <meta::kIsMap T, common::IsFormatValue Value>
88T Parse(const Value& value, To<T>) {
89 return impl::ParseObject<T>(value, &impl::AsExtractor<typename T::mapped_type, Value>);
90}
91
92template <typename T, typename Value>
93std::optional<decltype(Parse(std::declval<Value>(), To<T>{}))> Parse(const Value& value, To<std::optional<T>>) {
94 if (value.IsMissing() || value.IsNull()) {
95 return std::nullopt;
96 }
97 return value.template As<T>();
98}
99
100template <class Value>
101std::optional<std::nullptr_t> Parse(const Value&, To<std::optional<std::nullptr_t>>) {
102 static_assert(!sizeof(Value), "optional<nullptr_t> is forbidden, check IsNull() instead");
103 return nullptr;
104}
105
106template <impl::RangeNotMap T, typename Value>
107T Convert(const Value& value, To<T>) {
108 if (value.IsMissing()) {
109 return {};
110 }
111 return impl::ParseArray<T>(value, &impl::ConvertToExtractor<meta::RangeValueType<T>, Value>);
112}
113
114template <meta::kIsMap T, typename Value>
115T Convert(const Value& value, To<T>) {
116 if (value.IsMissing()) {
117 return {};
118 }
119 return impl::ParseObject<T>(value, &impl::ConvertToExtractor<typename T::mapped_type, Value>);
120}
121
122template <typename T, typename Value>
123std::optional<T> Convert(const Value& value, To<std::optional<T>>) {
124 if (value.IsMissing() || value.IsNull()) {
125 return std::nullopt;
126 }
127 return value.template ConvertTo<T>();
128}
129
130template <class Value>
131std::optional<std::nullptr_t> Convert(const Value&, To<std::optional<std::nullptr_t>>) {
132 static_assert(!sizeof(Value), "optional<nullptr_t> is forbidden, check IsNull() instead");
133 return nullptr;
134}
135
136} // namespace formats::parse
137
138USERVER_NAMESPACE_END