Loading...
Searching...
No Matches
adapters.hpp
1/* Copyright (c) 2018-2024 Marcelo Zimbres Silva (mzimbres@gmail.com)
2 *
3 * Distributed under the Boost Software License, Version 1.0. (See
4 * accompanying file LICENSE.txt)
5 */
6
7#ifndef BOOST_REDIS_ADAPTER_ADAPTERS_HPP
8#define BOOST_REDIS_ADAPTER_ADAPTERS_HPP
9
10#include <boost/redis/error.hpp>
11#include <boost/redis/resp3/type.hpp>
12#include <boost/redis/resp3/serialization.hpp>
13#include <boost/redis/resp3/node.hpp>
14#include <boost/redis/adapter/result.hpp>
15#include <boost/assert.hpp>
16
17#include <set>
18#include <optional>
19#include <unordered_set>
20#include <forward_list>
21#include <system_error>
22#include <map>
23#include <unordered_map>
24#include <list>
25#include <deque>
26#include <vector>
27#include <array>
28#include <string_view>
29#include <charconv>
30
31// See https://stackoverflow.com/a/31658120/1077832
32#ifdef _LIBCPP_VERSION
33#else
34#include <cstdlib>
35#endif
36
37namespace boost::redis::adapter::detail
38{
39
40template <class> struct is_integral : std::false_type {};
41
42template <> struct is_integral<long long int > : std::true_type {};
43template <> struct is_integral<unsigned long long int> : std::true_type {};
44template <> struct is_integral<int > : std::true_type {};
45
46template<class T, bool = is_integral<T>::value>
47struct converter;
48
49template<class T>
50struct converter<T, true> {
51 template <class String>
52 static void
53 apply(
54 T& i,
55 resp3::basic_node<String> const& node,
56 system::error_code& ec)
57 {
58 auto const res =
59 std::from_chars(node.value.data(), node.value.data() + node.value.size(), i);
60 if (res.ec != std::errc())
62 }
63};
64
65template<>
66struct converter<bool, false> {
67 template <class String>
68 static void
69 apply(
70 bool& t,
71 resp3::basic_node<String> const& node,
72 system::error_code& ec)
73 {
74 t = *node.value.data() == 't';
75 }
76};
77
78template<>
79struct converter<double, false> {
80 template <class String>
81 static void
82 apply(
83 double& d,
84 resp3::basic_node<String> const& node,
85 system::error_code& ec)
86 {
87#ifdef _LIBCPP_VERSION
88 // The string in node.value is not null terminated and we also
89 // don't know if there is enough space at the end for a null
90 // char. The easiest thing to do is to create a temporary.
91 std::string const tmp{node.value.data(), node.value.data() + node.value.size()};
92 char* end{};
93 d = std::strtod(tmp.data(), &end);
94 if (d == HUGE_VAL || d == 0)
96#else
97 auto const res = std::from_chars(node.value.data(), node.value.data() + node.value.size(), d);
98 if (res.ec != std::errc())
100#endif // _LIBCPP_VERSION
101 }
102};
103
104template <class CharT, class Traits, class Allocator>
105struct converter<std::basic_string<CharT, Traits, Allocator>, false> {
106 template <class String>
107 static void
108 apply(
109 std::basic_string<CharT, Traits, Allocator>& s,
110 resp3::basic_node<String> const& node,
111 system::error_code&)
112 {
113 s.append(node.value.data(), node.value.size());
114 }
115};
116
117template <class T>
118struct from_bulk_impl {
119 template <class String>
120 static void
121 apply(
122 T& t,
123 resp3::basic_node<String> const& node,
124 system::error_code& ec)
125 {
126 converter<T>::apply(t, node, ec);
127 }
128};
129
130template <class T>
131struct from_bulk_impl<std::optional<T>> {
132 template <class String>
133 static void
134 apply(
135 std::optional<T>& op,
136 resp3::basic_node<String> const& node,
137 system::error_code& ec)
138 {
139 if (node.data_type != resp3::type::null) {
140 op.emplace(T{});
141 converter<T>::apply(op.value(), node, ec);
142 }
143 }
144};
145
146template <class T, class String>
147void
148boost_redis_from_bulk(
149 T& t,
150 resp3::basic_node<String> const& node,
151 system::error_code& ec)
152{
153 from_bulk_impl<T>::apply(t, node, ec);
154}
155
156//================================================
157
158template <class Result>
159class general_aggregate {
160private:
161 Result* result_;
162
163public:
164 explicit general_aggregate(Result* c = nullptr): result_(c) {}
165 template <class String>
166 void operator()(resp3::basic_node<String> const& nd, system::error_code&)
167 {
168 BOOST_ASSERT_MSG(!!result_, "Unexpected null pointer");
169 switch (nd.data_type) {
172 *result_ = error{nd.data_type, std::string{std::cbegin(nd.value), std::cend(nd.value)}};
173 break;
174 default:
175 result_->value().push_back({nd.data_type, nd.aggregate_size, nd.depth, std::string{std::cbegin(nd.value), std::cend(nd.value)}});
176 }
177 }
178};
179
180template <class Node>
181class general_simple {
182private:
183 Node* result_;
184
185public:
186 explicit general_simple(Node* t = nullptr) : result_(t) {}
187
188 template <class String>
189 void operator()(resp3::basic_node<String> const& nd, system::error_code&)
190 {
191 BOOST_ASSERT_MSG(!!result_, "Unexpected null pointer");
192 switch (nd.data_type) {
195 *result_ = error{nd.data_type, std::string{std::cbegin(nd.value), std::cend(nd.value)}};
196 break;
197 default:
198 result_->value().data_type = nd.data_type;
199 result_->value().aggregate_size = nd.aggregate_size;
200 result_->value().depth = nd.depth;
201 result_->value().value.assign(nd.value.data(), nd.value.size());
202 }
203 }
204};
205
206template <class Result>
207class simple_impl {
208public:
209 void on_value_available(Result&) {}
210
211 template <class String>
212 void operator()(Result& result, resp3::basic_node<String> const& node, system::error_code& ec)
213 {
214 if (is_aggregate(node.data_type)) {
216 return;
217 }
218
219 boost_redis_from_bulk(result, node, ec);
220 }
221};
222
223template <class Result>
224class set_impl {
225private:
226 typename Result::iterator hint_;
227
228public:
229 void on_value_available(Result& result)
230 { hint_ = std::end(result); }
231
232 template <class String>
233 void operator()(Result& result, resp3::basic_node<String> const& nd, system::error_code& ec)
234 {
235 if (is_aggregate(nd.data_type)) {
236 if (nd.data_type != resp3::type::set)
238 return;
239 }
240
241 BOOST_ASSERT(nd.aggregate_size == 1);
242
243 if (nd.depth < 1) {
245 return;
246 }
247
248 typename Result::key_type obj;
249 boost_redis_from_bulk(obj, nd, ec);
250 hint_ = result.insert(hint_, std::move(obj));
251 }
252};
253
254template <class Result>
255class map_impl {
256private:
257 typename Result::iterator current_;
258 bool on_key_ = true;
259
260public:
261 void on_value_available(Result& result)
262 { current_ = std::end(result); }
263
264 template <class String>
265 void operator()(Result& result, resp3::basic_node<String> const& nd, system::error_code& ec)
266 {
267 if (is_aggregate(nd.data_type)) {
268 if (element_multiplicity(nd.data_type) != 2)
270 return;
271 }
272
273 BOOST_ASSERT(nd.aggregate_size == 1);
274
275 if (nd.depth < 1) {
277 return;
278 }
279
280 if (on_key_) {
281 typename Result::key_type obj;
282 boost_redis_from_bulk(obj, nd, ec);
283 current_ = result.insert(current_, {std::move(obj), {}});
284 } else {
285 typename Result::mapped_type obj;
286 boost_redis_from_bulk(obj, nd, ec);
287 current_->second = std::move(obj);
288 }
289
290 on_key_ = !on_key_;
291 }
292};
293
294template <class Result>
295class vector_impl {
296public:
297 void on_value_available(Result& ) { }
298
299 template <class String>
300 void operator()(Result& result, resp3::basic_node<String> const& nd, system::error_code& ec)
301 {
302 if (is_aggregate(nd.data_type)) {
303 auto const m = element_multiplicity(nd.data_type);
304 result.reserve(result.size() + m * nd.aggregate_size);
305 } else {
306 result.push_back({});
307 boost_redis_from_bulk(result.back(), nd, ec);
308 }
309 }
310};
311
312template <class Result>
313class array_impl {
314private:
315 int i_ = -1;
316
317public:
318 void on_value_available(Result& ) { }
319
320 template <class String>
321 void operator()(Result& result, resp3::basic_node<String> const& nd, system::error_code& ec)
322 {
323 if (is_aggregate(nd.data_type)) {
324 if (i_ != -1) {
326 return;
327 }
328
329 if (result.size() != nd.aggregate_size * element_multiplicity(nd.data_type)) {
331 return;
332 }
333 } else {
334 if (i_ == -1) {
336 return;
337 }
338
339 BOOST_ASSERT(nd.aggregate_size == 1);
340 boost_redis_from_bulk(result.at(i_), nd, ec);
341 }
342
343 ++i_;
344 }
345};
346
347template <class Result>
348struct list_impl {
349
350 void on_value_available(Result& ) { }
351
352 template <class String>
353 void operator()(Result& result, resp3::basic_node<String> const& nd, system::error_code& ec)
354 {
355 if (!is_aggregate(nd.data_type)) {
356 BOOST_ASSERT(nd.aggregate_size == 1);
357 if (nd.depth < 1) {
359 return;
360 }
361
362 result.push_back({});
363 boost_redis_from_bulk(result.back(), nd, ec);
364 }
365 }
366};
367
368//---------------------------------------------------
369
370template <class T>
371struct impl_map { using type = simple_impl<T>; };
372
373template <class Key, class Compare, class Allocator>
374struct impl_map<std::set<Key, Compare, Allocator>> { using type = set_impl<std::set<Key, Compare, Allocator>>; };
375
376template <class Key, class Compare, class Allocator>
377struct impl_map<std::multiset<Key, Compare, Allocator>> { using type = set_impl<std::multiset<Key, Compare, Allocator>>; };
378
379template <class Key, class Hash, class KeyEqual, class Allocator>
380struct impl_map<std::unordered_set<Key, Hash, KeyEqual, Allocator>> { using type = set_impl<std::unordered_set<Key, Hash, KeyEqual, Allocator>>; };
381
382template <class Key, class Hash, class KeyEqual, class Allocator>
383struct impl_map<std::unordered_multiset<Key, Hash, KeyEqual, Allocator>> { using type = set_impl<std::unordered_multiset<Key, Hash, KeyEqual, Allocator>>; };
384
385template <class Key, class T, class Compare, class Allocator>
386struct impl_map<std::map<Key, T, Compare, Allocator>> { using type = map_impl<std::map<Key, T, Compare, Allocator>>; };
387
388template <class Key, class T, class Compare, class Allocator>
389struct impl_map<std::multimap<Key, T, Compare, Allocator>> { using type = map_impl<std::multimap<Key, T, Compare, Allocator>>; };
390
391template <class Key, class Hash, class KeyEqual, class Allocator>
392struct impl_map<std::unordered_map<Key, Hash, KeyEqual, Allocator>> { using type = map_impl<std::unordered_map<Key, Hash, KeyEqual, Allocator>>; };
393
394template <class Key, class Hash, class KeyEqual, class Allocator>
395struct impl_map<std::unordered_multimap<Key, Hash, KeyEqual, Allocator>> { using type = map_impl<std::unordered_multimap<Key, Hash, KeyEqual, Allocator>>; };
396
397template <class T, class Allocator>
398struct impl_map<std::vector<T, Allocator>> { using type = vector_impl<std::vector<T, Allocator>>; };
399
400template <class T, std::size_t N>
401struct impl_map<std::array<T, N>> { using type = array_impl<std::array<T, N>>; };
402
403template <class T, class Allocator>
404struct impl_map<std::list<T, Allocator>> { using type = list_impl<std::list<T, Allocator>>; };
405
406template <class T, class Allocator>
407struct impl_map<std::deque<T, Allocator>> { using type = list_impl<std::deque<T, Allocator>>; };
408
409//---------------------------------------------------
410
411template <class>
412class wrapper;
413
414template <class T>
415class wrapper<result<T>> {
416public:
417 using response_type = result<T>;
418private:
419 response_type* result_;
420 typename impl_map<T>::type impl_;
421 bool called_once_ = false;
422
423 template <class String>
424 bool set_if_resp3_error(resp3::basic_node<String> const& nd) noexcept
425 {
426 switch (nd.data_type) {
430 *result_ = error{nd.data_type, {std::cbegin(nd.value), std::cend(nd.value)}};
431 return true;
432 default:
433 return false;
434 }
435 }
436
437public:
438 explicit wrapper(response_type* t = nullptr) : result_(t)
439 {
440 if (result_) {
441 result_->value() = T{};
442 impl_.on_value_available(result_->value());
443 }
444 }
445
446 template <class String>
447 void operator()(resp3::basic_node<String> const& nd, system::error_code& ec)
448 {
449 BOOST_ASSERT_MSG(!!result_, "Unexpected null pointer");
450
451 if (result_->has_error())
452 return;
453
454 if (!std::exchange(called_once_, true) && set_if_resp3_error(nd))
455 return;
456
457 BOOST_ASSERT(result_);
458 impl_(result_->value(), nd, ec);
459 }
460};
461
462template <class T>
463class wrapper<result<std::optional<T>>> {
464public:
465 using response_type = result<std::optional<T>>;
466
467private:
468 response_type* result_;
469 typename impl_map<T>::type impl_{};
470 bool called_once_ = false;
471
472 template <class String>
473 bool set_if_resp3_error(resp3::basic_node<String> const& nd) noexcept
474 {
475 switch (nd.data_type) {
478 *result_ = error{nd.data_type, {std::cbegin(nd.value), std::cend(nd.value)}};
479 return true;
480 default:
481 return false;
482 }
483 }
484
485public:
486 explicit wrapper(response_type* o = nullptr) : result_(o) {}
487
488 template <class String>
489 void
490 operator()(
491 resp3::basic_node<String> const& nd,
492 system::error_code& ec)
493 {
494 BOOST_ASSERT_MSG(!!result_, "Unexpected null pointer");
495
496 if (result_->has_error())
497 return;
498
499 if (set_if_resp3_error(nd))
500 return;
501
502 if (!std::exchange(called_once_, true) && nd.data_type == resp3::type::null)
503 return;
504
505 if (!result_->value().has_value()) {
506 result_->value() = T{};
507 impl_.on_value_available(result_->value().value());
508 }
509
510 impl_(result_->value().value(), nd, ec);
511 }
512};
513
514} // boost::redis::adapter::detail
515
516#endif // BOOST_REDIS_ADAPTER_ADAPTERS_HPP
error
Generic errors.
Definition error.hpp:18
system::result< Value, error > result
Stores response to individual Redis commands.
Definition result.hpp:56
basic_node< std::string > node
A node in the response tree that owns its data.
Definition node.hpp:60
@ expects_resp3_set
Expects a set aggregate but got something else.
@ incompatible_size
Aggregate container has incompatible size.
@ not_a_number
Can't parse the string as a number.
@ nested_aggregate_not_supported
Nested response not supported.
@ expects_resp3_map
Expects a map but got other aggregate.
@ not_a_double
Not a double.
@ expects_resp3_aggregate
Expects aggregate.
@ expects_resp3_simple_type
Expects a simple RESP3 type but got an aggregate.