Skip to content

Commit

Permalink
Merge 'auth: replace operator<<(..) with fmt formatter' from Kefu Chai
Browse files Browse the repository at this point in the history
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 `auth::auth_authentication_options` and `auth::resource_kind`
without the help of fmt::ostream. and their `operator<<(ostream,..)` are
dropped, as there are no users of them anymore.

Refs #13245

Closes #13460

* github.com:scylladb/scylladb:
  auth: remove unused operator<<(.., resource_kind)
  auth: specialize fmt::formatter<resource_kind>
  auth: remove unused operator<<(.., authentication_option)
  auth: specialize fmt::formatter<authentication_option>
  • Loading branch information
xemul committed Apr 10, 2023
2 parents 64a87f4 + 9d5fbe2 commit fd817e1
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 41 deletions.
1 change: 0 additions & 1 deletion auth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ target_sources(scylla_auth
allow_all_authenticator.cc
allow_all_authorizer.cc
authenticated_user.cc
authentication_options.cc
authenticator.cc
common.cc
default_authorizer.cc
Expand Down
24 changes: 0 additions & 24 deletions auth/authentication_options.cc

This file was deleted.

17 changes: 15 additions & 2 deletions auth/authentication_options.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ enum class authentication_option {
options
};

std::ostream& operator<<(std::ostream&, authentication_option);

using authentication_option_set = std::unordered_set<authentication_option>;

using custom_options = std::unordered_map<sstring, sstring>;
Expand All @@ -49,3 +47,18 @@ public:
};

}

template <>
struct fmt::formatter<auth::authentication_option> : fmt::formatter<std::string_view> {
template <typename FormatContext>
auto format(const auth::authentication_option a, FormatContext& ctx) const {
using enum auth::authentication_option;
switch (a) {
case password:
return formatter<std::string_view>::format("PASSWORD", ctx);
case options:
return formatter<std::string_view>::format("OPTIONS", ctx);
}
std::abort();
}
};
11 changes: 0 additions & 11 deletions auth/resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@

namespace auth {

std::ostream& operator<<(std::ostream& os, resource_kind kind) {
switch (kind) {
case resource_kind::data: os << "data"; break;
case resource_kind::role: os << "role"; break;
case resource_kind::service_level: os << "service_level"; break;
case resource_kind::functions: os << "functions"; break;
}

return os;
}

static const std::unordered_map<resource_kind, std::string_view> roots{
{resource_kind::data, "data"},
{resource_kind::role, "roles"},
Expand Down
21 changes: 19 additions & 2 deletions auth/resource.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ enum class resource_kind {
data, role, service_level, functions
};

std::ostream& operator<<(std::ostream&, resource_kind);

///
/// Type tag for constructing data resources.
///
Expand Down Expand Up @@ -266,6 +264,25 @@ std::pair<sstring, std::vector<data_type>> decode_signature(std::string_view enc

}

template <>
struct fmt::formatter<auth::resource_kind> : fmt::formatter<std::string_view> {
template <typename FormatContext>
auto format(const auth::resource_kind kind, FormatContext& ctx) const {
using enum auth::resource_kind;
switch (kind) {
case data:
return formatter<std::string_view>::format("data", ctx);
case role:
return formatter<std::string_view>::format("role", ctx);
case service_level:
return formatter<std::string_view>::format("service_level", ctx);
case functions:
return formatter<std::string_view>::format("functions", ctx);
}
std::abort();
}
};

namespace std {

template <>
Expand Down
1 change: 0 additions & 1 deletion configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,6 @@ def find_headers(repodir, excluded_dirs):
'auth/service.cc',
'auth/standard_role_manager.cc',
'auth/transitional.cc',
'auth/authentication_options.cc',
'auth/role_or_anonymous.cc',
'auth/sasl_challenge.cc',
'tracing/tracing.cc',
Expand Down
9 changes: 9 additions & 0 deletions test/boost/auth_resource_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@

#include "utils/to_string.hh"

namespace auth {

std::ostream& operator<<(std::ostream& os, resource_kind kind) {
fmt::print(os, "{}", kind);
return os;
}

}

BOOST_AUTO_TEST_CASE(root_of) {
//
// data
Expand Down

0 comments on commit fd817e1

Please sign in to comment.