Skip to content
Snippets Groups Projects
Unverified Commit 03394020 authored by Michael Emiel Gevaert's avatar Michael Emiel Gevaert Committed by GitHub
Browse files

Fix toJSON incorrect empty, node_id and compound problems (#289)

parent c04ed235
No related branches found
No related tags found
No related merge requests found
......@@ -28,7 +28,9 @@ using json = nlohmann::json;
void replace_trailing_coma(std::string& s, char c) {
s.pop_back();
if (s.back() == ',') {
s.pop_back();
}
s.push_back(c);
}
......@@ -119,10 +121,18 @@ class NodeSets
std::string toJSON() const {
std::string ret{"{\n"};
for (const auto& pair : node_sets_) {
if (pair.second->is_compound()) {
ret += fmt::format(" {},\n", pair.second->toJSON());
} else {
std::string contents = pair.second->toJSON();
if (contents.empty()) {
continue;
}
ret += fmt::format(R"( "{}": {{)", pair.first);
ret += pair.second->toJSON();
ret += contents;
ret += "},\n";
}
}
replace_trailing_coma(ret, '\n');
ret += "}";
......@@ -221,7 +231,7 @@ class NodeSetBasicNodeIds: public NodeSetRule
}
std::string toJSON() const final {
return toString("node_ids", values_);
return toString("node_id", values_);
}
std::unique_ptr<NodeSetRule> clone() const final {
......@@ -254,7 +264,11 @@ class NodeSetBasicMultiClause: public NodeSetRule
std::string toJSON() const final {
std::string ret;
for (const auto& clause : clauses_) {
ret += clause->toJSON();
std::string contents = clause->toJSON();
if (contents.empty()) {
continue;
}
ret += contents;
ret += ", ";
}
replace_trailing_coma(ret, ' ');
......@@ -422,7 +436,7 @@ class NodeSetCompoundRule: public NodeSetRule
}
std::string toJSON() const final {
return toString("node_ids", targets_);
return toString(name_, targets_);
}
bool is_compound() const override {
......
......@@ -293,6 +293,7 @@ TEST_CASE("NodeSet") {
})";
SECTION("toJSON") {
{
NodeSets ns0(node_sets);
std::string j = ns0.toJSON();
NodeSets ns1(j);
......@@ -302,6 +303,20 @@ TEST_CASE("NodeSet") {
CHECK(ns.toJSON() == ns1.toJSON());
}
{
NodeSets ns0(R"({"AND": {"node_id": [], "mtype": "L6_Y"}})");
CHECK(ns0.toJSON() == "{\n \"AND\": {\"mtype\": [\"L6_Y\"] }\n}");
}
{
NodeSets ns0(R"({"NAME": {"node_id": []}})");
CHECK(ns0.toJSON() == "{\n}");
NodeSets ns1(R"({})");
CHECK(ns1.toJSON() == "{\n}");
}
}
SECTION("names") {
NodeSets ns(node_sets);
std::set<std::string> expected = {"bio_layer45", "V1_point_prime", "combined", "power_number_test", "power_regex_test"};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment