Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 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/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 | } |
| 47 | sort.Sort(list) |
| 48 | return list.types |
| 49 | } |
| 50 | |
| 51 | // OrderTypes assigns a name to every type, and returns a list sorted by those |
| 52 | // names. |
| 53 | func (o *Orderer) OrderTypes(typeList []*types.Type) []*types.Type { |
| 54 | list := tList{ |
| 55 | namer: o.Namer, |
| 56 | types: typeList, |
| 57 | } |
| 58 | sort.Sort(list) |
| 59 | return list.types |
| 60 | } |
| 61 | |
| 62 | type tList struct { |
| 63 | namer Namer |
| 64 | types []*types.Type |
| 65 | } |
| 66 | |
| 67 | func (t tList) Len() int { return len(t.types) } |
| 68 | func (t tList) Less(i, j int) bool { return t.namer.Name(t.types[i]) < t.namer.Name(t.types[j]) } |
| 69 | func (t tList) Swap(i, j int) { t.types[i], t.types[j] = t.types[j], t.types[i] } |