order.go 1.8 KB

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