From 0389ef78fe273f25f326f8f0d15b0e679017e445 Mon Sep 17 00:00:00 2001 From: Alexander Greene Date: Thu, 27 Oct 2022 09:16:02 -0400 Subject: [PATCH] Add sort.Sort support to operatorsv1.Components This commit updates the operatorsv1.Components structure to implement the interface required by the sort.Sort function provided in golang. Signed-off-by: Alexander Greene --- pkg/operators/v1/operator_types.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pkg/operators/v1/operator_types.go b/pkg/operators/v1/operator_types.go index af735950f..210206b5f 100644 --- a/pkg/operators/v1/operator_types.go +++ b/pkg/operators/v1/operator_types.go @@ -47,6 +47,36 @@ type Components struct { Refs []RichReference `json:"refs,omitempty"` } +// Len returns the number of Refs in the component.Refs array +// Used to implement the interface required by sort.Sort function. +func (c Components) Len() int { + return len(c.Refs) +} + +// Less returns true if argument i should appear in an ordered list +// of references before argument j. +// Used to implement the sort.Sort interface. +func (c Components) Less(i, j int) bool { + if c.Refs[i].Kind != c.Refs[j].Kind { + return c.Refs[i].Kind < c.Refs[j].Kind + } + + if c.Refs[i].APIVersion != c.Refs[j].APIVersion { + return c.Refs[i].APIVersion < c.Refs[j].APIVersion + } + + if c.Refs[i].Namespace != c.Refs[j].Namespace { + return c.Refs[i].Namespace < c.Refs[j].Namespace + } + return c.Refs[i].Name < c.Refs[j].Name +} + +// Swap swaps the elements with indexes i and j. +// Used to implement the sort.Sort interface. +func (c Components) Swap(i, j int) { + c.Refs[i], c.Refs[j] = c.Refs[j], c.Refs[i] +} + // RichReference is a reference to a resource, enriched with its status conditions. type RichReference struct { *corev1.ObjectReference `json:",inline"`