boost_redis 1.4.2
A redis client library
serialization.hpp
1/* Copyright (c) 2018-2022 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_RESP3_SERIALIZATION_HPP
8#define BOOST_REDIS_RESP3_SERIALIZATION_HPP
9
10#include <boost/redis/resp3/type.hpp>
11#include <boost/system/system_error.hpp>
12#include <boost/throw_exception.hpp>
13#include <boost/redis/resp3/parser.hpp>
14
15#include <string>
16#include <tuple>
17
18// NOTE: Consider detecting tuples in the type in the parameter pack
19// to calculate the header size correctly.
20
21namespace boost::redis::resp3 {
22
42void boost_redis_to_bulk(std::string& payload, std::string_view data);
43
44template <class T, typename = typename std::enable_if<std::is_integral<T>::value>::type>
45void boost_redis_to_bulk(std::string& payload, T n)
46{
47 auto const s = std::to_string(n);
48 boost::redis::resp3::boost_redis_to_bulk(payload, std::string_view{s});
49}
50
51template <class T>
52struct add_bulk_impl {
53 static void add(std::string& payload, T const& from)
54 {
55 using namespace boost::redis::resp3;
56 boost_redis_to_bulk(payload, from);
57 }
58};
59
60template <class ...Ts>
61struct add_bulk_impl<std::tuple<Ts...>> {
62 static void add(std::string& payload, std::tuple<Ts...> const& t)
63 {
64 auto f = [&](auto const&... vs)
65 {
66 using namespace boost::redis::resp3;
67 (boost_redis_to_bulk(payload, vs), ...);
68 };
69
70 std::apply(f, t);
71 }
72};
73
74template <class U, class V>
75struct add_bulk_impl<std::pair<U, V>> {
76 static void add(std::string& payload, std::pair<U, V> const& from)
77 {
78 using namespace boost::redis::resp3;
79 boost_redis_to_bulk(payload, from.first);
80 boost_redis_to_bulk(payload, from.second);
81 }
82};
83
84void add_header(std::string& payload, type t, std::size_t size);
85
86template <class T>
87void add_bulk(std::string& payload, T const& data)
88{
89 add_bulk_impl<T>::add(payload, data);
90}
91
92template <class>
93struct bulk_counter;
94
95template <class>
96struct bulk_counter {
97 static constexpr auto size = 1U;
98};
99
100template <class T, class U>
101struct bulk_counter<std::pair<T, U>> {
102 static constexpr auto size = 2U;
103};
104
105void add_blob(std::string& payload, std::string_view blob);
106void add_separator(std::string& payload);
107
108namespace detail
109{
110
111template <class Adapter>
112void deserialize(std::string_view const& data, Adapter adapter, system::error_code& ec)
113{
114 parser parser;
115 while (!parser.done()) {
116 auto const res = parser.consume(data, ec);
117 if (ec)
118 return;
119
120 BOOST_ASSERT(res.has_value());
121
122 adapter(res.value(), ec);
123 if (ec)
124 return;
125 }
126
127 BOOST_ASSERT(parser.get_consumed() == std::size(data));
128}
129
130template <class Adapter>
131void deserialize(std::string_view const& data, Adapter adapter)
132{
133 system::error_code ec;
134 deserialize(data, adapter, ec);
135
136 if (ec)
137 BOOST_THROW_EXCEPTION(system::system_error{ec});
138}
139
140}
141
142} // boost::redis::resp3
143
144#endif // BOOST_REDIS_RESP3_SERIALIZATION_HPP
void boost_redis_to_bulk(std::string &payload, std::string_view data)
Adds a bulk to the request.
auto to_string(type t) noexcept -> char const *
Converts the data type to a string.
type
RESP3 data types.
Definition: type.hpp:23