From 26cb5d53fc40010a22b7a2b70800a4518371fa8d Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 9 Sep 2016 16:19:28 +0100 Subject: [PATCH] refactor: extract pluralization function This preserves the existing behaviour of showing count=0 as a plural rather than empty. However, I rather suspect we cannot actually encounter that case in the current code. In the "no count found" case we now *set* the base.LabelMinor to empty instead of leaving it alone. This is ok since it is empty to begin with. --- render/detailed/summary.go | 46 ++++++++++++++------------------------ 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/render/detailed/summary.go b/render/detailed/summary.go index e326b0b98..da74392e8 100644 --- a/render/detailed/summary.go +++ b/render/detailed/summary.go @@ -195,13 +195,8 @@ func containerImageNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bo } } - if c, ok := n.Counters.Lookup(report.Container); ok { - if c == 1 { - base.LabelMinor = fmt.Sprintf("%d container", c) - } else { - base.LabelMinor = fmt.Sprintf("%d containers", c) - } - } + base.LabelMinor = pluralize(n.Counters, report.Container, "container", "containers") + return base, true } @@ -214,13 +209,7 @@ func addKubernetesLabelAndRank(base NodeSummary, n report.Node) NodeSummary { func podNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) { base = addKubernetesLabelAndRank(base, n) - if c, ok := n.Counters.Lookup(report.Container); ok { - if c == 1 { - base.LabelMinor = fmt.Sprintf("%d container", c) - } else { - base.LabelMinor = fmt.Sprintf("%d containers", c) - } - } + base.LabelMinor = pluralize(n.Counters, report.Container, "container", "containers") return base, true } @@ -229,14 +218,9 @@ func podGroupNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) { base = addKubernetesLabelAndRank(base, n) base.Stack = true - // NB: pods are the highest aggregation level for which we display counts. - if p, ok := n.Counters.Lookup(report.Pod); ok { - if p == 1 { - base.LabelMinor = fmt.Sprintf("%d pod", p) - } else { - base.LabelMinor = fmt.Sprintf("%d pods", p) - } - } + // NB: pods are the highest aggregation level for which we display + // counts. + base.LabelMinor = pluralize(n.Counters, report.Pod, "pod", "pods") return base, true } @@ -272,13 +256,7 @@ func groupNodeSummary(base NodeSummary, r report.Report, n report.Node) (NodeSum t, ok := r.Topology(parts[1]) if ok && t.Label != "" { - if count, ok := n.Counters.Lookup(parts[1]); ok { - if count == 1 { - base.LabelMinor = fmt.Sprintf("%d %s", count, t.Label) - } else { - base.LabelMinor = fmt.Sprintf("%d %s", count, t.LabelPlural) - } - } + base.LabelMinor = pluralize(n.Counters, parts[1], t.Label, t.LabelPlural) } base.Shape = t.GetShape() @@ -286,6 +264,16 @@ func groupNodeSummary(base NodeSummary, r report.Report, n report.Node) (NodeSum return base, true } +func pluralize(counters report.Counters, key, singular, plural string) string { + if c, ok := counters.Lookup(key); ok { + if c == 1 { + return fmt.Sprintf("%d %s", c, singular) + } + return fmt.Sprintf("%d %s", c, plural) + } + return "" +} + type nodeSummariesByID []NodeSummary func (s nodeSummariesByID) Len() int { return len(s) }