summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-05-24 14:09:24 +0300
committerPaul Buetow <paul@buetow.org>2026-05-24 14:09:24 +0300
commitc95b15346def9fe1b34615b464f6a86046c0819b (patch)
tree64e51b1f091b6dc9991f9fa2d18f7281043ec0d3
parentf02d0996bcaee49a4226ba678788e7550b06be29 (diff)
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.
-rw-r--r--internal/rpn/metric_type.go35
1 files 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).