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

Conversation

StephanDollberg
Copy link
Contributor

@StephanDollberg StephanDollberg commented Sep 17, 2024

Previously the un/associative container overloads for formatter::write
were broken because it failed to find an overload for
write(output_stream, pair).

To fix this we make the write(output_stream<char>&, state, Iter, Iter)
overload actually write(output_stream, state, pair) so that the
existing overload that handles pair can be found.

Further we fix the fallback write(output_stream, state, T) overload to
call formatter::write recursively with the state stripped instead of
calling to_json. This keeps the recursive zero-copy nature of
formatter::write intact.

All of the above mirrors how the existing overloads for to_json
already work.

Adds some tests as well.

@avikivity
Copy link
Member

Does the test fail before the patch? If so please reorder them.

Previously the un/associative container overloads for `formatter::write`
were broken because it failed to find an overload for
`write(output_stream, pair)`.

To fix this we make the `write(output_stream<char>&, state, Iter, Iter)`
overload actually `write(output_stream, state, pair)` so that the
existing overload that handles `pair` can be found.

Further we fix the fallback `write(output_stream, state, T)` overload to
call `formatter::write` recurisvely with the state stripped instead of
calling `to_json`. This keeps the recursive zero-copy nature of
`formatter::write` intact.

All of the above mirrors how the existing overloads for `to_json`
already work.
@StephanDollberg
Copy link
Contributor Author

Does the test fail before the patch? If so please reorder them.

Yep, it doesn't compile. Done.

Copy link
Contributor

@tchaikov tchaikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm modulo some nits.


SEASTAR_THREAD_TEST_CASE(formatter_write) {

formatter_check_expected("3", [](auto &out) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a space after []. to be consistent the tree.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

f(out);
if (close) {
out.close().get();
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could remove ;.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done



SEASTAR_THREAD_TEST_CASE(test_stream_range_as_array) {
sstring expected = "[{\"subject\":\"1\",\"values\":[1]}, {\"subject\":\"2\",\"values\":[2]}, {\"subject\":\"3\",\"values\":[3]}]";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, could use raw-string literal. like

sstring expected = R"([{"subject":"1","values":[1]},....)";

more readable this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Adds some unit tests for the various `formatter::write` overloads.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants