Skip to content

Commit 9ec1d4b

Browse files
authored
Format the code the same way as the userver (#66)
1 parent 0a209b9 commit 9ec1d4b

20 files changed

+359
-395
lines changed

.clang-format

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
BasedOnStyle: Google
2+
DerivePointerAlignment: false
3+
IncludeBlocks: Preserve
4+
AttributeMacros: ["noexcept"]
5+
IndentRequires: false
6+
ColumnLimit: 120
7+
IndentWidth: 4
8+
TabWidth: 4
9+
AccessModifierOffset: -4
10+
BinPackParameters: false
11+
BinPackArguments: false
12+
AllowAllParametersOfDeclarationOnNextLine: false
13+
AlignAfterOpenBracket: BlockIndent
14+
AlwaysBreakAfterDefinitionReturnType: None
15+
PenaltyReturnTypeOnItsOwnLine: 200

src/cache/configs_cache.cpp

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,55 @@
1010
namespace uservice_dynconf::cache::settings_cache {
1111

1212
namespace {
13-
constexpr static const char *kDefaultService = "__default__";
13+
constexpr static const char* kDefaultService = "__default__";
1414
}
1515

1616
userver::storages::postgres::Query ConfigCachePolicy::GetQuery() {
17-
return uservice_dynconf::sql::kSelectSettingsForCache;
17+
return uservice_dynconf::sql::kSelectSettingsForCache;
1818
}
1919

20-
void ConfigCacheContainer::insert_or_assign(Key &&key, Config &&config) {
21-
auto config_ptr = std::make_shared<const Config>(std::move(config));
22-
configs_to_key_.insert_or_assign(key, config_ptr);
23-
configs_by_service_[key.service].push_back(std::move(config_ptr));
20+
void ConfigCacheContainer::insert_or_assign(Key&& key, Config&& config) {
21+
auto config_ptr = std::make_shared<const Config>(std::move(config));
22+
configs_to_key_.insert_or_assign(key, config_ptr);
23+
configs_by_service_[key.service].push_back(std::move(config_ptr));
2424
}
2525

26-
std::vector<ConfigCacheContainer::ConfigPtr>
27-
ConfigCacheContainer::FindConfigsByService(std::string_view service) const {
28-
if (auto it = configs_by_service_.find(service.data());
29-
it != configs_by_service_.end()) {
30-
return it->second;
31-
}
32-
return {};
26+
std::vector<ConfigCacheContainer::ConfigPtr> ConfigCacheContainer::FindConfigsByService(std::string_view service
27+
) const {
28+
if (auto it = configs_by_service_.find(service.data()); it != configs_by_service_.end()) {
29+
return it->second;
30+
}
31+
return {};
3332
}
3433

3534
size_t ConfigCacheContainer::size() const { return configs_to_key_.size(); }
3635

37-
ConfigCacheContainer::ConfigPtr
38-
ConfigCacheContainer::FindConfig(const ConfigCacheContainer::Key &key) const {
39-
if (auto c_ptr = userver::utils::FindOrDefault(configs_to_key_, key, nullptr);
40-
c_ptr) {
41-
return c_ptr;
42-
}
43-
return userver::utils::FindOrDefault(
44-
configs_to_key_,
45-
ConfigCacheContainer::Key{{kDefaultService}, key.config_name}, nullptr);
36+
ConfigCacheContainer::ConfigPtr ConfigCacheContainer::FindConfig(const ConfigCacheContainer::Key& key) const {
37+
if (auto c_ptr = userver::utils::FindOrDefault(configs_to_key_, key, nullptr); c_ptr) {
38+
return c_ptr;
39+
}
40+
return userver::utils::FindOrDefault(
41+
configs_to_key_, ConfigCacheContainer::Key{{kDefaultService}, key.config_name}, nullptr
42+
);
4643
}
4744

4845
std::vector<ConfigCacheContainer::ConfigPtr>
49-
ConfigCacheContainer::FindConfigs(std::string_view service,
50-
const std::vector<std::string> &ids) const {
51-
std::vector<ConfigCacheContainer::ConfigPtr> result{};
52-
result.reserve(ids.size());
53-
for (const auto &id : ids) {
54-
if (auto c_ptr = userver::utils::FindOrDefault(
55-
configs_to_key_, Key{service.data(), id}, nullptr);
56-
c_ptr) {
57-
result.emplace_back(c_ptr);
58-
continue;
59-
}
60-
if (auto c_ptr = userver::utils::FindOrDefault(
61-
configs_to_key_, ConfigCacheContainer::Key{{kDefaultService}, id},
62-
nullptr);
63-
c_ptr) {
64-
result.emplace_back(c_ptr);
46+
ConfigCacheContainer::FindConfigs(std::string_view service, const std::vector<std::string>& ids) const {
47+
std::vector<ConfigCacheContainer::ConfigPtr> result{};
48+
result.reserve(ids.size());
49+
for (const auto& id : ids) {
50+
if (auto c_ptr = userver::utils::FindOrDefault(configs_to_key_, Key{service.data(), id}, nullptr); c_ptr) {
51+
result.emplace_back(c_ptr);
52+
continue;
53+
}
54+
if (auto c_ptr = userver::utils::FindOrDefault(
55+
configs_to_key_, ConfigCacheContainer::Key{{kDefaultService}, id}, nullptr
56+
);
57+
c_ptr) {
58+
result.emplace_back(c_ptr);
59+
}
6560
}
66-
}
67-
return result;
61+
return result;
6862
}
6963

70-
} // namespace uservice_dynconf::cache::settings_cache
64+
} // namespace uservice_dynconf::cache::settings_cache

src/cache/configs_cache.hpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,34 @@ namespace uservice_dynconf::cache::settings_cache {
1212

1313
class ConfigCacheContainer {
1414
public:
15-
using Key = uservice_dynconf::models::Key;
16-
using Config = uservice_dynconf::models::Config;
17-
using ConfigPtr = std::shared_ptr<const Config>;
15+
using Key = uservice_dynconf::models::Key;
16+
using Config = uservice_dynconf::models::Config;
17+
using ConfigPtr = std::shared_ptr<const Config>;
1818

19-
void insert_or_assign(Key &&key, Config &&config);
20-
size_t size() const;
19+
void insert_or_assign(Key&& key, Config&& config);
20+
size_t size() const;
2121

22-
ConfigPtr FindConfig(const Key &key) const;
23-
std::vector<ConfigPtr> FindConfigsByService(std::string_view service) const;
24-
std::vector<ConfigPtr> FindConfigs(std::string_view service,
25-
const std::vector<std::string> &ids) const;
22+
ConfigPtr FindConfig(const Key& key) const;
23+
std::vector<ConfigPtr> FindConfigsByService(std::string_view service) const;
24+
std::vector<ConfigPtr> FindConfigs(std::string_view service, const std::vector<std::string>& ids) const;
2625

27-
auto begin() const { return configs_to_key_.begin(); }
28-
auto end() const { return configs_to_key_.end(); }
26+
auto begin() const { return configs_to_key_.begin(); }
27+
auto end() const { return configs_to_key_.end(); }
2928

3029
private:
31-
std::unordered_map<Key, ConfigPtr> configs_to_key_;
32-
std::unordered_map<std::string, std::vector<ConfigPtr>> configs_by_service_;
30+
std::unordered_map<Key, ConfigPtr> configs_to_key_;
31+
std::unordered_map<std::string, std::vector<ConfigPtr>> configs_by_service_;
3332
};
3433

3534
struct ConfigCachePolicy {
36-
static userver::storages::postgres::Query GetQuery();
37-
static constexpr auto kName = "configs-cache";
38-
using ValueType = uservice_dynconf::models::Config;
39-
using CacheContainer = ConfigCacheContainer;
40-
static constexpr auto kKeyMember = &uservice_dynconf::models::Config::key;
41-
static constexpr auto kUpdatedField = "updated_at";
42-
using UpdatedFieldType = userver::storages::postgres::TimePointTz;
35+
static userver::storages::postgres::Query GetQuery();
36+
static constexpr auto kName = "configs-cache";
37+
using ValueType = uservice_dynconf::models::Config;
38+
using CacheContainer = ConfigCacheContainer;
39+
static constexpr auto kKeyMember = &uservice_dynconf::models::Config::key;
40+
static constexpr auto kUpdatedField = "updated_at";
41+
using UpdatedFieldType = userver::storages::postgres::TimePointTz;
4342
};
4443

4544
using ConfigsCache = ::userver::components::PostgreCache<ConfigCachePolicy>;
46-
} // namespace uservice_dynconf::cache::settings_cache
45+
} // namespace uservice_dynconf::cache::settings_cache

src/handlers/admin_v1_configs.cpp

Lines changed: 84 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -20,103 +20,102 @@ namespace uservice_dynconf::handlers::admin_v1_configs::post {
2020
namespace {
2121

2222
bool ConsistsOfIdsFromConfigs(
23-
const std::unordered_set<std::string> &kill_switches,
24-
const userver::formats::json::Value &configs) {
25-
for (const auto &kill_switch : kill_switches) {
26-
if (!configs.HasMember(kill_switch)) {
27-
return false;
23+
const std::unordered_set<std::string>& kill_switches,
24+
const userver::formats::json::Value& configs
25+
) {
26+
for (const auto& kill_switch : kill_switches) {
27+
if (!configs.HasMember(kill_switch)) {
28+
return false;
29+
}
2830
}
29-
}
30-
return true;
31+
return true;
3132
}
3233

33-
bool HasIntersection(const std::unordered_set<std::string> &first,
34-
const std::unordered_set<std::string> &second) {
35-
for (const auto &key : first) {
36-
if (second.count(key) > 0) {
37-
return true;
34+
bool HasIntersection(const std::unordered_set<std::string>& first, const std::unordered_set<std::string>& second) {
35+
for (const auto& key : first) {
36+
if (second.count(key) > 0) {
37+
return true;
38+
}
3839
}
39-
}
40-
return false;
40+
return false;
4141
}
4242

4343
userver::formats::json::Value MakeConfigModeMap(
44-
const userver::formats::json::Value &configs,
45-
const std::unordered_set<std::string> &kill_switches_enabled,
46-
const std::unordered_set<std::string> &kill_switches_disabled) {
47-
using Mode = uservice_dynconf::models::Mode;
48-
49-
userver::formats::json::ValueBuilder builder;
50-
for (const auto &[config_name, config_value] : Items(configs)) {
51-
Mode config_mode = Mode::kDynamicConfig;
52-
if (kill_switches_enabled.count(config_name) > 0) {
53-
config_mode = Mode::kKillSwitchEnabled;
54-
} else if (kill_switches_disabled.count(config_name) > 0) {
55-
config_mode = Mode::kKillSwitchDisabled;
44+
const userver::formats::json::Value& configs,
45+
const std::unordered_set<std::string>& kill_switches_enabled,
46+
const std::unordered_set<std::string>& kill_switches_disabled
47+
) {
48+
using Mode = uservice_dynconf::models::Mode;
49+
50+
userver::formats::json::ValueBuilder builder;
51+
for (const auto& [config_name, config_value] : Items(configs)) {
52+
Mode config_mode = Mode::kDynamicConfig;
53+
if (kill_switches_enabled.count(config_name) > 0) {
54+
config_mode = Mode::kKillSwitchEnabled;
55+
} else if (kill_switches_disabled.count(config_name) > 0) {
56+
config_mode = Mode::kKillSwitchDisabled;
57+
}
58+
builder[config_name] = uservice_dynconf::models::ToString(config_mode);
5659
}
57-
builder[config_name] = uservice_dynconf::models::ToString(config_mode);
58-
}
5960

60-
return builder.ExtractValue();
61+
return builder.ExtractValue();
6162
}
6263

63-
} // namespace
64+
} // namespace
6465

65-
Handler::Handler(const userver::components::ComponentConfig &config,
66-
const userver::components::ComponentContext &context)
66+
Handler::Handler(
67+
const userver::components::ComponentConfig& config,
68+
const userver::components::ComponentContext& context
69+
)
6770
: HttpHandlerJsonBase(config, context),
68-
cluster_(
69-
context
70-
.FindComponent<userver::components::Postgres>("settings-database")
71-
.GetCluster()) {}
72-
73-
userver::formats::json::Value Handler::HandleRequestJsonThrow(
74-
const userver::server::http::HttpRequest &request,
75-
const userver::formats::json::Value &request_json,
76-
userver::server::request::RequestContext &) const {
77-
auto &http_response = request.GetHttpResponse();
78-
auto &&request_data = request_json.As<AdminConfigsRequestBody>();
79-
80-
if (!request_data.configs.has_value() ||
81-
request_data.configs.value().extra.IsEmpty() ||
82-
!request_data.service.has_value() ||
83-
request_data.service.value().empty()) {
84-
http_response.SetStatus(userver::server::http::HttpStatus::kBadRequest);
85-
return uservice_dynconf::utils::MakeError(
86-
"400", "Fields 'configs' and 'service' are required");
87-
}
88-
89-
const auto configs = request_data.configs.value().extra;
90-
const auto kill_switches_enabled =
91-
request_data.kill_switches_enabled.value_or(
92-
std::unordered_set<std::string>{});
93-
const auto kill_switches_disabled =
94-
request_data.kill_switches_disabled.value_or(
95-
std::unordered_set<std::string>{});
96-
97-
if (!ConsistsOfIdsFromConfigs(kill_switches_enabled, configs) ||
98-
!ConsistsOfIdsFromConfigs(kill_switches_disabled, configs)) {
99-
http_response.SetStatus(userver::server::http::HttpStatus::kBadRequest);
100-
return uservice_dynconf::utils::MakeError(
101-
"400", "Fields 'kill_switches_enabled' and 'kill_switches_disabled' "
102-
"must consist of ids from 'configs' field");
103-
}
104-
if (HasIntersection(kill_switches_enabled, kill_switches_disabled)) {
105-
http_response.SetStatus(userver::server::http::HttpStatus::kBadRequest);
106-
return uservice_dynconf::utils::MakeError(
107-
"400", "Ids in 'kill_switches_enabled' and 'kill_switches_disabled' "
108-
"must not overlap");
109-
}
110-
111-
const auto config_mode_map =
112-
MakeConfigModeMap(configs, kill_switches_enabled, kill_switches_disabled);
113-
cluster_->Execute(userver::storages::postgres::ClusterHostType::kMaster,
114-
uservice_dynconf::sql::kInsertConfigValue,
115-
request_data.service.value(), configs, config_mode_map);
116-
117-
http_response.SetStatus(userver::server::http::HttpStatus::kNoContent);
118-
119-
return {};
71+
cluster_(context.FindComponent<userver::components::Postgres>("settings-database").GetCluster()) {}
72+
73+
userver::formats::json::Value Handler::
74+
HandleRequestJsonThrow(const userver::server::http::HttpRequest& request, const userver::formats::json::Value& request_json, userver::server::request::RequestContext&)
75+
const {
76+
auto& http_response = request.GetHttpResponse();
77+
auto&& request_data = request_json.As<AdminConfigsRequestBody>();
78+
79+
if (!request_data.configs.has_value() || request_data.configs.value().extra.IsEmpty() ||
80+
!request_data.service.has_value() || request_data.service.value().empty()) {
81+
http_response.SetStatus(userver::server::http::HttpStatus::kBadRequest);
82+
return uservice_dynconf::utils::MakeError("400", "Fields 'configs' and 'service' are required");
83+
}
84+
85+
const auto configs = request_data.configs.value().extra;
86+
const auto kill_switches_enabled = request_data.kill_switches_enabled.value_or(std::unordered_set<std::string>{});
87+
const auto kill_switches_disabled = request_data.kill_switches_disabled.value_or(std::unordered_set<std::string>{});
88+
89+
if (!ConsistsOfIdsFromConfigs(kill_switches_enabled, configs) ||
90+
!ConsistsOfIdsFromConfigs(kill_switches_disabled, configs)) {
91+
http_response.SetStatus(userver::server::http::HttpStatus::kBadRequest);
92+
return uservice_dynconf::utils::MakeError(
93+
"400",
94+
"Fields 'kill_switches_enabled' and 'kill_switches_disabled' "
95+
"must consist of ids from 'configs' field"
96+
);
97+
}
98+
if (HasIntersection(kill_switches_enabled, kill_switches_disabled)) {
99+
http_response.SetStatus(userver::server::http::HttpStatus::kBadRequest);
100+
return uservice_dynconf::utils::MakeError(
101+
"400",
102+
"Ids in 'kill_switches_enabled' and 'kill_switches_disabled' "
103+
"must not overlap"
104+
);
105+
}
106+
107+
const auto config_mode_map = MakeConfigModeMap(configs, kill_switches_enabled, kill_switches_disabled);
108+
cluster_->Execute(
109+
userver::storages::postgres::ClusterHostType::kMaster,
110+
uservice_dynconf::sql::kInsertConfigValue,
111+
request_data.service.value(),
112+
configs,
113+
config_mode_map
114+
);
115+
116+
http_response.SetStatus(userver::server::http::HttpStatus::kNoContent);
117+
118+
return {};
120119
}
121120

122-
} // namespace uservice_dynconf::handlers::admin_v1_configs::post
121+
} // namespace uservice_dynconf::handlers::admin_v1_configs::post

src/handlers/admin_v1_configs.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ namespace uservice_dynconf::handlers::admin_v1_configs::post {
1212

1313
class Handler final : public userver::server::handlers::HttpHandlerJsonBase {
1414
public:
15-
static constexpr std::string_view kName = "handler-admin-v1-configs";
15+
static constexpr std::string_view kName = "handler-admin-v1-configs";
1616

17-
Handler(const userver::components::ComponentConfig &config,
18-
const userver::components::ComponentContext &context);
17+
Handler(const userver::components::ComponentConfig& config, const userver::components::ComponentContext& context);
1918

20-
userver::formats::json::Value HandleRequestJsonThrow(
21-
const userver::server::http::HttpRequest &request,
22-
const userver::formats::json::Value &request_json,
23-
userver::server::request::RequestContext &context) const override final;
19+
userver::formats::json::Value HandleRequestJsonThrow(
20+
const userver::server::http::HttpRequest& request,
21+
const userver::formats::json::Value& request_json,
22+
userver::server::request::RequestContext& context
23+
) const override final;
2424

2525
private:
26-
const userver::storages::postgres::ClusterPtr cluster_;
26+
const userver::storages::postgres::ClusterPtr cluster_;
2727
};
2828

29-
} // namespace uservice_dynconf::handlers::admin_v1_configs::post
29+
} // namespace uservice_dynconf::handlers::admin_v1_configs::post

0 commit comments

Comments
 (0)