Skip to content

Commit 0389ef7

Browse files
committed
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 <[email protected]>
1 parent b527a19 commit 0389ef7

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Diff for: pkg/operators/v1/operator_types.go

+30
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,36 @@ type Components struct {
4747
Refs []RichReference `json:"refs,omitempty"`
4848
}
4949

50+
// Len returns the number of Refs in the component.Refs array
51+
// Used to implement the interface required by sort.Sort function.
52+
func (c Components) Len() int {
53+
return len(c.Refs)
54+
}
55+
56+
// Less returns true if argument i should appear in an ordered list
57+
// of references before argument j.
58+
// Used to implement the sort.Sort interface.
59+
func (c Components) Less(i, j int) bool {
60+
if c.Refs[i].Kind != c.Refs[j].Kind {
61+
return c.Refs[i].Kind < c.Refs[j].Kind
62+
}
63+
64+
if c.Refs[i].APIVersion != c.Refs[j].APIVersion {
65+
return c.Refs[i].APIVersion < c.Refs[j].APIVersion
66+
}
67+
68+
if c.Refs[i].Namespace != c.Refs[j].Namespace {
69+
return c.Refs[i].Namespace < c.Refs[j].Namespace
70+
}
71+
return c.Refs[i].Name < c.Refs[j].Name
72+
}
73+
74+
// Swap swaps the elements with indexes i and j.
75+
// Used to implement the sort.Sort interface.
76+
func (c Components) Swap(i, j int) {
77+
c.Refs[i], c.Refs[j] = c.Refs[j], c.Refs[i]
78+
}
79+
5080
// RichReference is a reference to a resource, enriched with its status conditions.
5181
type RichReference struct {
5282
*corev1.ObjectReference `json:",inline"`

0 commit comments

Comments
 (0)