Skip to content

Commit 56d59fe

Browse files
committed
Uses class specialization instead of function overload. [skip-ci]
1 parent 31ceed9 commit 56d59fe

File tree

7 files changed

+221
-59
lines changed

7 files changed

+221
-59
lines changed

example/cpp20_containers.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018-2022 Marcelo Zimbres Silva ([email protected])
1+
/* Copyright (c) 2018-2025 Marcelo Zimbres Silva ([email protected])
22
*
33
* Distributed under the Boost Software License, Version 1.0. (See
44
* accompanying file LICENSE.txt)
@@ -24,13 +24,25 @@ using boost::asio::awaitable;
2424
using boost::asio::detached;
2525
using boost::asio::consign;
2626

27+
template<class T>
28+
std::ostream& operator<<(std::ostream& os, std::optional<T> const& opt)
29+
{
30+
if (opt.has_value())
31+
std::cout << opt.value();
32+
else
33+
std::cout << "null";
34+
35+
return os;
36+
}
37+
2738
void print(std::map<std::string, std::string> const& cont)
2839
{
2940
for (auto const& e: cont)
3041
std::cout << e.first << ": " << e.second << "\n";
3142
}
3243

33-
void print(std::vector<int> const& cont)
44+
template <class T>
45+
void print(std::vector<T> const& cont)
3446
{
3547
for (auto const& e: cont) std::cout << e << " ";
3648
std::cout << "\n";
@@ -48,6 +60,7 @@ auto store(std::shared_ptr<connection> conn) -> awaitable<void>
4860
request req;
4961
req.push_range("RPUSH", "rpush-key", vec);
5062
req.push_range("HSET", "hset-key", map);
63+
req.push("SET", "key", "value");
5164

5265
co_await conn->async_exec(req, ignore);
5366
}
@@ -67,6 +80,21 @@ auto hgetall(std::shared_ptr<connection> conn) -> awaitable<void>
6780
print(std::get<0>(resp).value());
6881
}
6982

83+
auto mget(std::shared_ptr<connection> conn) -> awaitable<void>
84+
{
85+
// A request contains multiple commands.
86+
request req;
87+
req.push("MGET", "key", "non-existing");
88+
89+
// Responses as tuple elements.
90+
response<std::vector<std::optional<std::string>>> resp;
91+
92+
// Executes the request and reads the response.
93+
co_await conn->async_exec(req, resp);
94+
95+
print(std::get<0>(resp).value());
96+
}
97+
7098
// Retrieves in a transaction.
7199
auto transaction(std::shared_ptr<connection> conn) -> awaitable<void>
72100
{
@@ -76,11 +104,15 @@ auto transaction(std::shared_ptr<connection> conn) -> awaitable<void>
76104
req.push("HGETALL", "hset-key"); // Retrieves
77105
req.push("EXEC");
78106

107+
// TODO: Retrieve with MGET
79108
response<
80109
ignore_t, // multi
81110
ignore_t, // lrange
82111
ignore_t, // hgetall
83-
response<std::optional<std::vector<int>>, std::optional<std::map<std::string, std::string>>> // exec
112+
response<
113+
std::optional<std::vector<int>>,
114+
std::optional<std::map<std::string, std::string>>
115+
> // exec
84116
> resp;
85117

86118
co_await conn->async_exec(req, resp);
@@ -98,6 +130,7 @@ awaitable<void> co_main(config cfg)
98130
co_await store(conn);
99131
co_await transaction(conn);
100132
co_await hgetall(conn);
133+
co_await mget(conn);
101134
conn->cancel();
102135
}
103136

example/cpp20_json.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
#include <boost/redis/resp3/serialization.hpp>
2222

2323
namespace asio = boost::asio;
24+
namespace resp3 = boost::redis::resp3;
2425
using namespace boost::describe;
2526
using boost::redis::request;
2627
using boost::redis::response;
2728
using boost::redis::ignore_t;
2829
using boost::redis::config;
2930
using boost::redis::connection;
31+
using boost::redis::resp3::node_view;
3032

3133
// Struct that will be stored in Redis using json serialization.
3234
struct user {
@@ -40,10 +42,18 @@ BOOST_DESCRIBE_STRUCT(user, (), (name, age, country))
4042

4143
// Boost.Redis customization points (example/json.hpp)
4244
void boost_redis_to_bulk(std::string& to, user const& u)
43-
{ boost::redis::resp3::boost_redis_to_bulk(to, boost::json::serialize(boost::json::value_from(u))); }
45+
{
46+
resp3::boost_redis_to_bulk(to, boost::json::serialize(boost::json::value_from(u)));
47+
}
4448

45-
void boost_redis_from_bulk(user& u, std::string_view sv, boost::system::error_code&)
46-
{ u = boost::json::value_to<user>(boost::json::parse(sv)); }
49+
void
50+
boost_redis_from_bulk(
51+
user& u,
52+
node_view const& node,
53+
boost::system::error_code&)
54+
{
55+
u = boost::json::value_to<user>(boost::json::parse(node.value));
56+
}
4757

4858
auto co_main(config cfg) -> asio::awaitable<void>
4959
{

example/cpp20_protobuf.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
2020

2121
namespace asio = boost::asio;
22+
namespace resp3 = boost::redis::resp3;
2223
using boost::redis::request;
2324
using boost::redis::response;
2425
using boost::redis::operation;
2526
using boost::redis::ignore_t;
2627
using boost::redis::config;
2728
using boost::redis::connection;
29+
using boost::redis::resp3::node_view;
2830

2931
// The protobuf type described in example/person.proto
3032
using tutorial::person;
@@ -42,12 +44,16 @@ void boost_redis_to_bulk(std::string& to, person const& u)
4244
if (!u.SerializeToString(&tmp))
4345
throw boost::system::system_error(boost::redis::error::invalid_data_type);
4446

45-
boost::redis::resp3::boost_redis_to_bulk(to, tmp);
47+
resp3::boost_redis_to_bulk(to, tmp);
4648
}
4749

48-
void boost_redis_from_bulk(person& u, std::string_view sv, boost::system::error_code& ec)
50+
void
51+
boost_redis_from_bulk(
52+
person& u,
53+
node_view const& node,
54+
boost::system::error_code& ec)
4955
{
50-
std::string const tmp {sv};
56+
std::string const tmp {node.value};
5157
if (!u.ParseFromString(tmp))
5258
ec = boost::redis::error::invalid_data_type;
5359
}

0 commit comments

Comments
 (0)