Skip to content

refactor: Extract generic enum serialization template to eliminate duplication #293

Description

@kcenon

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)

  1. Create include/kcenon/common/utils/enum_serialization.h
  2. Define EnumSerializable concept and enum_traits template
  3. Implement generic to_string() and from_string() templates
  4. Add specializations for existing enums
  5. Replace existing implementations with template usage
  6. Add unit tests for generic serialization
  7. Update documentation

Timeline (When)

  • Target: Next minor release

Acceptance Criteria

  • Generic enum serialization template created
  • All existing enum serializations migrated
  • No code duplication in enum-to-string conversions
  • Unit tests cover all enum types
  • Documentation updated

Labels

  • refactor
  • code-quality
  • duplication

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions