From ca6ebbd1f063503cb12eae6cb4946db7f1591c6c Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 21 Apr 2023 11:18:32 +0800 Subject: [PATCH] cql3, db: sstable: specialize fmt::formatter this is a part of a series to migrating from `operator<<(ostream&, ..)` based formatting to fmtlib based formatting. the goal here is to enable fmtlib to print `function_name` without the help of `operator<<`. the corresponding `operator<<()` are dropped dropped in this change, as all its callers are now using fmtlib for formatting now. Refs #13245 Signed-off-by: Kefu Chai Closes #13608 --- cql3/functions/abstract_function.hh | 5 ++--- cql3/functions/aggregate_fcts.cc | 2 +- cql3/functions/functions.cc | 2 +- db/functions/function.cc | 2 +- db/functions/function_name.hh | 25 +++++++++++++------------ 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cql3/functions/abstract_function.hh b/cql3/functions/abstract_function.hh index 1c72f80942..acccad942c 100644 --- a/cql3/functions/abstract_function.hh +++ b/cql3/functions/abstract_function.hh @@ -72,9 +72,8 @@ public: inline void abstract_function::print(std::ostream& os) const { - os << _name << " : ("; - os << _arg_types; - os << ") -> " << _return_type->as_cql3_type().to_string(); + fmt::print(os, "{} : ({}) -> {}", + _name, _arg_types, _return_type->as_cql3_type().to_string()); } } diff --git a/cql3/functions/aggregate_fcts.cc b/cql3/functions/aggregate_fcts.cc index 6b9917e564..f9e08fb93e 100644 --- a/cql3/functions/aggregate_fcts.cc +++ b/cql3/functions/aggregate_fcts.cc @@ -85,7 +85,7 @@ public: } virtual void print(std::ostream& os) const override { - os << _name; + fmt::print(os, "{}", _name); } virtual sstring column_name(const std::vector& column_names) const override { diff --git a/cql3/functions/functions.cc b/cql3/functions/functions.cc index 0b52cd2245..8f3e1eaab5 100644 --- a/cql3/functions/functions.cc +++ b/cql3/functions/functions.cc @@ -204,7 +204,7 @@ std::optional functions::used_by_user_function(const ut_name& use lw_shared_ptr functions::make_arg_spec(const sstring& receiver_ks, const sstring& receiver_cf, const function& fun, size_t i) { - auto&& name = boost::lexical_cast(fun.name()); + auto&& name = fmt::to_string(fun.name()); std::transform(name.begin(), name.end(), name.begin(), ::tolower); return make_lw_shared(receiver_ks, receiver_cf, diff --git a/db/functions/function.cc b/db/functions/function.cc index 62a1e6f62f..c33989cecf 100644 --- a/db/functions/function.cc +++ b/db/functions/function.cc @@ -79,7 +79,7 @@ aggregate_function::is_aggregate() const { void aggregate_function::print(std::ostream& os) const { - os << name(); + fmt::print(os, "{}", name()); } sstring diff --git a/db/functions/function_name.hh b/db/functions/function_name.hh index b9df29fcea..3acc8d80a6 100644 --- a/db/functions/function_name.hh +++ b/db/functions/function_name.hh @@ -48,19 +48,20 @@ public: } }; -inline -std::ostream& operator<<(std::ostream& os, const function_name& fn) { - if (!fn.keyspace.empty()) { - os << fn.keyspace << "."; +} +} + +template <> +struct fmt::formatter : fmt::formatter { + template + auto format(const db::functions::function_name& fn, FormatContext& ctx) const { + auto out = ctx.out(); + if (fn.has_keyspace()) { + out = fmt::format_to(out, "{}.", fn.keyspace); + } + return fmt::format_to(out, "{}", fn.name); } - return os << fn.name; -} - -} -} - - - +}; namespace std {