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" } }