util.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package dartgen
  2. import (
  3. "os"
  4. "reflect"
  5. "strings"
  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) (reflect.Value, error) {
  28. v, _ := util.TagLookup(tag, k)
  29. out := strings.Split(v, ",")[0]
  30. return reflect.ValueOf(out), nil
  31. }
  32. func isDirectType(s string) bool {
  33. return isAtomicType(s) || isListType(s) && isAtomicType(getCoreType(s))
  34. }
  35. func isAtomicType(s string) bool {
  36. switch s {
  37. case "String", "int", "double", "bool":
  38. return true
  39. default:
  40. return false
  41. }
  42. }
  43. func isListType(s string) bool {
  44. return strings.HasPrefix(s, "List<")
  45. }
  46. func isClassListType(s string) bool {
  47. return strings.HasPrefix(s, "List<") && !isAtomicType(getCoreType(s))
  48. }
  49. func getCoreType(s string) string {
  50. if isAtomicType(s) {
  51. return s
  52. }
  53. if isListType(s) {
  54. s = strings.Replace(s, "List<", "", -1)
  55. return strings.Replace(s, ">", "", -1)
  56. }
  57. return s
  58. }
  59. func fileExists(path string) bool {
  60. _, err := os.Stat(path)
  61. return !os.IsNotExist(err)
  62. }