util.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package dartgen
  2. import (
  3. "os"
  4. "strings"
  5. "github.com/tal-tech/go-zero/tools/goctl/api/spec"
  6. "github.com/tal-tech/go-zero/tools/goctl/api/util"
  7. )
  8. func lowCamelCase(s string) string {
  9. if len(s) < 1 {
  10. return ""
  11. }
  12. s = util.ToCamelCase(util.ToSnakeCase(s))
  13. return util.ToLower(s[:1]) + s[1:]
  14. }
  15. func pathToFuncName(path string) string {
  16. if !strings.HasPrefix(path, "/") {
  17. path = "/" + path
  18. }
  19. if !strings.HasPrefix(path, "/api") {
  20. path = "/api" + path
  21. }
  22. path = strings.Replace(path, "/", "_", -1)
  23. path = strings.Replace(path, "-", "_", -1)
  24. camel := util.ToCamelCase(path)
  25. return util.ToLower(camel[:1]) + camel[1:]
  26. }
  27. func tagGet(tag, k string) string {
  28. tags, err := spec.Parse(tag)
  29. if err != nil {
  30. panic(k + " not exist")
  31. }
  32. v, err := tags.Get(k)
  33. if err != nil {
  34. panic(k + " value not exist")
  35. }
  36. return v.Name
  37. }
  38. func isDirectType(s string) bool {
  39. return isAtomicType(s) || isListType(s) && isAtomicType(getCoreType(s))
  40. }
  41. func isAtomicType(s string) bool {
  42. switch s {
  43. case "String", "int", "double", "bool":
  44. return true
  45. default:
  46. return false
  47. }
  48. }
  49. func isListType(s string) bool {
  50. return strings.HasPrefix(s, "List<")
  51. }
  52. func isClassListType(s string) bool {
  53. return strings.HasPrefix(s, "List<") && !isAtomicType(getCoreType(s))
  54. }
  55. func getCoreType(s string) string {
  56. if isAtomicType(s) {
  57. return s
  58. }
  59. if isListType(s) {
  60. s = strings.Replace(s, "List<", "", -1)
  61. return strings.Replace(s, ">", "", -1)
  62. }
  63. return s
  64. }
  65. func fileExists(path string) bool {
  66. _, err := os.Stat(path)
  67. return !os.IsNotExist(err)
  68. }