Skip to content

Commit

Permalink
Remove obsolete code
Browse files Browse the repository at this point in the history
  • Loading branch information
Neverlord committed Feb 19, 2024
1 parent 2568111 commit 7883f0a
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 157 deletions.
3 changes: 0 additions & 3 deletions include/broker/internal/json.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ namespace broker::internal {
/// Bundles utility functions for JSON handling.
class json {
public:
/// Converts @p msg to a JSON object.
static void apply(const data_message& msg, caf::json_writer& out);

/// Converts a JSON object that represents a @ref data_message to a binary
/// representation.
static error data_message_to_binary(const caf::json_object& obj,
Expand Down
143 changes: 0 additions & 143 deletions src/internal/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,156 +9,13 @@

#include <caf/json_array.hpp>
#include <caf/json_object.hpp>
#include <caf/json_reader.hpp>
#include <caf/json_value.hpp>
#include <caf/json_writer.hpp>
#include <caf/type_id.hpp>

namespace broker::internal {

namespace {

struct checker {};

constexpr checker check;

const checker& operator<<(const checker& check, bool value) {
if (!value)
throw std::logic_error("failed to generate JSON");
return check;
}

bool do_apply(const variant& val, caf::json_writer& out);

bool do_apply(const variant_list& ls, caf::json_writer& out) {
check << out.begin_sequence(ls.size());
for (auto val : ls) {
check << out.begin_object(caf::type_id_v<data>, "data") //
<< do_apply(val, out) //
<< out.end_object();
}
check << out.end_sequence();
return true;
}

bool do_apply(const variant_set& ls, caf::json_writer& out) {
check << out.begin_sequence(ls.size());
for (auto val : ls) {
check << out.begin_object(caf::type_id_v<data>, "data") //
<< do_apply(val, out) //
<< out.end_object();
}
check << out.end_sequence();
return true;
}

bool do_apply(const variant_table& tbl, caf::json_writer& out) {
using namespace std::literals;
check << out.begin_sequence(tbl.size());
for (const auto& [key, val] : tbl) {
check << out.begin_associative_array(2) //
<< out.begin_key_value_pair() //
<< out.value("key"sv) //
<< out.begin_object(caf::type_id_v<data>, "data") //
<< do_apply(key, out) //
<< out.end_object() //
<< out.end_key_value_pair() //
<< out.begin_key_value_pair() //
<< out.value("value"sv) //
<< out.begin_object(caf::type_id_v<data>, "data") //
<< do_apply(val, out) //
<< out.end_object() //
<< out.end_key_value_pair() //
<< out.end_associative_array();
}
check << out.end_sequence();
return true;
}

bool do_apply(const variant& val, caf::json_writer& out) {
auto caf_print = [](auto val) {
std::string result;
caf::detail::print(result, val);
return result;
};
check << out.begin_field("@data-type") //
<< out.value(detail::json_type_name(val.get_tag())) //
<< out.end_field() //
<< out.begin_field("data");
switch (val.get_tag()) {
default: // none
check << out.begin_object(caf::type_id_v<none>, "none")
<< out.end_object();
break;
case variant_tag::boolean:
check << out.value(val.to_boolean());
break;
case variant_tag::count:
check << out.value(val.to_count());
break;
case variant_tag::integer:
check << out.value(val.to_integer());
break;
case variant_tag::real:
check << out.value(val.to_real());
break;
case variant_tag::string:
check << out.value(val.to_string());
break;
case variant_tag::address:
check << out.value(broker::to_string(val.to_address()));
break;
case variant_tag::subnet:
check << out.value(broker::to_string(val.to_subnet()));
break;
case variant_tag::port:
check << out.value(broker::to_string(val.to_port()));
break;
case variant_tag::timestamp:
check << out.value(caf_print(val.to_timestamp()));
break;
case variant_tag::timespan:
check << out.value(caf_print(val.to_timespan()));
break;
case variant_tag::enum_value:
check << out.value(val.to_enum_value().name);
break;
case variant_tag::set:
do_apply(val.to_set(), out);
break;
case variant_tag::table:
do_apply(val.to_table(), out);
break;
case variant_tag::list:
do_apply(val.to_list(), out);
break;
}
check << out.end_field();
return true;
}

bool do_apply(const data_message& msg, caf::json_writer& out) {
using namespace std::literals;
check << out.begin_object(caf::type_id_v<data_message>, "data-message") //
<< out.begin_field("type") //
<< out.value("data-message"sv) //
<< out.end_field() //
<< out.begin_field("topic") //
<< out.value(msg->topic()) //
<< out.end_field() //
<< do_apply(msg->value(), out) //
<< out.end_object();
return true;
}

} // namespace

void json::apply(const data_message& msg, caf::json_writer& out) {
do_apply(msg, out);
}

namespace {

template <class OutIter>
bool to_binary_impl(const caf::json_object& obj, OutIter& out) {
namespace bin_v1 = format::bin::v1;
Expand Down
11 changes: 0 additions & 11 deletions tests/cpp/internal/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <caf/json_object.hpp>
#include <caf/json_value.hpp>
#include <caf/json_writer.hpp>

using namespace broker;

Expand Down Expand Up @@ -163,13 +162,3 @@ TEST(a data message in JSON can be rewritten to the binary format) {
REQUIRE(maybe_msg);
CHECK_EQ((*maybe_msg)->value().to_data(), native()->value().to_data());
}

TEST(data messages can be applied to a JSON writer) {
using util = broker::internal::json;
caf::json_writer writer;
writer.skip_object_type_annotation(true);
writer.indentation(2);
auto msg = native();
util::apply(msg, writer);
CHECK_EQ(writer.str(), json);
}

0 comments on commit 7883f0a

Please sign in to comment.