123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- package template
- import (
- "github.com/Masterminds/sprig/v3"
- "strings"
- )
- var templateFuncMap = map[string]any{
- "leftLower": LeftLower,
- "leftUpper": LeftUpper,
- "upperShort": UpperShort,
- "Id": Id,
- "ID": ID,
- "id": id,
- "plural": Plural,
- "toUpper": strings.ToUpper,
- }
- func LeftLower(s string) string {
- if len(s) == 0 {
- return s
- }
- if strings.HasPrefix(s, "ID") {
- return strings.Replace(s, "ID", "id", 1)
- }
- return strings.ToLower(string(s[0])) + s[1:]
- }
- func LeftUpper(s string) string {
- if len(s) == 0 {
- return s
- }
- if strings.HasPrefix(s, "id") {
- return strings.Replace(s, "id", "ID", 1)
- }
- return strings.ToUpper(string(s[0])) + s[1:]
- }
- func SnakeCase(s string) string {
- return sprig.FuncMap()["snakecase"].(func(string) string)(s)
- }
- func UpperShort(s string) string {
- if len(s) == 0 {
- return s
- }
- serviceWords := strings.Split(SnakeCase(s), "_")
- if len(serviceWords) == 1 {
- return strings.ToUpper(serviceWords[0])[:0]
- }
- var short string
- for _, serviceWord := range serviceWords {
- short = short + strings.ToUpper(string(serviceWord[0]))
- }
- return short
- }
- func Id(s string) string {
- if strings.HasSuffix(s, "ID") {
- index := strings.LastIndex(s, "ID")
- return s[0:index] + "Id"
- }
- if strings.HasSuffix(s, "id") {
- index := strings.LastIndex(s, "id")
- return s[0:index] + "Id"
- }
- if strings.HasSuffix(s, "iD") {
- index := strings.LastIndex(s, "iD")
- return s[0:index] + "Id"
- }
- if strings.HasSuffix(s, "IDs") {
- index := strings.LastIndex(s, "IDs")
- return s[0:index] + "Ids"
- }
- if strings.HasSuffix(s, "ids") {
- index := strings.LastIndex(s, "ids")
- return s[0:index] + "Ids"
- }
- if strings.HasSuffix(s, "iDs") {
- index := strings.LastIndex(s, "iDs")
- return s[0:index] + "Ids"
- }
- return s
- }
- func ID(s string) string {
- if strings.HasSuffix(s, "Id") {
- index := strings.LastIndex(s, "Id")
- return s[0:index] + "ID"
- }
- if strings.HasSuffix(s, "id") {
- index := strings.LastIndex(s, "id")
- return s[0:index] + "ID"
- }
- if strings.HasSuffix(s, "iD") {
- index := strings.LastIndex(s, "iD")
- return s[0:index] + "ID"
- }
- if strings.HasSuffix(s, "Ids") {
- index := strings.LastIndex(s, "Ids")
- return s[0:index] + "IDs"
- }
- if strings.HasSuffix(s, "ids") {
- index := strings.LastIndex(s, "ids")
- return s[0:index] + "IDs"
- }
- if strings.HasSuffix(s, "iDs") {
- index := strings.LastIndex(s, "iDs")
- return s[0:index] + "IDs"
- }
- return s
- }
- func id(s string) string {
- if strings.HasSuffix(s, "Id") {
- index := strings.LastIndex(s, "Id")
- return s[0:index] + "id"
- }
- if strings.HasSuffix(s, "ID") {
- index := strings.LastIndex(s, "ID")
- return s[0:index] + "id"
- }
- if strings.HasSuffix(s, "iD") {
- index := strings.LastIndex(s, "iD")
- return s[0:index] + "id"
- }
- if strings.HasSuffix(s, "Ids") {
- index := strings.LastIndex(s, "Ids")
- return s[0:index] + "ids"
- }
- if strings.HasSuffix(s, "IDs") {
- index := strings.LastIndex(s, "IDs")
- return s[0:index] + "ids"
- }
- if strings.HasSuffix(s, "iDs") {
- index := strings.LastIndex(s, "iDs")
- return s[0:index] + "ids"
- }
- return s
- }
- var specialPluralWordMap = map[string]string{
- "goose": "geese",
- "Goose": "Geese",
- "foot": "feet",
- "Foot": "Feet",
- "tooth": "teeth",
- "Tooth": "Teeth",
- "man": "men",
- "Man": "Men",
- "woman": "women",
- "Woman": "Women",
- "mouse": "mice",
- "Mouse": "Mice",
- "sheep": "sheep",
- "Sheep": "Sheep",
- "deer": "deer",
- "Deer": "Deer",
- "fish": "fish",
- "Fish": "Fish",
- "child": "children",
- "Child": "Children",
- "ox": "oxen",
- "Ox": "Oxen",
- "information": "information",
- "Information": "Information",
- "info": "infos",
- "Info": "Infos",
- }
- func Plural(s string) string {
- for specialPluralWord, pluralSpecialPluralWord := range specialPluralWordMap {
- if strings.HasSuffix(s, specialPluralWord) {
- s = strings.TrimRight(s, specialPluralWord)
- s = s + pluralSpecialPluralWord
- return s
- }
- }
- if strings.HasSuffix(s, "s") || strings.HasSuffix(s, "x") ||
- strings.HasSuffix(s, "sh") || strings.HasSuffix(s, "ch") ||
- strings.HasSuffix(s, "o") {
- return s + "es"
- } else if strings.HasSuffix(s, "f") {
- return s[:len(s)-1] + "ves"
- } else if strings.HasSuffix(s, "fe") {
- return s[:len(s)-2] + "ves"
- } else if strings.HasSuffix(s, "y") {
- return s[:len(s)-1] + "ies"
- } else {
- return s + "s"
- }
- }
|