Skip to content

Commit

Permalink
fix RegularPlural
Browse files Browse the repository at this point in the history
Signed-off-by: hang.jiang <hang.jiang@daocloud.io>
  • Loading branch information
hang.jiang committed May 12, 2023
1 parent d64868e commit 49a6853
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
21 changes: 18 additions & 3 deletions pkg/model/resource/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"fmt"
"path"
"strings"

"github.com/gobuffalo/flect"
)

const V1beta1 = "v1beta1"
Expand Down Expand Up @@ -67,7 +65,24 @@ func APIPackagePathLegacy(repo, group, version string, multiGroup bool) string {
return path.Join(repo, "api", version)
}

var unpluralizedSuffixes = []string{
"endpoints",
}

// RegularPlural returns a default plural form when none was specified
// refer to https://github.com/kubernetes/apimachinery/blob/master/pkg/api/meta/restmapper.go#L126
func RegularPlural(singular string) string {
return flect.Pluralize(strings.ToLower(singular))
singularName := strings.ToLower(singular)
for _, skip := range unpluralizedSuffixes {
if strings.HasSuffix(singularName, skip) {
return singular
}
}
switch string(singularName[len(singularName)-1]) {
case "s":
return singularName + "es"
case "y":
return strings.TrimSuffix(singularName, "y") + "ies"
}
return singularName + "s"
}
5 changes: 3 additions & 2 deletions pkg/model/resource/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ var _ = Describe("APIPackagePathLegacy", func() {

var _ = DescribeTable("RegularPlural should return the regular plural form",
func(singular, plural string) { Expect(RegularPlural(singular)).To(Equal(plural)) },
Entry("unpluralized", "endpoints", "endpoints"),
Entry("basic singular", "firstmate", "firstmates"),
Entry("capitalized singular", "Firstmate", "firstmates"),
Entry("camel-cased singular", "FirstMate", "firstmates"),
Entry("irregular well-known plurals", "fish", "fish"),
Entry("irregular well-known plurals", "helmswoman", "helmswomen"),
Entry("basic singular", "fish", "fishs"),
Entry("basic singular", "helmswoman", "helmswomans"),
)

0 comments on commit 49a6853

Please sign in to comment.