mutation: specialize fmt::formatter<partition_region>

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 `partition_region` with the help of fmt::ostream.

to help with the review process, the corresponding `to_string()` is
dropped, and its callers now switch over to `fmt::to_string()` in
this change as well. to use `fmt::to_string()` helps with consolidating
all places to use fmtlib for printing/formatting.

Refs #13245

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
This commit is contained in:
Kefu Chai
2023-03-31 17:20:57 +08:00
parent 207dcbb8fa
commit 500eeeb12c
3 changed files with 21 additions and 13 deletions

View File

@@ -3621,7 +3621,7 @@ static rjson::value encode_paging_state(const schema& schema, const service::pag
// We conditionally include these fields when reading CQL tables through alternator.
if (!is_alternator_keyspace(schema.ks_name()) && (!pos.has_key() || pos.get_bound_weight() != bound_weight::equal)) {
rjson::add_with_string_name(last_evaluated_key, scylla_paging_region, rjson::empty_object());
rjson::add(last_evaluated_key[scylla_paging_region.data()], "S", rjson::from_string(to_string(pos.region())));
rjson::add(last_evaluated_key[scylla_paging_region.data()], "S", rjson::from_string(fmt::to_string(pos.region())));
rjson::add_with_string_name(last_evaluated_key, scylla_paging_weight, rjson::empty_object());
rjson::add(last_evaluated_key[scylla_paging_weight.data()], "N", static_cast<int>(pos.get_bound_weight()));
}

View File

@@ -38,16 +38,6 @@ operator<<(std::ostream& os, const partition_end& eop) {
return os << "{partition_end}";
}
std::string_view to_string(partition_region r) {
switch (r) {
case partition_region::partition_start: return "partition_start";
case partition_region::static_row: return "static_row";
case partition_region::clustered: return "clustered";
case partition_region::partition_end: return "partition_end";
}
std::abort(); // compiler will error before we reach here
}
partition_region parse_partition_region(std::string_view s) {
if (s == "partition_start") {
return partition_region::partition_start;
@@ -63,7 +53,8 @@ partition_region parse_partition_region(std::string_view s) {
}
std::ostream& operator<<(std::ostream& out, partition_region r) {
return out << to_string(r);
fmt::print(out, "{}", r);
return out;
}
std::ostream& operator<<(std::ostream& os, position_in_partition_view::printer p) {

View File

@@ -82,8 +82,25 @@ enum class partition_region : uint8_t {
struct view_and_holder;
template <>
struct fmt::formatter<partition_region> : fmt::formatter<std::string_view> {
template <typename FormatContext>
auto format(const ::partition_region& r, FormatContext& ctx) const {
switch (r) {
case partition_region::partition_start:
return formatter<std::string_view>::format("partition_start", ctx);
case partition_region::static_row:
return formatter<std::string_view>::format("static_row", ctx);
case partition_region::clustered:
return formatter<std::string_view>::format("clustered", ctx);
case partition_region::partition_end:
return formatter<std::string_view>::format("partition_end", ctx);
}
std::abort(); // compiler will error before we reach here
}
};
std::ostream& operator<<(std::ostream&, partition_region);
std::string_view to_string(partition_region);
partition_region parse_partition_region(std::string_view);
class position_in_partition_view {