From f344e1306618f388056056e5cc61e4bb2a796137 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sun, 14 Jan 2024 12:32:11 +0800 Subject: [PATCH] types: add formatter for data_value before this change, we rely on the default-generated fmt::formatter created from operator<<, but fmt v10 dropped the default-generated formatter. in this change, we define a formatter for data_value, but its its operator<<() is preserved as we are still using the generic homebrew formatter for formatting std::vector, which in turn uses operator<< of the element type. Refs #13245 Signed-off-by: Kefu Chai Closes scylladb/scylladb#16767 --- types/types.cc | 12 +++++++++--- types/types.hh | 9 ++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/types/types.cc b/types/types.cc index edad9a623b..dad399f904 100644 --- a/types/types.cc +++ b/types/types.cc @@ -3679,11 +3679,17 @@ make_user_value(data_type type, user_type_impl::native_type value) { return data_value::make_new(std::move(type), std::move(value)); } -std::ostream& operator<<(std::ostream& out, const data_value& v) { + auto fmt::formatter::format(const data_value& v, + fmt::format_context& ctx) const -> decltype(ctx.out()) { if (v.is_null()) { - return out << "null"; + return fmt::format_to(ctx.out(), "null"); } - return out << v.type()->to_string_impl(v); + return fmt::format_to(ctx.out(), "{}", v.type()->to_string_impl(v)); +} + +std::ostream& operator<<(std::ostream& out, const data_value& v) { + fmt::print(out, "{}", v); + return out; } shared_ptr reversed_type_impl::get_instance(data_type type) { diff --git a/types/types.hh b/types/types.hh index 5336151876..a80997507e 100644 --- a/types/types.hh +++ b/types/types.hh @@ -269,7 +269,6 @@ public: friend class empty_type_impl; template friend const T& value_cast(const data_value&); template friend T&& value_cast(data_value&&); - friend std::ostream& operator<<(std::ostream&, const data_value&); friend data_value make_tuple_value(data_type, maybe_empty>); friend data_value make_set_value(data_type, maybe_empty>); friend data_value make_list_value(data_type, maybe_empty>); @@ -1034,4 +1033,12 @@ struct fmt::formatter : fmt::formatter { } }; +template <> struct fmt::formatter { + constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); } + auto format(const data_value&, fmt::format_context& ctx) const -> decltype(ctx.out()); +}; + +std::ostream& operator<<(std::ostream& out, const data_value& v); + using data_value_list = std::initializer_list; +