| 1 |
/* |
| 2 |
Copyright 2015 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package namer |
| 18 |
|
| 19 |
import ( |
| 20 |
"sort" |
| 21 |
|
| 22 |
"k8s.io/gengo/v2/types" |
| 23 |
) |
| 24 |
|
| 25 |
// Orderer produces an ordering of types given a Namer. |
| 26 |
type Orderer struct { |
| 27 |
Namer |
| 28 |
} |
| 29 |
|
| 30 |
// OrderUniverse assigns a name to every type in the Universe, including Types, |
| 31 |
// Functions and Variables, and returns a list sorted by those names. |
| 32 |
func (o *Orderer) OrderUniverse(u types.Universe) []*types.Type { |
| 33 |
list := tList{ |
| 34 |
namer: o.Namer, |
| 35 |
} |
| 36 |
for _, p := range u { |
| 37 |
for _, t := range p.Types { |
| 38 |
list.types = append(list.types, t) |
| 39 |
} |
| 40 |
for _, f := range p.Functions { |
| 41 |
list.types = append(list.types, f) |
| 42 |
} |
| 43 |
for _, v := range p.Variables { |
| 44 |
list.types = append(list.types, v) |
| 45 |
} |
| 46 |
for _, v := range p.Constants { |
| 47 |
list.types = append(list.types, v) |
| 48 |
} |
| 49 |
} |
| 50 |
sort.Sort(list) |
| 51 |
return list.types |
| 52 |
} |
| 53 |
|
| 54 |
// OrderTypes assigns a name to every type, and returns a list sorted by those |
| 55 |
// names. |
| 56 |
func (o *Orderer) OrderTypes(typeList []*types.Type) []*types.Type { |
| 57 |
list := tList{ |
| 58 |
namer: o.Namer, |
| 59 |
types: typeList, |
| 60 |
} |
| 61 |
sort.Sort(list) |
| 62 |
return list.types |
| 63 |
} |
| 64 |
|
| 65 |
type tList struct { |
| 66 |
namer Namer |
| 67 |
types []*types.Type |
| 68 |
} |
| 69 |
|
| 70 |
func (t tList) Len() int { return len(t.types) } |
| 71 |
func (t tList) Less(i, j int) bool { return t.namer.Name(t.types[i]) < t.namer.Name(t.types[j]) } |
| 72 |
func (t tList) Swap(i, j int) { t.types[i], t.types[j] = t.types[j], t.types[i] } |