plural_namer.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package namer
  2. import (
  3. "strings"
  4. "go-common/app/tool/gengo/types"
  5. )
  6. var consonants = "bcdfghjklmnpqrsttvwxyz"
  7. type pluralNamer struct {
  8. // key is the case-sensitive type name, value is the case-insensitive
  9. // intended output.
  10. exceptions map[string]string
  11. finalize func(string) string
  12. }
  13. // NewPublicPluralNamer returns a namer that returns the plural form of the input
  14. // type's name, starting with a uppercase letter.
  15. func NewPublicPluralNamer(exceptions map[string]string) *pluralNamer {
  16. return &pluralNamer{exceptions, IC}
  17. }
  18. // NewPrivatePluralNamer returns a namer that returns the plural form of the input
  19. // type's name, starting with a lowercase letter.
  20. func NewPrivatePluralNamer(exceptions map[string]string) *pluralNamer {
  21. return &pluralNamer{exceptions, IL}
  22. }
  23. // NewAllLowercasePluralNamer returns a namer that returns the plural form of the input
  24. // type's name, with all letters in lowercase.
  25. func NewAllLowercasePluralNamer(exceptions map[string]string) *pluralNamer {
  26. return &pluralNamer{exceptions, strings.ToLower}
  27. }
  28. // Name returns the plural form of the type's name. If the type's name is found
  29. // in the exceptions map, the map value is returned.
  30. func (r *pluralNamer) Name(t *types.Type) string {
  31. singular := t.Name.Name
  32. var plural string
  33. var ok bool
  34. if plural, ok = r.exceptions[singular]; ok {
  35. return r.finalize(plural)
  36. }
  37. if len(singular) < 2 {
  38. return r.finalize(singular)
  39. }
  40. switch rune(singular[len(singular)-1]) {
  41. case 's', 'x', 'z':
  42. plural = esPlural(singular)
  43. case 'y':
  44. sl := rune(singular[len(singular)-2])
  45. if isConsonant(sl) {
  46. plural = iesPlural(singular)
  47. } else {
  48. plural = sPlural(singular)
  49. }
  50. case 'h':
  51. sl := rune(singular[len(singular)-2])
  52. if sl == 'c' || sl == 's' {
  53. plural = esPlural(singular)
  54. } else {
  55. plural = sPlural(singular)
  56. }
  57. case 'e':
  58. sl := rune(singular[len(singular)-2])
  59. if sl == 'f' {
  60. plural = vesPlural(singular[:len(singular)-1])
  61. } else {
  62. plural = sPlural(singular)
  63. }
  64. case 'f':
  65. plural = vesPlural(singular)
  66. default:
  67. plural = sPlural(singular)
  68. }
  69. return r.finalize(plural)
  70. }
  71. func iesPlural(singular string) string {
  72. return singular[:len(singular)-1] + "ies"
  73. }
  74. func vesPlural(singular string) string {
  75. return singular[:len(singular)-1] + "ves"
  76. }
  77. func esPlural(singular string) string {
  78. return singular + "es"
  79. }
  80. func sPlural(singular string) string {
  81. return singular + "s"
  82. }
  83. func isConsonant(char rune) bool {
  84. for _, c := range consonants {
  85. if char == c {
  86. return true
  87. }
  88. }
  89. return false
  90. }