Skip to content

Commit

Permalink
[metric][fix]to_json for empty vector (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 3, 2024
1 parent 40f6cf7 commit c902933
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions include/ylt/metric/metric_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ class manager_helper {
#ifdef CINATRA_ENABLE_METRIC_JSON
static std::string serialize_to_json(
const std::vector<std::shared_ptr<metric_t>>& metrics) {
if (metrics.empty()) {
return "";
}
std::string str;
str.append("[");
for (auto& m : metrics) {
Expand All @@ -50,7 +47,10 @@ class manager_helper {
}

if (str.size() == 1) {
return "";
str.append("]");
}
else {
str.back() = ']';
}

str.back() = ']';
Expand Down
4 changes: 2 additions & 2 deletions src/metric/tests/test_metric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1155,12 +1155,12 @@ TEST_CASE("test filter metrics static") {
#ifdef CINATRA_ENABLE_METRIC_JSON
std::vector<std::shared_ptr<metric_t>> vec{};
auto s = manager_helper::serialize_to_json(vec);
CHECK(s.empty());
CHECK(s == "[]");
auto c = std::make_shared<counter_t>(std::string("get_count"),
std::string("get counter"));
vec.push_back(c);
s = manager_helper::serialize_to_json(vec);
CHECK(s.empty());
CHECK(s == "[]");
#endif
}

Expand Down

0 comments on commit c902933

Please sign in to comment.