funcs.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package ktgen
  2. import (
  3. "github.com/tal-tech/go-zero/tools/goctl/api/util"
  4. "log"
  5. "strings"
  6. "text/template"
  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.Replace(path, "/", "_", -1)
  26. path = strings.Replace(path, "-", "_", -1)
  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. }