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") { return strings.ReplaceAll(s, "ID", "Id") } if strings.HasSuffix(s, "id") { return strings.ReplaceAll(s, "id", "Id") } if strings.HasSuffix(s, "iD") { return strings.ReplaceAll(s, "iD", "Id") } return s } func ID(s string) string { if strings.HasSuffix(s, "Id") { return strings.ReplaceAll(s, "Id", "ID") } if strings.HasSuffix(s, "id") { return strings.ReplaceAll(s, "id", "ID") } if strings.HasSuffix(s, "iD") { return strings.ReplaceAll(s, "iD", "ID") } return s } func id(s string) string { if strings.HasSuffix(s, "Id") { return strings.ReplaceAll(s, "Id", "id") } if strings.HasSuffix(s, "ID") { return strings.ReplaceAll(s, "ID", "id") } if strings.HasSuffix(s, "iD") { return strings.ReplaceAll(s, "iD", "id") } 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" } }