template_func.go 3.3 KB

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