template_func.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. return s
  65. }
  66. func ID(s string) string {
  67. if strings.HasSuffix(s, "Id") {
  68. index := strings.LastIndex(s, "Id")
  69. return s[0:index] + "ID"
  70. }
  71. if strings.HasSuffix(s, "id") {
  72. index := strings.LastIndex(s, "id")
  73. return s[0:index] + "ID"
  74. }
  75. if strings.HasSuffix(s, "iD") {
  76. index := strings.LastIndex(s, "iD")
  77. return s[0:index] + "ID"
  78. }
  79. return s
  80. }
  81. func id(s string) string {
  82. if strings.HasSuffix(s, "Id") {
  83. index := strings.LastIndex(s, "Id")
  84. return s[0:index] + "id"
  85. }
  86. if strings.HasSuffix(s, "ID") {
  87. index := strings.LastIndex(s, "ID")
  88. return s[0:index] + "id"
  89. }
  90. if strings.HasSuffix(s, "iD") {
  91. index := strings.LastIndex(s, "iD")
  92. return s[0:index] + "id"
  93. }
  94. return s
  95. }
  96. var specialPluralWordMap = map[string]string{
  97. "goose": "geese",
  98. "Goose": "Geese",
  99. "foot": "feet",
  100. "Foot": "Feet",
  101. "tooth": "teeth",
  102. "Tooth": "Teeth",
  103. "man": "men",
  104. "Man": "Men",
  105. "woman": "women",
  106. "Woman": "Women",
  107. "mouse": "mice",
  108. "Mouse": "Mice",
  109. "sheep": "sheep",
  110. "Sheep": "Sheep",
  111. "deer": "deer",
  112. "Deer": "Deer",
  113. "fish": "fish",
  114. "Fish": "Fish",
  115. "child": "children",
  116. "Child": "Children",
  117. "ox": "oxen",
  118. "Ox": "Oxen",
  119. "information": "information",
  120. "Information": "Information",
  121. "info": "infos",
  122. "Info": "Infos",
  123. }
  124. func Plural(s string) string {
  125. for specialPluralWord, pluralSpecialPluralWord := range specialPluralWordMap {
  126. if strings.HasSuffix(s, specialPluralWord) {
  127. s = strings.TrimRight(s, specialPluralWord)
  128. s = s + pluralSpecialPluralWord
  129. return s
  130. }
  131. }
  132. if strings.HasSuffix(s, "s") || strings.HasSuffix(s, "x") ||
  133. strings.HasSuffix(s, "sh") || strings.HasSuffix(s, "ch") ||
  134. strings.HasSuffix(s, "o") {
  135. return s + "es"
  136. } else if strings.HasSuffix(s, "f") {
  137. return s[:len(s)-1] + "ves"
  138. } else if strings.HasSuffix(s, "fe") {
  139. return s[:len(s)-2] + "ves"
  140. } else if strings.HasSuffix(s, "y") {
  141. return s[:len(s)-1] + "ies"
  142. } else {
  143. return s + "s"
  144. }
  145. }