You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/api/ktgen/funcs.go

78 lines
1.4 KiB
Go

package ktgen
import (
"log"
"strings"
"text/template"
4 years ago
"github.com/iancoleman/strcase"
4 years ago
"github.com/tal-tech/go-zero/tools/goctl/api/util"
)
4 years ago
var funcsMap = template.FuncMap{
"lowCamelCase": lowCamelCase,
"routeToFuncName": routeToFuncName,
"parseType": parseType,
"add": add,
"upperCase": upperCase,
}
4 years ago
func lowCamelCase(s string) string {
if len(s) < 1 {
return ""
}
s = util.ToCamelCase(util.ToSnakeCase(s))
return util.ToLower(s[:1]) + s[1:]
}
func routeToFuncName(method, path string) string {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
4 years ago
path = strings.ReplaceAll(path, "/", "_")
path = strings.ReplaceAll(path, "-", "_")
path = strings.ReplaceAll(path, ":", "With_")
return strings.ToLower(method)+strcase.ToCamel(path)
}
4 years ago
func parseType(t string) string {
4 years ago
t = strings.Replace(t, "*", "", -1)
if strings.HasPrefix(t, "[]") {
return "List<" + parseType(t[2:]) + ">"
}
4 years ago
if strings.HasPrefix(t, "map") {
tys, e := util.DecomposeType(t)
if e != nil {
log.Fatal(e)
}
4 years ago
if len(tys) != 2 {
log.Fatal("Map type number !=2")
}
4 years ago
return "Map<String," + parseType(tys[1]) + ">"
}
switch t {
case "string":
return "String"
case "int", "int32", "int64":
return "Int"
case "float", "float32", "float64":
return "Double"
case "bool":
return "Boolean"
default:
return t
}
}
4 years ago
func add(a, i int) int {
return a + i
}
func upperCase(s string) string {
return strings.ToUpper(s)
}