blob: f86282b9b115179b17bad78c62411b5a0f5b59c2 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package namer
18
19import (
20 "sort"
21
22 "k8s.io/gengo/types"
23)
24
25// Orderer produces an ordering of types given a Namer.
26type 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.
32func (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.
53func (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
62type tList struct {
63 namer Namer
64 types []*types.Type
65}
66
67func (t tList) Len() int { return len(t.types) }
68func (t tList) Less(i, j int) bool { return t.namer.Name(t.types[i]) < t.namer.Name(t.types[j]) }
69func (t tList) Swap(i, j int) { t.types[i], t.types[j] = t.types[j], t.types[i] }