mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
cleanup sort implementation
This commit is contained in:
parent
9ac127408d
commit
081ccdc75e
@ -8,7 +8,6 @@ go_library(
|
|||||||
"gvkparser.go",
|
"gvkparser.go",
|
||||||
"managedfields.go",
|
"managedfields.go",
|
||||||
"pathelement.go",
|
"pathelement.go",
|
||||||
"sort.go",
|
|
||||||
"typeconverter.go",
|
"typeconverter.go",
|
||||||
"versionconverter.go",
|
"versionconverter.go",
|
||||||
],
|
],
|
||||||
|
@ -150,24 +150,23 @@ func encodeManagedFields(managedFields fieldpath.ManagedFields) (encodedManagedF
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sortEncodedManagedFields(encodedManagedFields []metav1.ManagedFieldsEntry) (sortedManagedFields []metav1.ManagedFieldsEntry, err error) {
|
func sortEncodedManagedFields(encodedManagedFields []metav1.ManagedFieldsEntry) (sortedManagedFields []metav1.ManagedFieldsEntry, err error) {
|
||||||
operation := func(p, q metav1.ManagedFieldsEntry) bool {
|
sort.Slice(encodedManagedFields, func(i, j int) bool {
|
||||||
return p.Operation < q.Operation
|
p, q := encodedManagedFields[i], encodedManagedFields[j]
|
||||||
}
|
|
||||||
timestamp := func(p, q metav1.ManagedFieldsEntry) bool {
|
if p.Operation != q.Operation {
|
||||||
|
return p.Operation < q.Operation
|
||||||
|
}
|
||||||
|
|
||||||
if p.Time == nil || q.Time == nil {
|
if p.Time == nil || q.Time == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return q.Time.Before(p.Time)
|
if !p.Time.Equal(q.Time) {
|
||||||
}
|
return q.Time.Before(p.Time)
|
||||||
manager := func(p, q metav1.ManagedFieldsEntry) bool {
|
}
|
||||||
|
|
||||||
return p.Manager < q.Manager
|
return p.Manager < q.Manager
|
||||||
}
|
})
|
||||||
|
|
||||||
sorter := &managedFieldsSorter{
|
|
||||||
less: []managedFieldsLessFunc{operation, timestamp, manager},
|
|
||||||
}
|
|
||||||
|
|
||||||
sorter.Sort(encodedManagedFields)
|
|
||||||
return encodedManagedFields, nil
|
return encodedManagedFields, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,61 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2019 The Kubernetes Authors.
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package internal
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sort"
|
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
)
|
|
||||||
|
|
||||||
type managedFieldsLessFunc func(p, q metav1.ManagedFieldsEntry) bool
|
|
||||||
|
|
||||||
type managedFieldsSorter struct {
|
|
||||||
fields []metav1.ManagedFieldsEntry
|
|
||||||
less []managedFieldsLessFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *managedFieldsSorter) Sort(fields []metav1.ManagedFieldsEntry) {
|
|
||||||
s.fields = fields
|
|
||||||
sort.Sort(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Len is the amount of managedFields to sort
|
|
||||||
func (s *managedFieldsSorter) Len() int {
|
|
||||||
return len(s.fields)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Swap is part of sort.Interface.
|
|
||||||
func (s *managedFieldsSorter) Swap(p, q int) {
|
|
||||||
s.fields[p], s.fields[q] = s.fields[q], s.fields[p]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Less is part of sort.Interface
|
|
||||||
func (s *managedFieldsSorter) Less(p, q int) bool {
|
|
||||||
a, b := s.fields[p], s.fields[q]
|
|
||||||
var k int
|
|
||||||
for k = 0; k < len(s.less)-1; k++ {
|
|
||||||
less := s.less[k]
|
|
||||||
switch {
|
|
||||||
case less(a, b):
|
|
||||||
return true
|
|
||||||
case less(b, a):
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return s.less[k](a, b)
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user