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