Skip to content

Commit

Permalink
Merge pull request #112 from boostorg/111-simplify-the-serialization-…
Browse files Browse the repository at this point in the history
…examples

111 simplify the serialization examples
  • Loading branch information
mzimbres authored Jun 11, 2023
2 parents a8a78c3 + c99790a commit 607946f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 86 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions examples/cpp20_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@
#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>

#if defined(BOOST_ASIO_HAS_CO_AWAIT)

#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>;
Expand All @@ -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>
{
Expand Down
20 changes: 17 additions & 3 deletions examples/cpp20_protobuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down
33 changes: 0 additions & 33 deletions examples/json.hpp

This file was deleted.

40 changes: 0 additions & 40 deletions examples/protobuf.hpp

This file was deleted.

0 comments on commit 607946f

Please sign in to comment.