Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json_formatter: Make formatter::write work for std::pair #2440

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/seastar/json/formatter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ class formatter {
static future<> write(output_stream<char>& stream, state s, Iter i, Iter e) {
return do_with(true, [&stream, s, i, e] (bool& first) {
return stream.write(begin(s)).then([&first, &stream, s, i, e] {
return do_for_each(i, e, [&first, &stream] (auto& m) {
return do_for_each(i, e, [&first, &stream, s] (auto& m) {
auto f = (first) ? make_ready_future<>() : stream.write(",");
first = false;
return f.then([&m, &stream] {
return write(stream, m);
return f.then([&m, &stream, s] {
return write(stream, s, m);
});
}).then([&stream, s] {
return stream.write(end(s));
Expand All @@ -120,7 +120,7 @@ class formatter {
// fallback template
template<typename T>
static future<> write(output_stream<char>& stream, state, const T& t) {
return stream.write(to_json(t));
return write(stream, t);
}

public:
Expand Down
64 changes: 54 additions & 10 deletions tests/unit/json_formatter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,15 @@ SEASTAR_TEST_CASE(test_jsonable) {
return make_ready_future();
}

SEASTAR_THREAD_TEST_CASE(test_stream_range_as_array) {
template<typename F>
void formatter_check_expected(sstring expected, F f, bool close = true) {
auto vec = std::vector<net::packet>{};
auto out = output_stream<char>(data_sink(std::make_unique<vector_data_sink>(vec)), 8);

auto mapper = stream_range_as_array(std::vector<int>{1,2,3}, [] (auto i) {
object_json obj;
obj.subject = std::to_string(i);
obj.values.push(i);
return obj;
});

mapper(std::move(out)).get();
f(out);
if (close) {
out.close().get();
}

auto packets = net::packet{};
for (auto &p : vec) {
Expand All @@ -112,6 +109,53 @@ SEASTAR_THREAD_TEST_CASE(test_stream_range_as_array) {
auto buf = packets.release();

sstring result(buf.front().get(), buf.front().size());
sstring expected = "[{\"subject\":\"1\",\"values\":[1]}, {\"subject\":\"2\",\"values\":[2]}, {\"subject\":\"3\",\"values\":[3]}]";
BOOST_CHECK_EQUAL(expected, result);
}


SEASTAR_THREAD_TEST_CASE(test_stream_range_as_array) {
sstring expected = R"([{"subject":"1","values":[1]}, {"subject":"2","values":[2]}, {"subject":"3","values":[3]}])";
formatter_check_expected(expected, [] (auto& out) {
auto mapper = stream_range_as_array(std::vector<int>{1,2,3}, [] (auto i) {
object_json obj;
obj.subject = std::to_string(i);
obj.values.push(i);
return obj;
});

mapper(std::move(out)).get();
}, false);
}

SEASTAR_THREAD_TEST_CASE(formatter_write) {

formatter_check_expected("3", [] (auto &out) {
json::formatter::write(out, 3).get();
});
formatter_check_expected("false", [] (auto &out) {
json::formatter::write(out, false).get();
});
formatter_check_expected("\"foo\"", [] (auto &out) {
json::formatter::write(out, "foo").get();
});

formatter_check_expected("{1:2,3:4}", [] (auto& out) {
json::formatter::write(out, std::map<int, int>({{1, 2}, {3, 4}})).get();
});
formatter_check_expected("{3:4,1:2}", [] (auto& out) {
json::formatter::write(out, std::unordered_map<int, int>({{1, 2}, {3, 4}})).get();
});
formatter_check_expected("[1,2,3,4]", [] (auto &out) {
json::formatter::write(out, std::vector<int>({1, 2, 3, 4})).get();
});

formatter_check_expected("[{1:2},{3:4}]", [] (auto &out) {
json::formatter::write(out, std::vector<std::pair<int, int>>({{1, 2}, {3, 4}})).get();
});
formatter_check_expected("[{1:2},{3:4}]", [] (auto &out) {
json::formatter::write(out, std::vector<std::map<int, int>>({{{1, 2}}, {{3, 4}}})).get();
});
formatter_check_expected("[[1,2],[3,4]]", [] (auto &out) {
json::formatter::write(out, std::vector<std::vector<int>>({{1, 2}, {3, 4}})).get();
});
}