template_func.go 3.3 KB

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