mirror of
https://github.com/boostorg/redis.git
synced 2026-01-19 04:42:09 +00:00
Merge pull request #112 from boostorg/111-simplify-the-serialization-examples
111 simplify the serialization examples
This commit is contained in:
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@@ -67,8 +67,8 @@ jobs:
|
||||
platform_version: ${{env.BOOST_PLATFORM_VERSION}}
|
||||
arch: null
|
||||
|
||||
- name: Install packages
|
||||
run: cinst openssl
|
||||
- name: Install openssl
|
||||
run: choco install openssl
|
||||
|
||||
- name: Create build directory
|
||||
run: mkdir build
|
||||
|
||||
@@ -111,10 +111,11 @@ set(examples_cpp20
|
||||
cpp20_streams
|
||||
cpp20_containers
|
||||
cpp20_echo_server
|
||||
cpp20_resolve_with_sentinel
|
||||
cpp20_json
|
||||
cpp20_subscriber
|
||||
cpp20_intro_tls)
|
||||
cpp20_intro_tls
|
||||
cpp20_resolve_with_sentinel
|
||||
)
|
||||
|
||||
if (Protobuf_FOUND)
|
||||
list(APPEND examples_cpp20 cpp20_protobuf)
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
#include <boost/redis/connection.hpp>
|
||||
#include <boost/asio/deferred.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/co_spawn.hpp>
|
||||
#include <boost/describe.hpp>
|
||||
#include <boost/asio/consign.hpp>
|
||||
#include <boost/asio/use_awaitable.hpp>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
@@ -17,14 +17,16 @@
|
||||
|
||||
#define BOOST_JSON_NO_LIB
|
||||
#define BOOST_CONTAINER_NO_LIB
|
||||
#include "json.hpp"
|
||||
#include <boost/json/serialize.hpp>
|
||||
#include <boost/json/parse.hpp>
|
||||
#include <boost/json/value_from.hpp>
|
||||
#include <boost/redis/resp3/serialization.hpp>
|
||||
#include <boost/json/src.hpp>
|
||||
|
||||
namespace net = boost::asio;
|
||||
using namespace boost::describe;
|
||||
using boost::redis::request;
|
||||
using boost::redis::response;
|
||||
using boost::redis::operation;
|
||||
using boost::redis::ignore_t;
|
||||
using boost::redis::config;
|
||||
using connection = net::deferred_t::as_default_on_t<boost::redis::connection>;
|
||||
@@ -41,10 +43,10 @@ BOOST_DESCRIBE_STRUCT(user, (), (name, age, country))
|
||||
|
||||
// Boost.Redis customization points (examples/json.hpp)
|
||||
void boost_redis_to_bulk(std::string& to, user const& u)
|
||||
{ boost::redis::json::to_bulk(to, u); }
|
||||
{ boost::redis::resp3::boost_redis_to_bulk(to, boost::json::serialize(boost::json::value_from(u))); }
|
||||
|
||||
void boost_redis_from_bulk(user& u, std::string_view sv, boost::system::error_code& ec)
|
||||
{ boost::redis::json::from_bulk(u, sv, ec); }
|
||||
void boost_redis_from_bulk(user& u, std::string_view sv, boost::system::error_code&)
|
||||
{ u = boost::json::value_to<user>(boost::json::parse(sv)); }
|
||||
|
||||
auto co_main(config cfg) -> net::awaitable<void>
|
||||
{
|
||||
|
||||
@@ -5,12 +5,13 @@
|
||||
*/
|
||||
|
||||
#include <boost/redis/connection.hpp>
|
||||
#include <boost/redis/resp3/serialization.hpp>
|
||||
#include <boost/asio/deferred.hpp>
|
||||
#include <boost/asio/co_spawn.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/consign.hpp>
|
||||
#include <boost/system/errc.hpp>
|
||||
#include <iostream>
|
||||
#include "protobuf.hpp"
|
||||
|
||||
// See the definition in person.proto. This header is automatically
|
||||
// generated by CMakeLists.txt.
|
||||
@@ -33,11 +34,24 @@ using tutorial::person;
|
||||
namespace tutorial
|
||||
{
|
||||
|
||||
// Below I am using a Boost.Redis to indicate a protobuf error, this
|
||||
// is ok for an example, users however might want to define their own
|
||||
// error codes.
|
||||
void boost_redis_to_bulk(std::string& to, person const& u)
|
||||
{ boost::redis::protobuf::to_bulk(to, u); }
|
||||
{
|
||||
std::string tmp;
|
||||
if (!u.SerializeToString(&tmp))
|
||||
throw boost::system::system_error(boost::redis::error::invalid_data_type);
|
||||
|
||||
boost::redis::resp3::boost_redis_to_bulk(to, tmp);
|
||||
}
|
||||
|
||||
void boost_redis_from_bulk(person& u, std::string_view sv, boost::system::error_code& ec)
|
||||
{ boost::redis::protobuf::from_bulk(u, sv, ec); }
|
||||
{
|
||||
std::string const tmp {sv};
|
||||
if (!u.ParseFromString(tmp))
|
||||
ec = boost::redis::error::invalid_data_type;
|
||||
}
|
||||
|
||||
} // tutorial
|
||||
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com)
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See
|
||||
* accompanying file LICENSE.txt)
|
||||
*/
|
||||
|
||||
#ifndef BOOST_REDIS_JSON_HPP
|
||||
#define BOOST_REDIS_JSON_HPP
|
||||
|
||||
#include <boost/json/serialize.hpp>
|
||||
#include <boost/json/parse.hpp>
|
||||
#include <boost/json/value_from.hpp>
|
||||
#include <boost/redis/resp3/serialization.hpp>
|
||||
|
||||
namespace boost::redis::json
|
||||
{
|
||||
|
||||
template <class T>
|
||||
void to_bulk(std::string& to, T const& u)
|
||||
{
|
||||
redis::resp3::boost_redis_to_bulk(to, boost::json::serialize(boost::json::value_from(u)));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void from_bulk(T& u, std::string_view sv, system::error_code&)
|
||||
{
|
||||
auto const jv = boost::json::parse(sv);
|
||||
u = boost::json::value_to<T>(jv);
|
||||
}
|
||||
|
||||
} // boost::redis::json
|
||||
|
||||
#endif // BOOST_REDIS_JSON_HPP
|
||||
@@ -1,40 +0,0 @@
|
||||
/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com)
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See
|
||||
* accompanying file LICENSE.txt)
|
||||
*/
|
||||
|
||||
#ifndef BOOST_REDIS_PROTOBUF_HPP
|
||||
#define BOOST_REDIS_PROTOBUF_HPP
|
||||
|
||||
#include <boost/redis/resp3/serialization.hpp>
|
||||
#include <boost/system/errc.hpp>
|
||||
|
||||
namespace boost::redis::protobuf
|
||||
{
|
||||
|
||||
// Below I am using a Boost.Redis to indicate a protobuf error, this
|
||||
// is ok for an example, users however might want to define their own
|
||||
// error codes.
|
||||
|
||||
template <class T>
|
||||
void to_bulk(std::string& to, T const& u)
|
||||
{
|
||||
std::string tmp;
|
||||
if (!u.SerializeToString(&tmp))
|
||||
throw system::system_error(redis::error::invalid_data_type);
|
||||
|
||||
boost::redis::resp3::boost_redis_to_bulk(to, tmp);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void from_bulk(T& u, std::string_view sv, system::error_code& ec)
|
||||
{
|
||||
std::string const tmp {sv};
|
||||
if (!u.ParseFromString(tmp))
|
||||
ec = redis::error::invalid_data_type;
|
||||
}
|
||||
|
||||
} // boost::redis::json
|
||||
|
||||
#endif // BOOST_REDIS_PROTOBUF_HPP
|
||||
Reference in New Issue
Block a user