plural_namer.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "strings"
  16. "k8s.io/gengo/types"
  17. )
  18. var consonants = "bcdfghjklmnpqrsttvwxyz"
  19. type pluralNamer struct {
  20. // key is the case-sensitive type name, value is the case-insensitive
  21. // intended output.
  22. exceptions map[string]string
  23. finalize func(string) string
  24. }
  25. // NewPublicPluralNamer returns a namer that returns the plural form of the input
  26. // type's name, starting with a uppercase letter.
  27. func NewPublicPluralNamer(exceptions map[string]string) *pluralNamer {
  28. return &pluralNamer{exceptions, IC}
  29. }
  30. // NewPrivatePluralNamer returns a namer that returns the plural form of the input
  31. // type's name, starting with a lowercase letter.
  32. func NewPrivatePluralNamer(exceptions map[string]string) *pluralNamer {
  33. return &pluralNamer{exceptions, IL}
  34. }
  35. // NewAllLowercasePluralNamer returns a namer that returns the plural form of the input
  36. // type's name, with all letters in lowercase.
  37. func NewAllLowercasePluralNamer(exceptions map[string]string) *pluralNamer {
  38. return &pluralNamer{exceptions, strings.ToLower}
  39. }
  40. // Name returns the plural form of the type's name. If the type's name is found
  41. // in the exceptions map, the map value is returned.
  42. func (r *pluralNamer) Name(t *types.Type) string {
  43. singular := t.Name.Name
  44. var plural string
  45. var ok bool
  46. if plural, ok = r.exceptions[singular]; ok {
  47. return r.finalize(plural)
  48. }
  49. if len(singular) < 2 {
  50. return r.finalize(singular)
  51. }
  52. switch rune(singular[len(singular)-1]) {
  53. case 's', 'x', 'z':
  54. plural = esPlural(singular)
  55. case 'y':
  56. sl := rune(singular[len(singular)-2])
  57. if isConsonant(sl) {
  58. plural = iesPlural(singular)
  59. } else {
  60. plural = sPlural(singular)
  61. }
  62. case 'h':
  63. sl := rune(singular[len(singular)-2])
  64. if sl == 'c' || sl == 's' {
  65. plural = esPlural(singular)
  66. } else {
  67. plural = sPlural(singular)
  68. }
  69. case 'e':
  70. sl := rune(singular[len(singular)-2])
  71. if sl == 'f' {
  72. plural = vesPlural(singular[:len(singular)-1])
  73. } else {
  74. plural = sPlural(singular)
  75. }
  76. case 'f':
  77. plural = vesPlural(singular)
  78. default:
  79. plural = sPlural(singular)
  80. }
  81. return r.finalize(plural)
  82. }
  83. func iesPlural(singular string) string {
  84. return singular[:len(singular)-1] + "ies"
  85. }
  86. func vesPlural(singular string) string {
  87. return singular[:len(singular)-1] + "ves"
  88. }
  89. func esPlural(singular string) string {
  90. return singular + "es"
  91. }
  92. func sPlural(singular string) string {
  93. return singular + "s"
  94. }
  95. func isConsonant(char rune) bool {
  96. for _, c := range consonants {
  97. if char == c {
  98. return true
  99. }
  100. }
  101. return false
  102. }