Skip to content

Commit

Permalink
First steps with using adapters to process a generic_response.
Browse files Browse the repository at this point in the history
  • Loading branch information
mzimbres committed Sep 4, 2023
1 parent 44a608c commit 4547e1a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
36 changes: 24 additions & 12 deletions include/boost/redis/adapter/detail/adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ class general_aggregate {

public:
explicit general_aggregate(Result* c = nullptr): result_(c) {}
void operator()(resp3::basic_node<std::string_view> const& nd, system::error_code&)
template <class String>
void operator()(resp3::basic_node<String> const& nd, system::error_code&)
{
BOOST_ASSERT_MSG(!!result_, "Unexpected null pointer");
switch (nd.data_type) {
Expand All @@ -114,7 +115,8 @@ class general_simple {
public:
explicit general_simple(Node* t = nullptr) : result_(t) {}

void operator()(resp3::basic_node<std::string_view> const& nd, system::error_code&)
template <class String>
void operator()(resp3::basic_node<String> const& nd, system::error_code&)
{
BOOST_ASSERT_MSG(!!result_, "Unexpected null pointer");
switch (nd.data_type) {
Expand All @@ -136,10 +138,11 @@ class simple_impl {
public:
void on_value_available(Result&) {}

template <class String>
void
operator()(
Result& result,
resp3::basic_node<std::string_view> const& n,
resp3::basic_node<String> const& n,
system::error_code& ec)
{
if (is_aggregate(n.data_type)) {
Expand All @@ -160,10 +163,11 @@ class set_impl {
void on_value_available(Result& result)
{ hint_ = std::end(result); }

template <class String>
void
operator()(
Result& result,
resp3::basic_node<std::string_view> const& nd,
resp3::basic_node<String> const& nd,
system::error_code& ec)
{
if (is_aggregate(nd.data_type)) {
Expand Down Expand Up @@ -195,10 +199,11 @@ class map_impl {
void on_value_available(Result& result)
{ current_ = std::end(result); }

template <class String>
void
operator()(
Result& result,
resp3::basic_node<std::string_view> const& nd,
resp3::basic_node<String> const& nd,
system::error_code& ec)
{
if (is_aggregate(nd.data_type)) {
Expand Down Expand Up @@ -233,10 +238,11 @@ class vector_impl {
public:
void on_value_available(Result& ) { }

template <class String>
void
operator()(
Result& result,
resp3::basic_node<std::string_view> const& nd,
resp3::basic_node<String> const& nd,
system::error_code& ec)
{
if (is_aggregate(nd.data_type)) {
Expand All @@ -257,10 +263,11 @@ class array_impl {
public:
void on_value_available(Result& ) { }

template <class String>
void
operator()(
Result& result,
resp3::basic_node<std::string_view> const& nd,
resp3::basic_node<String> const& nd,
system::error_code& ec)
{
if (is_aggregate(nd.data_type)) {
Expand Down Expand Up @@ -292,10 +299,11 @@ struct list_impl {

void on_value_available(Result& ) { }

template <class String>
void
operator()(
Result& result,
resp3::basic_node<std::string_view> const& nd,
resp3::basic_node<String> const& nd,
system::error_code& ec)
{
if (!is_aggregate(nd.data_type)) {
Expand Down Expand Up @@ -365,7 +373,8 @@ class wrapper<result<Result>> {
response_type* result_;
typename impl_map<Result>::type impl_;

bool set_if_resp3_error(resp3::basic_node<std::string_view> const& nd) noexcept
template <class String>
bool set_if_resp3_error(resp3::basic_node<String> const& nd) noexcept
{
switch (nd.data_type) {
case resp3::type::null:
Expand All @@ -387,9 +396,10 @@ class wrapper<result<Result>> {
}
}

template <class String>
void
operator()(
resp3::basic_node<std::string_view> const& nd,
resp3::basic_node<String> const& nd,
system::error_code& ec)
{
BOOST_ASSERT_MSG(!!result_, "Unexpected null pointer");
Expand All @@ -414,7 +424,8 @@ class wrapper<result<std::optional<T>>> {
response_type* result_;
typename impl_map<T>::type impl_{};

bool set_if_resp3_error(resp3::basic_node<std::string_view> const& nd) noexcept
template <class String>
bool set_if_resp3_error(resp3::basic_node<String> const& nd) noexcept
{
switch (nd.data_type) {
case resp3::type::blob_error:
Expand All @@ -429,9 +440,10 @@ class wrapper<result<std::optional<T>>> {
public:
explicit wrapper(response_type* o = nullptr) : result_(o) {}

template <class String>
void
operator()(
resp3::basic_node<std::string_view> const& nd,
resp3::basic_node<String> const& nd,
system::error_code& ec)
{
BOOST_ASSERT_MSG(!!result_, "Unexpected null pointer");
Expand Down
12 changes: 8 additions & 4 deletions include/boost/redis/adapter/detail/response_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ namespace boost::redis::adapter::detail

class ignore_adapter {
public:
template <class String>
void
operator()(std::size_t, resp3::basic_node<std::string_view> const& nd, system::error_code& ec)
operator()(std::size_t, resp3::basic_node<String> const& nd, system::error_code& ec)
{
switch (nd.data_type) {
case resp3::type::simple_error: ec = redis::error::resp3_simple_error; break;
Expand Down Expand Up @@ -59,10 +60,11 @@ class static_adapter {
auto get_supported_response_size() const noexcept
{ return size;}

template <class String>
void
operator()(
std::size_t i,
resp3::basic_node<std::string_view> const& nd,
resp3::basic_node<String> const& nd,
system::error_code& ec)
{
using std::visit;
Expand All @@ -88,10 +90,11 @@ class vector_adapter {
get_supported_response_size() const noexcept
{ return static_cast<std::size_t>(-1);}

template <class String>
void
operator()(
std::size_t,
resp3::basic_node<std::string_view> const& nd,
resp3::basic_node<String> const& nd,
system::error_code& ec)
{
adapter_(nd, ec);
Expand Down Expand Up @@ -142,7 +145,8 @@ class wrapper {
public:
explicit wrapper(Adapter adapter) : adapter_{adapter} {}

void operator()(resp3::basic_node<std::string_view> const& nd, system::error_code& ec)
template <class String>
void operator()(resp3::basic_node<String> const& nd, system::error_code& ec)
{ return adapter_(0, nd, ec); }

[[nodiscard]]
Expand Down
6 changes: 4 additions & 2 deletions include/boost/redis/adapter/detail/result_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ class static_aggregate_adapter<result<Tuple>> {
}
}

void count(resp3::basic_node<std::string_view> const& nd)
template <class String>
void count(resp3::basic_node<String> const& nd)
{
if (nd.depth == 1) {
if (is_aggregate(nd.data_type))
Expand All @@ -131,7 +132,8 @@ class static_aggregate_adapter<result<Tuple>> {
++i_;
}

void operator()(resp3::basic_node<std::string_view> const& nd, system::error_code& ec)
template <class String>
void operator()(resp3::basic_node<String> const& nd, system::error_code& ec)
{
using std::visit;

Expand Down
13 changes: 13 additions & 0 deletions tests/test_low_level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,3 +589,16 @@ BOOST_AUTO_TEST_CASE(adapter)
BOOST_CHECK_EQUAL(std::get<1>(resp).value(), 42);
BOOST_TEST(!ec);
}

// TODO: This was an experiment, I will resume implementing this
// later.
BOOST_AUTO_TEST_CASE(adapter_as)
{
result<std::set<std::string>> set;
auto adapter = adapt2(set);

for (auto const& e: set_expected1a.value()) {
error_code ec;
adapter(e, ec);
}
}

0 comments on commit 4547e1a

Please sign in to comment.