Skip to content

Commit

Permalink
Add metadata/fields separation
Browse files Browse the repository at this point in the history
  • Loading branch information
L3nn0x committed Dec 23, 2020
1 parent 0258b28 commit 974243d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
13 changes: 8 additions & 5 deletions generator/src/codegen/codegen_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,13 @@ impl<'a, W: Write> CodeSourceGenerator<'a, W> {
}

fn packet_to_json(&mut self, packet: &Packet) -> Result<()> {
cg!(self, "void to_json(nlohmann::json& j, const {}& data) {{", packet.class_name());
cg!(self, "void RoseCommon::Packet::to_json(nlohmann::json& j, const {}& data) {{", packet.class_name());
self.indent();
cg!(self, "j = nlohmann::json{{");
self.indent();
cg!(self, "{{ \"packet\", \"{}\" }},", packet.type_());
cg!(self, "{{ \"size\", data.get_size() }},");
cg!(self, "{{ \"metadata\", {{ {{ \"packet\", \"{}\" }}, {{ \"size\", data.get_size() }} }} }},", packet.type_());
cg!(self, "{{ \"fields\", {{");
self.indent();
for content in packet.contents() {
use self::PacketContent::*;
match content {
Expand All @@ -323,6 +324,8 @@ impl<'a, W: Write> CodeSourceGenerator<'a, W> {
}
}
self.dedent();
cg!(self, "}} }}");
self.dedent();
cg!(self, "}};");
self.dedent();
cg!(self, "}}");
Expand All @@ -334,7 +337,7 @@ impl<'a, W: Write> CodeSourceGenerator<'a, W> {
}

fn simple_type_to_json(&mut self, packet_name:&str, element: &SimpleType) -> Result<()> {
cg!(self, "void to_json(nlohmann::json& j, const {}::{}& data) {{", packet_name, element.name());
cg!(self, "void RoseCommon::Packet::to_json(nlohmann::json& j, const {}::{}& data) {{", packet_name, element.name());
self.indent();
cg!(self, "j = nlohmann::json{{");
self.indent();
Expand Down Expand Up @@ -369,7 +372,7 @@ impl<'a, W: Write> CodeSourceGenerator<'a, W> {
if element.inline() == true {
return Ok(());
}
cg!(self, "void to_json(nlohmann::json& j, const {}::{}& data) {{", packet_name, element.name());
cg!(self, "void RoseCommon::Packet::to_json(nlohmann::json& j, const {}::{}& data) {{", packet_name, element.name());
self.indent();
use ::flat_ast::ComplexTypeContent::*;
cg!(self, "j = nlohmann::json{{");
Expand Down
49 changes: 25 additions & 24 deletions srv_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,46 +385,47 @@ constexpr size_t SrvTest::size() {
}


void to_json(nlohmann::json& j, const Aaa& data) {
j = json{
void RoseCommon::Packet::to_json(nlohmann::json& j, const SrvTest::Aaa& data) {
j = nlohmann::json{
{ "value", static_cast<uint8_t>(data) },
};
}
void to_json(nlohmann::json& j, const Bbb& data) {
j = json{
void RoseCommon::Packet::to_json(nlohmann::json& j, const SrvTest::Bbb& data) {
j = nlohmann::json{
{ "value", data.operator std::string() },
};
}

void to_json(nlohmann::json& j, const Pote& data) {
j = json{
void RoseCommon::Packet::to_json(nlohmann::json& j, const SrvTest::Pote& data) {
j = nlohmann::json{
{ "a", data.get_a() },
{ "b", data.get_b() },
{ "c", data.get_c() },
};
}
void to_json(nlohmann::json& j, const Pote2& data) {
j = json{
void RoseCommon::Packet::to_json(nlohmann::json& j, const SrvTest::Pote2& data) {
j = nlohmann::json{
{ "a", data.get_a() == 1 },
{ "b", data.get_b() == 1 },
};
}
void to_json(nlohmann::json& j, const SrvTest& data) {
j = json{
{ "packet", "PAKWC_TEST" },
{ "size", data.get_size() },
{ "a", data.get_a() == 1 },
{ "b", data.get_b() == 1 },
{ "c", data.get_c() },
{ "d", data.get_d() == 1 },
{ "e", data.get_e() == 1 },
{ "f", data.get_f() == 1 },
{ "g", data.get_g() == 1 },
{ "h", data.get_h() == 1 },
{ "pote", data.get_pote() },
{ "pote2", data.get_pote2() },
{ "x", data.get_x() },
{ "y", data.get_y() },
void RoseCommon::Packet::to_json(nlohmann::json& j, const SrvTest& data) {
j = nlohmann::json{
{ "metadata", { "packet", "PAKWC_TEST" }, { "size", data.get_size() } },
{ "fields", {
{ "a", data.get_a() == 1 },
{ "b", data.get_b() == 1 },
{ "c", data.get_c() },
{ "d", data.get_d() == 1 },
{ "e", data.get_e() == 1 },
{ "f", data.get_f() == 1 },
{ "g", data.get_g() == 1 },
{ "h", data.get_h() == 1 },
{ "pote", data.get_pote() },
{ "pote2", data.get_pote2() },
{ "x", data.get_x() },
{ "y", data.get_y() },
} }
};
}

8 changes: 4 additions & 4 deletions srv_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ class SrvTest : public CRosePacket {
Bbb y;
};

void to_json(nlohmann::json& j, const Aaa& data);
void to_json(nlohmann::json& j, const Bbb& data);
void to_json(nlohmann::json& j, const SrvTest::Aaa& data);
void to_json(nlohmann::json& j, const SrvTest::Bbb& data);

void to_json(nlohmann::json& j, const Pote& data);
void to_json(nlohmann::json& j, const Pote2& data);
void to_json(nlohmann::json& j, const SrvTest::Pote& data);
void to_json(nlohmann::json& j, const SrvTest::Pote2& data);
void to_json(nlohmann::json& j, const SrvTest& data);

}
Expand Down

0 comments on commit 974243d

Please sign in to comment.