Skip to content

Commit

Permalink
Merge pull request #4821 from felixguendling/topic_namespace_serde_ex…
Browse files Browse the repository at this point in the history
…ample

serde serialization for topic_namespace
  • Loading branch information
mmaslankaprv committed May 20, 2022
2 parents a596ebd + 32f5217 commit 4e86751
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/v/model/fundamental.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

#pragma once

#include "bytes/iobuf.h"
#include "seastarx.h"
#include "serde/serde.h"
#include "ssx/sformat.h"
#include "utils/named_type.h"
#include "vassert.h"
Expand Down Expand Up @@ -89,6 +91,17 @@ class topic : public named_type<ss::sstring, struct model_topic_type> {
topic(model::topic_view view) // NOLINT - see topic_view_tests.cc
: named_type<ss::sstring, struct model_topic_type>(ss::sstring(view())) {}

friend void
read_nested(iobuf_parser& in, topic& t, size_t const bytes_left_limit) {
using serde::read_nested;
return read_nested(in, t._value, bytes_left_limit);
}

friend void write(iobuf& out, topic t) {
using serde::write;
return write(out, std::move(t._value));
}

operator topic_view() { return topic_view(_value); }

operator topic_view() const { return topic_view(_value); }
Expand Down
18 changes: 18 additions & 0 deletions src/v/model/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "model/fundamental.h"
#include "net/unresolved_address.h"
#include "seastarx.h"
#include "serde/envelope.h"
#include "utils/named_type.h"

#include <seastar/core/sstring.hh>
Expand Down Expand Up @@ -245,6 +246,8 @@ struct topic_namespace_view {
};

struct topic_namespace {
topic_namespace() = default;

topic_namespace(model::ns n, model::topic t)
: ns(std::move(n))
, tp(std::move(t)) {}
Expand All @@ -271,6 +274,21 @@ struct topic_namespace {
return H::combine(std::move(h), tp_ns.ns, tp_ns.tp);
}

friend void write(iobuf& out, topic_namespace t) {
using serde::write;
write(out, std::move(t.ns));
write(out, std::move(t.tp));
}

friend void read_nested(
iobuf_parser& in,
topic_namespace& t,
std::size_t const bytes_left_limit) {
using serde::read_nested;
read_nested(in, t.ns, bytes_left_limit);
read_nested(in, t.tp, bytes_left_limit);
}

model::ns ns;
model::topic tp;

Expand Down
13 changes: 13 additions & 0 deletions src/v/serde/test/serde_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "hashing/crc32c.h"
#include "model/fundamental.h"
#include "model/metadata.h"
#include "serde/envelope.h"
#include "serde/serde.h"
#include "utils/fragmented_vector.h"
Expand Down Expand Up @@ -695,3 +696,15 @@ SEASTAR_THREAD_TEST_CASE(fragmented_vector_test) {
v_out.begin(), v_out.end(), v_in_copy.begin(), v_in_copy.end());
}
}

SEASTAR_THREAD_TEST_CASE(serde_topic_namespace_test) {
iobuf b;
{
model::topic_namespace tn{model::ns{"abc"}, model::topic{"def"}};
b = serde::to_iobuf(std::move(tn));
}

auto tn = serde::from_iobuf<model::topic_namespace>(std::move(b));
BOOST_CHECK_EQUAL(tn.ns, "abc");
BOOST_CHECK_EQUAL(tn.tp, "def");
}

0 comments on commit 4e86751

Please sign in to comment.