Boost.Redis: example/cpp20_protobuf.cpp Source File

Boost.Redis 1.84.0 A redis client library

Loading...
Searching...
No Matches
cpp20_protobuf.cpp
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#include <boost/redis/connection.hpp>
8#include <boost/redis/resp3/serialization.hpp>
9#include <boost/asio/co_spawn.hpp>
10#include <boost/asio/detached.hpp>
11#include <boost/asio/consign.hpp>
12#include <boost/system/errc.hpp>
13#include <iostream>
14
15// See the definition in person.proto. This header is automatically
16// generated by CMakeLists.txt.
17#include "person.pb.h"
18
19#if defined(BOOST_ASIO_HAS_CO_AWAIT)
20
21namespace asio = boost::asio;
22namespace resp3 = boost::redis::resp3;
23using boost::redis::request;
24using boost::redis::response;
25using boost::redis::operation;
26using boost::redis::ignore_t;
27using boost::redis::config;
28using boost::redis::connection;
29using boost::redis::resp3::node_view;
30
31// The protobuf type described in example/person.proto
32using tutorial::person;
33
34// Boost.Redis customization points (example/protobuf.hpp)
35namespace tutorial
36{
37
38// Below I am using a Boost.Redis to indicate a protobuf error, this
39// is ok for an example, users however might want to define their own
40// error codes.
41void boost_redis_to_bulk(std::string& to, person const& u)
42{
43 std::string tmp;
44 if (!u.SerializeToString(&tmp))
45 throw boost::system::system_error(boost::redis::error::invalid_data_type);
46
47 resp3::boost_redis_to_bulk(to, tmp);
48}
49
50void
51boost_redis_from_bulk(
52 person& u,
53 node_view const& node,
54 boost::system::error_code& ec)
55{
56 std::string const tmp {node.value};
57 if (!u.ParseFromString(tmp))
58 ec = boost::redis::error::invalid_data_type;
59}
60
61} // tutorial
62
63using tutorial::boost_redis_to_bulk;
64using tutorial::boost_redis_from_bulk;
65
66asio::awaitable<void> co_main(config cfg)
67{
68 auto ex = co_await asio::this_coro::executor;
69 auto conn = std::make_shared<connection>(ex);
70 conn->async_run(cfg, {}, asio::consign(asio::detached, conn));
71
72 person p;
73 p.set_name("Louis");
74 p.set_id(3);
75 p.set_email("No email yet.");
76
77 request req;
78 req.push("SET", "protobuf-key", p);
79 req.push("GET", "protobuf-key");
80
81 response<ignore_t, person> resp;
82
83 // Sends the request and receives the response.
84 co_await conn->async_exec(req, resp);
85 conn->cancel();
86
87 std::cout
88 << "Name: " << std::get<1>(resp).value().name() << "\n"
89 << "Age: " << std::get<1>(resp).value().id() << "\n"
90 << "Email: " << std::get<1>(resp).value().email() << "\n";
91}
92
93#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
boost::redis::connection
A basic_connection that type erases the executor.
Definition connection.hpp:1231
boost::redis::request
Creates Redis requests.
Definition request.hpp:46
boost::redis::ignore_t
std::decay_t< decltype(std::ignore)> ignore_t
Type used to ignore responses.
Definition ignore.hpp:31
boost::redis::resp3::node
basic_node< std::string > node
A node in the response tree that owns its data.
Definition node.hpp:60
boost::redis::operation
operation
Connection operations that can be cancelled.
Definition operation.hpp:18
boost::redis::response
std::tuple< adapter::result< Ts >... > response
Response with compile-time size.
Definition response.hpp:25
boost::redis::error::invalid_data_type
@ invalid_data_type
Invalid RESP3 type.
boost::redis::config
Configure parameters used by the connection classes.
Definition config.hpp:30
boost::redis::resp3::basic_node
A node in the response tree.
Definition node.hpp:28