raus7n 4 days ago committed by GitHub
commit 241420e432
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -114,45 +114,57 @@ var UninstallOrder KindSortOrder = []string{
"PriorityClass", "PriorityClass",
} }
type KindOrderMap map[string]int
func NewKindOrderMap(ordering KindSortOrder) KindOrderMap {
orderMap := make(KindOrderMap, len(ordering))
for i, kind := range ordering {
orderMap[kind] = i
}
return orderMap
}
// sort manifests by kind. // sort manifests by kind.
//
// Results are sorted by 'ordering', keeping order of items with equal kind/priority // Results are sorted by 'ordering', keeping order of items with equal kind/priority
func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifest { func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifest {
orderMap := NewKindOrderMap(ordering)
sort.SliceStable(manifests, func(i, j int) bool { sort.SliceStable(manifests, func(i, j int) bool {
return lessByKind(manifests[i], manifests[j], manifests[i].Head.Kind, manifests[j].Head.Kind, ordering) return orderMap.Less(
manifests[i].Head.Kind,
manifests[j].Head.Kind,
)
}) })
return manifests return manifests
} }
// sort hooks by kind, using an out-of-place sort to preserve the input parameters. // sort hooks by kind.
// //
// Results are sorted by 'ordering', keeping order of items with equal kind/priority // Results are sorted by 'ordering', keeping order of items with equal kind/priority
func sortHooksByKind(hooks []*release.Hook, ordering KindSortOrder) []*release.Hook { func sortHooksByKind(hooks []*release.Hook, ordering KindSortOrder) []*release.Hook {
h := hooks orderMap := NewKindOrderMap(ordering)
sort.SliceStable(h, func(i, j int) bool { sort.SliceStable(hooks, func(i, j int) bool {
return lessByKind(h[i], h[j], h[i].Kind, h[j].Kind, ordering) return orderMap.Less(
hooks[i].Kind,
hooks[j].Kind,
)
}) })
return h return hooks
} }
func lessByKind(_, _ any, kindA, kindB string, o KindSortOrder) bool { func (k KindOrderMap) Less(kindA, kindB string) bool {
ordering := make(map[string]int, len(o)) first, aok := k[kindA]
for v, k := range o { second, bok := k[kindB]
ordering[k] = v
}
first, aok := ordering[kindA]
second, bok := ordering[kindB]
if !aok && !bok { if !aok && !bok {
// if both are unknown then sort alphabetically by kind, keep original order if same kind // if both are unknown then sort alphabetically by kind, keep original order if same kind
if kindA != kindB { if kindA != kindB {
return kindA < kindB return kindA < kindB
} }
return first < second return false
} }
// unknown kind is last // unknown kind is last
if !aok { if !aok {
return false return false
@ -160,6 +172,7 @@ func lessByKind(_, _ any, kindA, kindB string, o KindSortOrder) bool {
if !bok { if !bok {
return true return true
} }
// sort different kinds, keep original order if same priority // sort different kinds, keep original order if same priority
return first < second return first < second
} }

@ -114,45 +114,60 @@ var UninstallOrder KindSortOrder = []string{
"PriorityClass", "PriorityClass",
} }
type KindOrderMap map[string]int
func NewKindOrderMap(ordering KindSortOrder) KindOrderMap {
orderMap := make(KindOrderMap, len(ordering))
for i, kind := range ordering {
orderMap[kind] = i
}
return orderMap
}
// sort manifests by kind. // sort manifests by kind.
// //
// Results are sorted by 'ordering', keeping order of items with equal kind/priority // Results are sorted by 'ordering', keeping order of items with equal kind/priority
func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifest { func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifest {
orderMap := NewKindOrderMap(ordering)
sort.SliceStable(manifests, func(i, j int) bool { sort.SliceStable(manifests, func(i, j int) bool {
return lessByKind(manifests[i], manifests[j], manifests[i].Head.Kind, manifests[j].Head.Kind, ordering) return orderMap.Less(
manifests[i].Head.Kind,
manifests[j].Head.Kind,
)
}) })
return manifests return manifests
} }
// sort hooks by kind, using an out-of-place sort to preserve the input parameters. // sort hooks by kind.
// //
// Results are sorted by 'ordering', keeping order of items with equal kind/priority // Results are sorted by 'ordering', keeping order of items with equal kind/priority
func sortHooksByKind(hooks []*release.Hook, ordering KindSortOrder) []*release.Hook { func sortHooksByKind(hooks []*release.Hook, ordering KindSortOrder) []*release.Hook {
h := hooks orderMap := NewKindOrderMap(ordering)
sort.SliceStable(h, func(i, j int) bool {
return lessByKind(h[i], h[j], h[i].Kind, h[j].Kind, ordering) sort.SliceStable(hooks, func(i, j int) bool {
return orderMap.Less(
hooks[i].Kind,
hooks[j].Kind,
)
}) })
return h return hooks
} }
func lessByKind(_, _ any, kindA, kindB string, o KindSortOrder) bool { func (k KindOrderMap) Less(kindA, kindB string) bool {
ordering := make(map[string]int, len(o)) first, aok := k[kindA]
for v, k := range o { second, bok := k[kindB]
ordering[k] = v
}
first, aok := ordering[kindA]
second, bok := ordering[kindB]
if !aok && !bok { if !aok && !bok {
// if both are unknown then sort alphabetically by kind, keep original order if same kind // if both are unknown then sort alphabetically by kind, keep original order if same kind
if kindA != kindB { if kindA != kindB {
return kindA < kindB return kindA < kindB
} }
return first < second return false
} }
// unknown kind is last // unknown kind is last
if !aok { if !aok {
return false return false
@ -160,6 +175,7 @@ func lessByKind(_, _ any, kindA, kindB string, o KindSortOrder) bool {
if !bok { if !bok {
return true return true
} }
// sort different kinds, keep original order if same priority // sort different kinds, keep original order if same priority
return first < second return first < second
} }

Loading…
Cancel
Save