Skip to content

Commit

Permalink
hints: host_filter: add formatter for hints::host_filter
Browse files Browse the repository at this point in the history
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 formatters for `hints::host_filter`. its
operator<< is preserved as it's still used by the homebrew generic
formatter for vector<>, which is in turn used by db/config.cc.

Refs scylladb#13245

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>

Closes scylladb#17347
  • Loading branch information
tchaikov authored and xemul committed Feb 16, 2024
1 parent e132ffd commit 50964c4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
24 changes: 15 additions & 9 deletions db/hints/host_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ host_filter host_filter::parse_from_dc_list(sstring opt) {
return host_filter(std::unordered_set<sstring>(dcs.begin(), dcs.end()));
}

std::ostream& operator<<(std::ostream& os, const host_filter& f) {
fmt::print(os, "{}", f);
return os;
}

std::istream& operator>>(std::istream& is, host_filter& f) {
sstring tmp;
is >> tmp;
Expand Down Expand Up @@ -100,16 +105,17 @@ std::string_view host_filter::enabled_kind_to_string(host_filter::enabled_kind e
throw std::logic_error("Uncovered variant of enabled_kind");
}

std::ostream& operator<<(std::ostream& os, const host_filter& f) {
fmt::print(os, "host_filter{{enabled_kind={}",
}
}

auto fmt::formatter<db::hints::host_filter>::format(const db::hints::host_filter& f, fmt::format_context& ctx) const
-> decltype(ctx.out()) {
using db::hints::host_filter;
auto out = ctx.out();
out = fmt::format_to(out, "host_filter{{enabled_kind={}",
host_filter::enabled_kind_to_string(f._enabled_kind));
if (f._enabled_kind == host_filter::enabled_kind::enabled_selectively) {
fmt::print(os, ", dcs={{{}}}", fmt::join(f._dcs, ","));
out = fmt::format_to(out, ", dcs={{{}}}", fmt::join(f._dcs, ","));
}
fmt::print(os, "}}");
return os;
}

}
return fmt::format_to(out, "}}");
}

8 changes: 7 additions & 1 deletion db/hints/host_filter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ public:

sstring to_configuration_string() const;

friend std::ostream& operator<<(std::ostream& os, const host_filter& f);
friend fmt::formatter<host_filter>;
};

std::ostream& operator<<(std::ostream& os, const host_filter& f);
std::istream& operator>>(std::istream& is, host_filter& f);

class hints_configuration_parse_error : public std::runtime_error {
Expand All @@ -91,3 +92,8 @@ public:

}
}

template <>
struct fmt::formatter<db::hints::host_filter> : fmt::formatter<std::string_view> {
auto format(const db::hints::host_filter&, fmt::format_context& ctx) const -> decltype(ctx.out());
};

0 comments on commit 50964c4

Please sign in to comment.