From d1c52e78382e3909a4ff70373fcf5da628ef8238 Mon Sep 17 00:00:00 2001 From: tylerslaton Date: Fri, 19 Jan 2024 12:31:08 -0500 Subject: [PATCH] fix: address issue with computeclass tables not converting properly Signed-off-by: tylerslaton --- pkg/cli/builder/table/funcs.go | 54 +++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/pkg/cli/builder/table/funcs.go b/pkg/cli/builder/table/funcs.go index cace2143c..9e19ce002 100644 --- a/pkg/cli/builder/table/funcs.go +++ b/pkg/cli/builder/table/funcs.go @@ -272,46 +272,58 @@ func DisplayRange(minVal, maxVal any) (string, error) { } func DefaultMemory(obj any) (string, error) { - b, ok := obj.(adminv1.ComputeClassMemory) - if !ok { - return "", fmt.Errorf("object passed is not a ComputeClassMemory struct") + switch b := obj.(type) { + case apiv1.ComputeClassMemory: + return defaultMemory(b.Default, b.Max), nil + case adminv1.ComputeClassMemory: + return defaultMemory(b.Default, b.Max), nil + default: + return "", fmt.Errorf("object passed cannot be converted into a ComputeClassMemory struct") } +} - result := b.Default - if b.Default == "0" || b.Default == "" { - result = b.Max +func defaultMemory(def, max string) string { + result := def + if def == "0" || def == "" { + result = max if result == "0" || result == "" { result = "Unrestricted" } } - return result, nil + return result } func MemoryToRange(obj any) (string, error) { - b, ok := obj.(adminv1.ComputeClassMemory) - if !ok { - return "", fmt.Errorf("object passed is not a ComputeClassMemory struct") + switch b := obj.(type) { + case apiv1.ComputeClassMemory: + return memoryToRange(b.Min, b.Max, b.Values), nil + case adminv1.ComputeClassMemory: + return memoryToRange(b.Min, b.Max, b.Values), nil + default: + return "", fmt.Errorf("object passed cannot be converted into a ComputeClassMemory struct") } +} - min := b.Min - if min == "" { - min = "0" +func memoryToRange(memoryMin, memoryMax string, values []string) string { + if len(values) != 0 { + return strings.Join(values, ",") } - max := b.Max - if max == "" || max == "0" { - max = "Unrestricted" + rangeMin := memoryMin + if rangeMin == "" { + rangeMin = "0" } - if len(b.Values) != 0 { - return strings.Join(b.Values, ","), nil + rangeMax := memoryMax + if rangeMax == "" || rangeMax == "0" { + rangeMax = "Unrestricted" } - if max == "Unrestricted" && min == "0" { - return "Unrestricted", nil + if rangeMin == "0" && rangeMax == "Unrestricted" { + return "Unrestricted" } - return fmt.Sprintf("%v-%v", min, max), nil + return fmt.Sprintf("%v-%v", rangeMin, rangeMax) } func AppGeneration(app apiv1.App, msg string) string {