Added auto serialization for Bind and Roster
All checks were successful
PR Check / on-push-commit-check (push) Successful in 13m5s
All checks were successful
PR Check / on-push-commit-check (push) Successful in 13m5s
This commit is contained in:
parent
30a5e69d14
commit
495b58489e
5 changed files with 54 additions and 39 deletions
|
@ -97,7 +97,7 @@ struct Client {
|
||||||
|
|
||||||
auto UpdateListOfContacts() -> boost::asio::awaitable<void> {
|
auto UpdateListOfContacts() -> boost::asio::awaitable<void> {
|
||||||
SPDLOG_INFO("Send IQ: Get::Roster");
|
SPDLOG_INFO("Send IQ: Get::Roster");
|
||||||
co_await this->Send(::iq::GetRoster{.id = "1", .from = std::format("{}@{}", "invalid_user", jid.server), .payload = {}});
|
co_await this->Send(::iq::GetRoster{.id = "1", .from = jid, .payload = {}});
|
||||||
|
|
||||||
const auto get_roster_response = co_await connection.template Read<Iq<::iq::Roster>>();
|
const auto get_roster_response = co_await connection.template Read<Iq<::iq::Roster>>();
|
||||||
std::visit(utempl::Overloaded(
|
std::visit(utempl::Overloaded(
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include <larra/jid.hpp>
|
||||||
#include <larra/serialization.hpp>
|
#include <larra/serialization.hpp>
|
||||||
#include <larra/stream_error.hpp>
|
#include <larra/stream_error.hpp>
|
||||||
#include <larra/utils.hpp>
|
#include <larra/utils.hpp>
|
||||||
|
@ -12,8 +13,8 @@ namespace iq {
|
||||||
template <auto& Name, typename PayloadType>
|
template <auto& Name, typename PayloadType>
|
||||||
struct BaseImplWithPayload {
|
struct BaseImplWithPayload {
|
||||||
std::string id;
|
std::string id;
|
||||||
std::optional<std::string> from{};
|
std::optional<Jid> from{};
|
||||||
std::optional<std::string> to{};
|
std::optional<Jid> to{};
|
||||||
PayloadType payload;
|
PayloadType payload;
|
||||||
static const inline std::string kName = Name;
|
static const inline std::string kName = Name;
|
||||||
static constexpr auto kDefaultName = "iq";
|
static constexpr auto kDefaultName = "iq";
|
||||||
|
@ -38,10 +39,10 @@ struct BaseImplWithPayload {
|
||||||
element->set_attribute("id", self.id);
|
element->set_attribute("id", self.id);
|
||||||
|
|
||||||
if(self.to) {
|
if(self.to) {
|
||||||
element->set_attribute("to", *self.to);
|
element->set_attribute("to", ToString(*self.to));
|
||||||
}
|
}
|
||||||
if(self.from) {
|
if(self.from) {
|
||||||
element->set_attribute("from", *self.from);
|
element->set_attribute("from", ToString(*self.from));
|
||||||
}
|
}
|
||||||
element->set_attribute("type", kName);
|
element->set_attribute("type", kName);
|
||||||
using S = Serialization<PayloadType>;
|
using S = Serialization<PayloadType>;
|
||||||
|
@ -86,8 +87,8 @@ struct BaseImplWithPayload {
|
||||||
throw std::runtime_error("Invalid payload for parse Iq");
|
throw std::runtime_error("Invalid payload for parse Iq");
|
||||||
}
|
}
|
||||||
return {.id = idNode->get_value(),
|
return {.id = idNode->get_value(),
|
||||||
.from = (from ? std::optional{from->get_value()} : std::nullopt),
|
.from = (from ? std::optional{BareJid::Parse(from->get_value())} : std::nullopt),
|
||||||
.to = (to ? std::optional{to->get_value()} : std::nullopt),
|
.to = (to ? std::optional{BareJid::Parse(to->get_value())} : std::nullopt),
|
||||||
.payload = S::Parse(payload2)};
|
.payload = S::Parse(payload2)};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include <larra/iq.hpp>
|
#include <larra/iq.hpp>
|
||||||
#include <larra/jid.hpp>
|
#include <larra/jid.hpp>
|
||||||
|
#include <larra/serialization/auto.hpp>
|
||||||
#include <larra/utils.hpp>
|
#include <larra/utils.hpp>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -11,18 +12,28 @@
|
||||||
|
|
||||||
namespace larra::xmpp::iq {
|
namespace larra::xmpp::iq {
|
||||||
|
|
||||||
|
struct RosterItem {
|
||||||
|
BareJid jid;
|
||||||
|
friend constexpr auto ToString(const RosterItem& item) {
|
||||||
|
return ToString(item.jid);
|
||||||
|
}
|
||||||
|
constexpr auto operator==(const RosterItem&) const -> bool = default;
|
||||||
|
friend constexpr auto operator<<(xmlpp::Element* element, const RosterItem& item);
|
||||||
|
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> RosterItem;
|
||||||
|
};
|
||||||
|
|
||||||
struct Roster {
|
struct Roster {
|
||||||
static constexpr auto kDefaultName = "query";
|
static constexpr auto kDefaultName = "query";
|
||||||
static constexpr auto kDefaultNamespace = "jabber:iq:roster";
|
static constexpr auto kDefaultNamespace = "jabber:iq:roster";
|
||||||
|
|
||||||
std::vector<BareJid> items;
|
std::vector<RosterItem> items;
|
||||||
|
|
||||||
friend auto ToString(const Roster& roster) -> std::string {
|
friend auto ToString(const Roster& roster) -> std::string {
|
||||||
static constexpr std::string_view prefix = "Roster: [\n\t";
|
static constexpr std::string_view prefix = "Roster: [\n\t";
|
||||||
static constexpr std::string_view suffix = "]";
|
static constexpr std::string_view suffix = "]";
|
||||||
// \n\r\t
|
// \n\r\t
|
||||||
std::size_t total_length = std::ranges::fold_left(roster.items | std::views::transform([](const auto& el) {
|
std::size_t total_length = std::ranges::fold_left(roster.items | std::views::transform([](const auto& el) {
|
||||||
return larra::xmpp::utils::AccumulateFieldLength(el) + 3;
|
return larra::xmpp::utils::AccumulateFieldLength(el.jid) + 3;
|
||||||
}),
|
}),
|
||||||
prefix.length() + suffix.length(),
|
prefix.length() + suffix.length(),
|
||||||
std::plus<>{});
|
std::plus<>{});
|
||||||
|
@ -36,33 +47,8 @@ struct Roster {
|
||||||
}
|
}
|
||||||
return s += suffix;
|
return s += suffix;
|
||||||
}
|
}
|
||||||
friend constexpr auto operator<<(xmlpp::Element* element, const Roster& roster) {
|
friend constexpr auto operator<<(xmlpp::Element* element, const Roster& roster);
|
||||||
element->set_attribute("xmlns", Roster::kDefaultNamespace);
|
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Roster;
|
||||||
for(const auto& item : roster.items) {
|
|
||||||
element->add_child_element("item")->set_attribute("jid", ToString(item));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Roster {
|
|
||||||
const auto& item_nodes = element->get_children("item");
|
|
||||||
if(item_nodes.empty()) {
|
|
||||||
SPDLOG_DEBUG("No items at Iq::Roster");
|
|
||||||
}
|
|
||||||
|
|
||||||
return {.items = item_nodes | std::views::transform([](const xmlpp::Node* node) {
|
|
||||||
auto item_element = dynamic_cast<const xmlpp::Element*>(node);
|
|
||||||
if(!item_element) {
|
|
||||||
throw std::runtime_error("Can't convert xmlpp::Node to xmlpp::Element");
|
|
||||||
}
|
|
||||||
|
|
||||||
auto jid_ptr = item_element->get_attribute("jid");
|
|
||||||
if(!jid_ptr) {
|
|
||||||
throw std::runtime_error("Not found attribute 'jid' for parse Roster item");
|
|
||||||
}
|
|
||||||
|
|
||||||
return BareJid::Parse(jid_ptr->get_value());
|
|
||||||
}) |
|
|
||||||
std::ranges::to<std::vector<BareJid>>()};
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
using GetRoster = Get<Roster>;
|
using GetRoster = Get<Roster>;
|
||||||
|
@ -70,3 +56,31 @@ using ResultRoster = Result<Roster>;
|
||||||
using IqRoster = Iq<Roster>;
|
using IqRoster = Iq<Roster>;
|
||||||
|
|
||||||
} // namespace larra::xmpp::iq
|
} // namespace larra::xmpp::iq
|
||||||
|
|
||||||
|
namespace larra::xmpp::serialization {
|
||||||
|
namespace iq = larra::xmpp::iq;
|
||||||
|
|
||||||
|
template <>
|
||||||
|
constexpr auto kSerializationConfig<iq::RosterItem> = SerializationConfig<iq::RosterItem>{};
|
||||||
|
template <>
|
||||||
|
constexpr auto kSerializationConfig<iq::Roster> = SerializationConfig<iq::Roster>{}.With<"items">({Config<std::vector<iq::RosterItem>>{}});
|
||||||
|
} // namespace larra::xmpp::serialization
|
||||||
|
|
||||||
|
namespace larra::xmpp::iq {
|
||||||
|
namespace S = larra::xmpp::serialization;
|
||||||
|
|
||||||
|
constexpr auto operator<<(xmlpp::Element* element, const RosterItem& self) {
|
||||||
|
S::Serialize(element, self);
|
||||||
|
}
|
||||||
|
constexpr auto RosterItem::Parse(xmlpp::Element* element) -> RosterItem {
|
||||||
|
return S::Parse<RosterItem>(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr auto operator<<(xmlpp::Element* element, const Roster& self) {
|
||||||
|
element->set_attribute("xmlns", Roster::kDefaultNamespace);
|
||||||
|
S::Serialize(element, self);
|
||||||
|
}
|
||||||
|
constexpr auto Roster::Parse(xmlpp::Element* element) -> Roster {
|
||||||
|
return S::Parse<Roster>(element);
|
||||||
|
}
|
||||||
|
} // namespace larra::xmpp::iq
|
||||||
|
|
|
@ -115,7 +115,7 @@ struct Config : V {
|
||||||
requires(!HasParse<T>)
|
requires(!HasParse<T>)
|
||||||
: V(AttributeConfig{}) {
|
: V(AttributeConfig{}) {
|
||||||
}
|
}
|
||||||
constexpr auto Base() const -> const V& {
|
[[nodiscard]] constexpr auto Base() const -> const V& {
|
||||||
return static_cast<const V&>(*this);
|
return static_cast<const V&>(*this);
|
||||||
}
|
}
|
||||||
using type = T;
|
using type = T;
|
||||||
|
|
|
@ -7,7 +7,7 @@ namespace larra::xmpp {
|
||||||
|
|
||||||
TEST(Roster, SerializeAndParse) {
|
TEST(Roster, SerializeAndParse) {
|
||||||
FullJid jid{.username = "test", .server = "server", .resource = "res"}; // NOLINT
|
FullJid jid{.username = "test", .server = "server", .resource = "res"}; // NOLINT
|
||||||
auto roster = iq::GetRoster{.id = "1", .from = ToString(jid), .payload = iq::Roster{.items = {{"u1", "s1"}, {"u2", "s2"}, {"u3", "s3"}}}};
|
auto roster = iq::GetRoster{.id = "1", .from = jid, .payload = iq::Roster{.items = {{"u1", "s1"}, {"u2", "s2"}, {"u3", "s3"}}}};
|
||||||
|
|
||||||
xmlpp::Document doc;
|
xmlpp::Document doc;
|
||||||
auto node = doc.create_root_node("iq");
|
auto node = doc.create_root_node("iq");
|
||||||
|
@ -24,7 +24,7 @@ TEST(Roster, SerializeAndParse) {
|
||||||
static constexpr std::string_view kRosterPrintExpectedData = "Roster: [\n\tu1@s1\n\tu2@s2\n\tu3@s3\n\t]";
|
static constexpr std::string_view kRosterPrintExpectedData = "Roster: [\n\tu1@s1\n\tu2@s2\n\tu3@s3\n\t]";
|
||||||
TEST(Roster, Print) {
|
TEST(Roster, Print) {
|
||||||
FullJid jid{.username = "test", .server = "server", .resource = "res"}; // NOLINT
|
FullJid jid{.username = "test", .server = "server", .resource = "res"}; // NOLINT
|
||||||
auto roster = iq::GetRoster{.id = "1", .from = ToString(jid), .payload = iq::Roster{.items = {{"u1", "s1"}, {"u2", "s2"}, {"u3", "s3"}}}};
|
auto roster = iq::GetRoster{.id = "1", .from = jid, .payload = iq::Roster{.items = {{"u1", "s1"}, {"u2", "s2"}, {"u3", "s3"}}}};
|
||||||
|
|
||||||
EXPECT_NO_THROW({
|
EXPECT_NO_THROW({
|
||||||
auto roster_str = ToString(roster.payload);
|
auto roster_str = ToString(roster.payload);
|
||||||
|
|
Loading…
Reference in a new issue