Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠️🐛 Fix RegularPlural #3408

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/cli/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ var _ = Describe("resourceOptions", func() {
Expect(resource.Plural).To(Equal(plural))
},
Entry("for `FirstMate`", "FirstMate", "firstmates"),
Entry("for `Fish`", "Fish", "fish"),
Entry("for `Helmswoman`", "Helmswoman", "helmswomen"),
Entry("for `Fish`", "Fish", "fishs"),
Entry("for `Helmswoman`", "Helmswoman", "helmswomans"),
)
})
})
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"),
)