adapters.hpp 15 KB

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