Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sort.Sort support to operatorsv1.Components #271

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pkg/operators/v1/operator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to define the functions to satisfy sort.Sort in the API? Or can we just call sort.Slice at the sort call site with this less function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, moved it in that direction!

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"`
Expand Down