funcs.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package ktgen
  2. import (
  3. "log"
  4. "strings"
  5. "text/template"
  6. "github.com/tal-tech/go-zero/tools/goctl/api/util"
  7. )
  8. var funcsMap = template.FuncMap{
  9. "lowCamelCase": lowCamelCase,
  10. "pathToFuncName": pathToFuncName,
  11. "parseType": parseType,
  12. "add": add,
  13. }
  14. func lowCamelCase(s string) string {
  15. if len(s) < 1 {
  16. return ""
  17. }
  18. s = util.ToCamelCase(util.ToSnakeCase(s))
  19. return util.ToLower(s[:1]) + s[1:]
  20. }
  21. func pathToFuncName(path string) string {
  22. if !strings.HasPrefix(path, "/") {
  23. path = "/" + path
  24. }
  25. path = strings.ReplaceAll(path, "/", "_")
  26. path = strings.ReplaceAll(path, "-", "_")
  27. camel := util.ToCamelCase(path)
  28. return util.ToLower(camel[:1]) + camel[1:]
  29. }
  30. func parseType(t string) string {
  31. t = strings.Replace(t, "*", "", -1)
  32. if strings.HasPrefix(t, "[]") {
  33. return "List<" + parseType(t[2:]) + ">"
  34. }
  35. if strings.HasPrefix(t, "map") {
  36. tys, e := util.DecomposeType(t)
  37. if e != nil {
  38. log.Fatal(e)
  39. }
  40. if len(tys) != 2 {
  41. log.Fatal("Map type number !=2")
  42. }
  43. return "Map<String," + parseType(tys[1]) + ">"
  44. }
  45. switch t {
  46. case "string":
  47. return "String"
  48. case "int", "int32", "int64":
  49. return "Int"
  50. case "float", "float32", "float64":
  51. return "Double"
  52. case "bool":
  53. return "Boolean"
  54. default:
  55. return t
  56. }
  57. }
  58. func add(a, i int) int {
  59. return a + i
  60. }