template_func.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package template
  2. import (
  3. "github.com/Masterminds/sprig/v3"
  4. "strings"
  5. )
  6. var templateFuncMap = map[string]any{
  7. "leftLower": LeftLower,
  8. "leftUpper": LeftUpper,
  9. "upperShort": UpperShort,
  10. "Id": Id,
  11. "ID": ID,
  12. "id": id,
  13. "plural": Plural,
  14. "toUpper": strings.ToUpper,
  15. }
  16. func LeftLower(s string) string {
  17. if len(s) == 0 {
  18. return s
  19. }
  20. if strings.HasPrefix(s, "ID") {
  21. return strings.Replace(s, "ID", "id", 1)
  22. }
  23. return strings.ToLower(string(s[0])) + s[1:]
  24. }
  25. func LeftUpper(s string) string {
  26. if len(s) == 0 {
  27. return s
  28. }
  29. if strings.HasPrefix(s, "id") {
  30. return strings.Replace(s, "id", "ID", 1)
  31. }
  32. return strings.ToUpper(string(s[0])) + s[1:]
  33. }
  34. func SnakeCase(s string) string {
  35. return sprig.FuncMap()["snakecase"].(func(string) string)(s)
  36. }
  37. func UpperShort(s string) string {
  38. if len(s) == 0 {
  39. return s
  40. }
  41. serviceWords := strings.Split(SnakeCase(s), "_")
  42. if len(serviceWords) == 1 {
  43. return strings.ToUpper(serviceWords[0])[:0]
  44. }
  45. var short string
  46. for _, serviceWord := range serviceWords {
  47. short = short + strings.ToUpper(string(serviceWord[0]))
  48. }
  49. return short
  50. }
  51. func Id(s string) string {
  52. if strings.HasSuffix(s, "ID") {
  53. index := strings.LastIndex(s, "ID")
  54. return s[0:index] + "Id"
  55. }
  56. if strings.HasSuffix(s, "id") {
  57. index := strings.LastIndex(s, "id")
  58. return s[0:index] + "Id"
  59. }
  60. if strings.HasSuffix(s, "iD") {
  61. index := strings.LastIndex(s, "iD")
  62. return s[0:index] + "Id"
  63. }
  64. if strings.HasSuffix(s, "IDs") {
  65. index := strings.LastIndex(s, "IDs")
  66. return s[0:index] + "Ids"
  67. }
  68. if strings.HasSuffix(s, "ids") {
  69. index := strings.LastIndex(s, "ids")
  70. return s[0:index] + "Ids"
  71. }
  72. if strings.HasSuffix(s, "iDs") {
  73. index := strings.LastIndex(s, "iDs")
  74. return s[0:index] + "Ids"
  75. }
  76. return s
  77. }
  78. func ID(s string) string {
  79. if strings.HasSuffix(s, "Id") {
  80. index := strings.LastIndex(s, "Id")
  81. return s[0:index] + "ID"
  82. }
  83. if strings.HasSuffix(s, "id") {
  84. index := strings.LastIndex(s, "id")
  85. return s[0:index] + "ID"
  86. }
  87. if strings.HasSuffix(s, "iD") {
  88. index := strings.LastIndex(s, "iD")
  89. return s[0:index] + "ID"
  90. }
  91. if strings.HasSuffix(s, "Ids") {
  92. index := strings.LastIndex(s, "Ids")
  93. return s[0:index] + "IDs"
  94. }
  95. if strings.HasSuffix(s, "ids") {
  96. index := strings.LastIndex(s, "ids")
  97. return s[0:index] + "IDs"
  98. }
  99. if strings.HasSuffix(s, "iDs") {
  100. index := strings.LastIndex(s, "iDs")
  101. return s[0:index] + "IDs"
  102. }
  103. return s
  104. }
  105. func id(s string) string {
  106. if strings.HasSuffix(s, "Id") {
  107. index := strings.LastIndex(s, "Id")
  108. return s[0:index] + "id"
  109. }
  110. if strings.HasSuffix(s, "ID") {
  111. index := strings.LastIndex(s, "ID")
  112. return s[0:index] + "id"
  113. }
  114. if strings.HasSuffix(s, "iD") {
  115. index := strings.LastIndex(s, "iD")
  116. return s[0:index] + "id"
  117. }
  118. if strings.HasSuffix(s, "Ids") {
  119. index := strings.LastIndex(s, "Ids")
  120. return s[0:index] + "ids"
  121. }
  122. if strings.HasSuffix(s, "IDs") {
  123. index := strings.LastIndex(s, "IDs")
  124. return s[0:index] + "ids"
  125. }
  126. if strings.HasSuffix(s, "iDs") {
  127. index := strings.LastIndex(s, "iDs")
  128. return s[0:index] + "ids"
  129. }
  130. return s
  131. }
  132. var specialPluralWordMap = map[string]string{
  133. "goose": "geese",
  134. "Goose": "Geese",
  135. "foot": "feet",
  136. "Foot": "Feet",
  137. "tooth": "teeth",
  138. "Tooth": "Teeth",
  139. "man": "men",
  140. "Man": "Men",
  141. "woman": "women",
  142. "Woman": "Women",
  143. "mouse": "mice",
  144. "Mouse": "Mice",
  145. "sheep": "sheep",
  146. "Sheep": "Sheep",
  147. "deer": "deer",
  148. "Deer": "Deer",
  149. "fish": "fish",
  150. "Fish": "Fish",
  151. "child": "children",
  152. "Child": "Children",
  153. "ox": "oxen",
  154. "Ox": "Oxen",
  155. "information": "information",
  156. "Information": "Information",
  157. "info": "infos",
  158. "Info": "Infos",
  159. }
  160. func Plural(s string) string {
  161. for specialPluralWord, pluralSpecialPluralWord := range specialPluralWordMap {
  162. if strings.HasSuffix(s, specialPluralWord) {
  163. s = strings.TrimRight(s, specialPluralWord)
  164. s = s + pluralSpecialPluralWord
  165. return s
  166. }
  167. }
  168. if strings.HasSuffix(s, "s") || strings.HasSuffix(s, "x") ||
  169. strings.HasSuffix(s, "sh") || strings.HasSuffix(s, "ch") ||
  170. strings.HasSuffix(s, "o") {
  171. return s + "es"
  172. } else if strings.HasSuffix(s, "f") {
  173. return s[:len(s)-1] + "ves"
  174. } else if strings.HasSuffix(s, "fe") {
  175. return s[:len(s)-2] + "ves"
  176. } else if strings.HasSuffix(s, "y") {
  177. return s[:len(s)-1] + "ies"
  178. } else {
  179. return s + "s"
  180. }
  181. }