From c95b15346def9fe1b34615b464f6a86046c0819b Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 24 May 2026 14:09:24 +0300 Subject: fix: replace Category.String() switch with data-driven slice (qj) OCP violation: adding a new Category required editing the String() method. Use a parallel categoryNames slice indexed by Category value instead, so adding a new category only requires appending to the slice. _sentinel already bounds the range. --- internal/rpn/metric_type.go | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/internal/rpn/metric_type.go b/internal/rpn/metric_type.go index 6c77a8c..9d92fb1 100644 --- a/internal/rpn/metric_type.go +++ b/internal/rpn/metric_type.go @@ -36,28 +36,25 @@ const ( // ignore the bool return cannot accidentally treat it as Universal. const invalidCategory Category = -1 +// categoryNames maps each Category value to its human-readable name. +// Adding a new Category only requires appending here; _sentinel bounds the range. +var categoryNames = []string{ + "Universal", + "DataRate", + "DataSize", + "Time", + "Weight", + "Speed", + "Distance", + "Custom", +} + // String returns the human-readable name of the category. func (c Category) String() string { - switch c { - case Universal: - return "Universal" - case DataRate: - return "DataRate" - case DataSize: - return "DataSize" - case Time: - return "Time" - case Weight: - return "Weight" - case Speed: - return "Speed" - case Distance: - return "Distance" - case Custom: - return "Custom" - default: - return fmt.Sprintf("Category(%d)", c) + if c >= 0 && c < Category(len(categoryNames)) { + return categoryNames[c] } + return fmt.Sprintf("Category(%d)", c) } // PrefixMode determines whether data size prefixes are SI (1000-based) or IEC (1024-based). -- cgit v1.2.3