Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

refactor: use go-pluralize for entity name formatting

Acho Arnold52b55dd82328
diff --git a/api/go.mod b/api/go.mod
index 7d752977..40a311d3 100644
--- a/api/go.mod
+++ b/api/go.mod
@@ -18,6 +18,7 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/dgraph-io/ristretto/v2 v2.4.0
github.com/dustin/go-humanize v1.0.1
+ github.com/gertd/go-pluralize v0.2.1
github.com/go-hermes/hermes/v2 v2.6.2
github.com/gofiber/contrib/otelfiber v1.0.10
github.com/gofiber/fiber/v2 v2.52.13
diff --git a/api/go.sum b/api/go.sum
index 1e3acc56..02111796 100644
--- a/api/go.sum
+++ b/api/go.sum
@@ -112,6 +112,8 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
+github.com/gertd/go-pluralize v0.2.1 h1:M3uASbVjMnTsPb0PNqg+E/24Vwigyo/tvyMTtAlLgiA=
+github.com/gertd/go-pluralize v0.2.1/go.mod h1:rbYaKDbsXxmRfr8uygAEKhOWsjyrrqrkHVpZvoOp8zk=
github.com/go-faster/city v1.0.1 h1:4WAxSZ3V2Ws4QRDrscLEDcibJY8uf41H6AhXDrNDcGw=
github.com/go-faster/city v1.0.1/go.mod h1:jKcUJId49qdW3L1qKHH/3wPeUstCVpVSXTM6vO3VcTw=
github.com/go-faster/errors v0.7.1 h1:MkJTnDoEdi9pDabt1dpWf7AA8/BaSYZqibYyhZ20AYg=
diff --git a/api/pkg/services/entitlement_service.go b/api/pkg/services/entitlement_service.go
index 2cdae129..cd4d740f 100644
--- a/api/pkg/services/entitlement_service.go
+++ b/api/pkg/services/entitlement_service.go
@@ -9,6 +9,7 @@ import (
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/NdoleStudio/httpsms/pkg/repositories"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
+ pluralize "github.com/gertd/go-pluralize"
"github.com/palantir/stacktrace"
)
@@ -88,7 +89,7 @@ func (service *EntitlementService) Check(
return &EntitlementCheckResult{
Allowed: false,
Message: fmt.Sprintf(
- "Upgrade to a paid plan to create more than %d %s. Visit https://httpsms.com/pricing for details.",
+ "Upgrade to a paid plan to create more than [%d] %s. Visit https://httpsms.com/pricing for details.",
limit,
formatEntityName(entityName, true),
),
@@ -112,25 +113,9 @@ func formatEntityName(name string, plural bool) string {
words = append(words, strings.ToLower(name[start:]))
if plural && len(words) > 0 {
- last := words[len(words)-1]
- switch {
- case strings.HasSuffix(last, "s"), strings.HasSuffix(last, "x"), strings.HasSuffix(last, "z"),
- strings.HasSuffix(last, "sh"), strings.HasSuffix(last, "ch"):
- words[len(words)-1] = last + "es"
- case strings.HasSuffix(last, "y") && len(last) > 1 && !isVowel(last[len(last)-2]):
- words[len(words)-1] = last[:len(last)-1] + "ies"
- default:
- words[len(words)-1] = last + "s"
- }
+ client := pluralize.NewClient()
+ words[len(words)-1] = client.Plural(words[len(words)-1])
}
return strings.Join(words, " ")
}
-
-func isVowel(c byte) bool {
- switch c {
- case 'a', 'e', 'i', 'o', 'u':
- return true
- }
- return false
-}