2 Copyright 2015 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
25 var consonants = "bcdfghjklmnpqrsttvwxyz"
27 type pluralNamer struct {
28 // key is the case-sensitive type name, value is the case-insensitive
30 exceptions map[string]string
31 finalize func(string) string
34 // NewPublicPluralNamer returns a namer that returns the plural form of the input
35 // type's name, starting with a uppercase letter.
36 func NewPublicPluralNamer(exceptions map[string]string) *pluralNamer {
37 return &pluralNamer{exceptions, IC}
40 // NewPrivatePluralNamer returns a namer that returns the plural form of the input
41 // type's name, starting with a lowercase letter.
42 func NewPrivatePluralNamer(exceptions map[string]string) *pluralNamer {
43 return &pluralNamer{exceptions, IL}
46 // NewAllLowercasePluralNamer returns a namer that returns the plural form of the input
47 // type's name, with all letters in lowercase.
48 func NewAllLowercasePluralNamer(exceptions map[string]string) *pluralNamer {
49 return &pluralNamer{exceptions, strings.ToLower}
52 // Name returns the plural form of the type's name. If the type's name is found
53 // in the exceptions map, the map value is returned.
54 func (r *pluralNamer) Name(t *types.Type) string {
55 singular := t.Name.Name
58 if plural, ok = r.exceptions[singular]; ok {
59 return r.finalize(plural)
61 if len(singular) < 2 {
62 return r.finalize(singular)
65 switch rune(singular[len(singular)-1]) {
67 plural = esPlural(singular)
69 sl := rune(singular[len(singular)-2])
71 plural = iesPlural(singular)
73 plural = sPlural(singular)
76 sl := rune(singular[len(singular)-2])
77 if sl == 'c' || sl == 's' {
78 plural = esPlural(singular)
80 plural = sPlural(singular)
83 sl := rune(singular[len(singular)-2])
85 plural = vesPlural(singular[:len(singular)-1])
87 plural = sPlural(singular)
90 plural = vesPlural(singular)
92 plural = sPlural(singular)
94 return r.finalize(plural)
97 func iesPlural(singular string) string {
98 return singular[:len(singular)-1] + "ies"
101 func vesPlural(singular string) string {
102 return singular[:len(singular)-1] + "ves"
105 func esPlural(singular string) string {
106 return singular + "es"
109 func sPlural(singular string) string {
110 return singular + "s"
113 func isConsonant(char rune) bool {
114 for _, c := range consonants {