Skip to content

Commit 13f2ef5

Browse files
committed
Rewrite to make the logic clearer
1 parent 7195315 commit 13f2ef5

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

collector/lib/CollectorConfig.cpp

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -544,38 +544,54 @@ std::pair<option::ArgStatus, std::string> CollectorConfig::CheckConfiguration(co
544544
return std::make_pair(ARG_OK, "");
545545
}
546546

547-
CollectorConfig::ExternalIPsConfig::ExternalIPsConfig(std::optional<sensor::CollectorConfig> runtime_config, bool default_enabled)
548-
: direction_enabled_(Direction::NONE) {
549-
if (runtime_config.has_value()) {
550-
if (runtime_config.value().networking().external_ips().enabled() == sensor::ExternalIpsEnabled::ENABLED) {
551-
switch (runtime_config.value().networking().external_ips().direction()) {
552-
case sensor::ExternalIpsDirection::INGRESS:
553-
direction_enabled_ = Direction::INGRESS;
554-
break;
555-
case sensor::ExternalIpsDirection::EGRESS:
556-
direction_enabled_ = Direction::EGRESS;
557-
break;
558-
default:
559-
direction_enabled_ = Direction::BOTH;
560-
break;
561-
}
562-
}
563-
} else if (default_enabled) {
564-
direction_enabled_ = Direction::BOTH;
547+
CollectorConfig::ExternalIPsConfig::ExternalIPsConfig(std::optional<sensor::CollectorConfig> runtime_config, bool default_enabled) {
548+
if (!runtime_config.has_value()) {
549+
direction_enabled_ = default_enabled ? Direction::BOTH : Direction::NONE;
550+
return;
551+
}
552+
553+
// At this point we know runtime_config has a value, we can access it directly
554+
const auto& external_ips = runtime_config->networking().external_ips();
555+
if (external_ips.enabled() != sensor::ExternalIpsEnabled::ENABLED) {
556+
direction_enabled_ = Direction::NONE;
557+
return;
558+
}
559+
560+
switch (external_ips.direction()) {
561+
case sensor::ExternalIpsDirection::INGRESS:
562+
direction_enabled_ = Direction::INGRESS;
563+
break;
564+
case sensor::ExternalIpsDirection::EGRESS:
565+
direction_enabled_ = Direction::EGRESS;
566+
break;
567+
default:
568+
direction_enabled_ = Direction::BOTH;
569+
break;
565570
}
566571
}
567572

568573
std::ostream& operator<<(std::ostream& os, const collector::CollectorConfig::ExternalIPsConfig& config) {
569-
static std::unordered_map<collector::CollectorConfig::ExternalIPsConfig::Direction, std::string> mapping = {
570-
{CollectorConfig::ExternalIPsConfig::Direction::NONE, "NONE"},
571-
{CollectorConfig::ExternalIPsConfig::Direction::INGRESS, "INGRESS"},
572-
{CollectorConfig::ExternalIPsConfig::Direction::EGRESS, "EGRESS"},
573-
{CollectorConfig::ExternalIPsConfig::Direction::BOTH, "BOTH"},
574-
};
575-
576-
auto str = mapping.find(config.GetDirection());
574+
os << "direction(";
575+
576+
switch (config.GetDirection()) {
577+
case CollectorConfig::ExternalIPsConfig::Direction::NONE:
578+
os << "NONE";
579+
break;
580+
case CollectorConfig::ExternalIPsConfig::Direction::INGRESS:
581+
os << "INGRESS";
582+
break;
583+
case CollectorConfig::ExternalIPsConfig::Direction::EGRESS:
584+
os << "EGRESS";
585+
break;
586+
case CollectorConfig::ExternalIPsConfig::Direction::BOTH:
587+
os << "BOTH";
588+
break;
589+
default:
590+
os << "invalid";
591+
break;
592+
}
577593

578-
return os << "direction(" << ((str == mapping.end()) ? "invalid" : (*str).second) << ")";
594+
return os << ")";
579595
}
580596

581597
} // namespace collector

0 commit comments

Comments
 (0)