Skip to content

Commit 52b55dd

Browse files
AchoArnoldCopilot
andcommitted
refactor: use go-pluralize for entity name formatting
Replace custom pluralization logic with github.com/gertd/go-pluralize for more accurate English pluralization rules. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ceb3b35 commit 52b55dd

3 files changed

Lines changed: 7 additions & 19 deletions

File tree

‎api/go.mod‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
1919
github.com/dgraph-io/ristretto/v2 v2.4.0
2020
github.com/dustin/go-humanize v1.0.1
21+
github.com/gertd/go-pluralize v0.2.1
2122
github.com/go-hermes/hermes/v2 v2.6.2
2223
github.com/gofiber/contrib/otelfiber v1.0.10
2324
github.com/gofiber/fiber/v2 v2.52.13

‎api/go.sum‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
112112
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
113113
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
114114
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
115+
github.com/gertd/go-pluralize v0.2.1 h1:M3uASbVjMnTsPb0PNqg+E/24Vwigyo/tvyMTtAlLgiA=
116+
github.com/gertd/go-pluralize v0.2.1/go.mod h1:rbYaKDbsXxmRfr8uygAEKhOWsjyrrqrkHVpZvoOp8zk=
115117
github.com/go-faster/city v1.0.1 h1:4WAxSZ3V2Ws4QRDrscLEDcibJY8uf41H6AhXDrNDcGw=
116118
github.com/go-faster/city v1.0.1/go.mod h1:jKcUJId49qdW3L1qKHH/3wPeUstCVpVSXTM6vO3VcTw=
117119
github.com/go-faster/errors v0.7.1 h1:MkJTnDoEdi9pDabt1dpWf7AA8/BaSYZqibYyhZ20AYg=

‎api/pkg/services/entitlement_service.go‎

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/NdoleStudio/httpsms/pkg/entities"
1010
"github.com/NdoleStudio/httpsms/pkg/repositories"
1111
"github.com/NdoleStudio/httpsms/pkg/telemetry"
12+
pluralize "github.com/gertd/go-pluralize"
1213
"github.com/palantir/stacktrace"
1314
)
1415

@@ -88,7 +89,7 @@ func (service *EntitlementService) Check(
8889
return &EntitlementCheckResult{
8990
Allowed: false,
9091
Message: fmt.Sprintf(
91-
"Upgrade to a paid plan to create more than %d %s. Visit https://httpsms.com/pricing for details.",
92+
"Upgrade to a paid plan to create more than [%d] %s. Visit https://httpsms.com/pricing for details.",
9293
limit,
9394
formatEntityName(entityName, true),
9495
),
@@ -112,25 +113,9 @@ func formatEntityName(name string, plural bool) string {
112113
words = append(words, strings.ToLower(name[start:]))
113114

114115
if plural && len(words) > 0 {
115-
last := words[len(words)-1]
116-
switch {
117-
case strings.HasSuffix(last, "s"), strings.HasSuffix(last, "x"), strings.HasSuffix(last, "z"),
118-
strings.HasSuffix(last, "sh"), strings.HasSuffix(last, "ch"):
119-
words[len(words)-1] = last + "es"
120-
case strings.HasSuffix(last, "y") && len(last) > 1 && !isVowel(last[len(last)-2]):
121-
words[len(words)-1] = last[:len(last)-1] + "ies"
122-
default:
123-
words[len(words)-1] = last + "s"
124-
}
116+
client := pluralize.NewClient()
117+
words[len(words)-1] = client.Plural(words[len(words)-1])
125118
}
126119

127120
return strings.Join(words, " ")
128121
}
129-
130-
func isVowel(c byte) bool {
131-
switch c {
132-
case 'a', 'e', 'i', 'o', 'u':
133-
return true
134-
}
135-
return false
136-
}

0 commit comments

Comments
 (0)