cleanup sort implementation

pull/564/head
Kevin Wiesmüller 2019-03-02 12:07:19 +01:00
parent 9ac127408d
commit 081ccdc75e
3 changed files with 12 additions and 75 deletions

View File

@ -8,7 +8,6 @@ go_library(
"gvkparser.go",
"managedfields.go",
"pathelement.go",
"sort.go",
"typeconverter.go",
"versionconverter.go",
],

View File

@ -150,24 +150,23 @@ func encodeManagedFields(managedFields fieldpath.ManagedFields) (encodedManagedF
}
func sortEncodedManagedFields(encodedManagedFields []metav1.ManagedFieldsEntry) (sortedManagedFields []metav1.ManagedFieldsEntry, err error) {
operation := func(p, q metav1.ManagedFieldsEntry) bool {
return p.Operation < q.Operation
}
timestamp := func(p, q metav1.ManagedFieldsEntry) bool {
sort.Slice(encodedManagedFields, func(i, j int) bool {
p, q := encodedManagedFields[i], encodedManagedFields[j]
if p.Operation != q.Operation {
return p.Operation < q.Operation
}
if p.Time == nil || q.Time == nil {
return false
}
return q.Time.Before(p.Time)
}
manager := func(p, q metav1.ManagedFieldsEntry) bool {
if !p.Time.Equal(q.Time) {
return q.Time.Before(p.Time)
}
return p.Manager < q.Manager
}
})
sorter := &managedFieldsSorter{
less: []managedFieldsLessFunc{operation, timestamp, manager},
}
sorter.Sort(encodedManagedFields)
return encodedManagedFields, nil
}

View File

@ -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)
}