Summary
Extract a generic enum-to-string serialization template to eliminate duplicated switch/case patterns across multiple files.
Background (Why)
Multiple enum types have identical serialization patterns
Each enum has separate to_string() and from_string() functions with similar structure
Violates DRY principle and increases maintenance burden
Any change to serialization pattern requires updates in multiple locations
Scope (What)
Current State (Duplicated Pattern)
File 1 : include/kcenon/common/interfaces/monitoring/health_check.h (lines 38-80)
```cpp
inline std::string to_string(health_check_type type) {
switch (type) {
case health_check_type::liveness: return "LIVENESS";
case health_check_type::readiness: return "READINESS";
// ...
}
}
inline Result<health_check_type> health_check_type_from_string(const std::string& str) {
std::string upper = str;
std::transform(upper.begin(), upper.end(), upper.begin(),
[](unsigned char c) { return std::toupper(c); });
if (upper == "LIVENESS") return ok(health_check_type::liveness);
// ...
}
```
File 2 : include/kcenon/common/interfaces/logger_interface.h (lines 295-328)
Same pattern for log_level enum
Proposed State
```cpp
// Generic concept and template
template
concept EnumSerializable = requires {
typename enum_traits;
{ enum_traits::values } -> std::convertible_to<std::span<const std::pair<Enum, std::string_view>>>;
};
template
inline std::string to_string(Enum value) {
for (const auto& [e, s] : enum_traits::values) {
if (e == value) return std::string(s);
}
return "UNKNOWN";
}
template
inline Result from_string(std::string_view str) {
// Case-insensitive lookup using enum_traits
}
// Specialization example
template<>
struct enum_traits<health_check_type> {
static constexpr std::array values = {
std::pair{health_check_type::liveness, "LIVENESS"},
std::pair{health_check_type::readiness, "READINESS"},
// ...
};
};
```
Impact Analysis (Where)
File
Lines
Enum Type
interfaces/monitoring/health_check.h
38-80
health_check_type, health_status
interfaces/logger_interface.h
295-328
log_level
error/error_codes.h
150+
category (potential)
Estimated duplication : ~100 lines across 3+ files
Implementation Plan (How)
Create include/kcenon/common/utils/enum_serialization.h
Define EnumSerializable concept and enum_traits template
Implement generic to_string() and from_string() templates
Add specializations for existing enums
Replace existing implementations with template usage
Add unit tests for generic serialization
Update documentation
Timeline (When)
Target: Next minor release
Acceptance Criteria
Labels
refactor
code-quality
duplication
Summary
Extract a generic enum-to-string serialization template to eliminate duplicated switch/case patterns across multiple files.
Background (Why)
to_string()andfrom_string()functions with similar structureScope (What)
Current State (Duplicated Pattern)
File 1:
include/kcenon/common/interfaces/monitoring/health_check.h(lines 38-80)```cpp
inline std::string to_string(health_check_type type) {
switch (type) {
case health_check_type::liveness: return "LIVENESS";
case health_check_type::readiness: return "READINESS";
// ...
}
}
inline Result<health_check_type> health_check_type_from_string(const std::string& str) {
std::string upper = str;
std::transform(upper.begin(), upper.end(), upper.begin(),
[](unsigned char c) { return std::toupper(c); });
if (upper == "LIVENESS") return ok(health_check_type::liveness);
// ...
}
```
File 2:
include/kcenon/common/interfaces/logger_interface.h(lines 295-328)log_levelenumProposed State
```cpp
// Generic concept and template
template
concept EnumSerializable = requires {
typename enum_traits;
{ enum_traits::values } -> std::convertible_to<std::span<const std::pair<Enum, std::string_view>>>;
};
template
inline std::string to_string(Enum value) {
for (const auto& [e, s] : enum_traits::values) {
if (e == value) return std::string(s);
}
return "UNKNOWN";
}
template
inline Result from_string(std::string_view str) {
// Case-insensitive lookup using enum_traits
}
// Specialization example
template<>
struct enum_traits<health_check_type> {
static constexpr std::array values = {
std::pair{health_check_type::liveness, "LIVENESS"},
std::pair{health_check_type::readiness, "READINESS"},
// ...
};
};
```
Impact Analysis (Where)
interfaces/monitoring/health_check.hhealth_check_type,health_statusinterfaces/logger_interface.hlog_levelerror/error_codes.hcategory(potential)Estimated duplication: ~100 lines across 3+ files
Implementation Plan (How)
include/kcenon/common/utils/enum_serialization.hEnumSerializableconcept andenum_traitstemplateto_string()andfrom_string()templatesTimeline (When)
Acceptance Criteria
Labels
refactorcode-qualityduplication