util.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package dartgen
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path"
  7. "strings"
  8. "github.com/zeromicro/go-zero/tools/goctl/api/spec"
  9. "github.com/zeromicro/go-zero/tools/goctl/api/util"
  10. )
  11. func lowCamelCase(s string) string {
  12. if len(s) < 1 {
  13. return ""
  14. }
  15. s = util.ToCamelCase(util.ToSnakeCase(s))
  16. return util.ToLower(s[:1]) + s[1:]
  17. }
  18. func pathToFuncName(path string) string {
  19. if !strings.HasPrefix(path, "/") {
  20. path = "/" + path
  21. }
  22. if !strings.HasPrefix(path, "/api") {
  23. path = "/api" + 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 getBaseName(str string) string {
  31. return path.Base(str)
  32. }
  33. func getPropertyFromMember(member spec.Member) string {
  34. name, err := member.GetPropertyName()
  35. if err != nil {
  36. panic(fmt.Sprintf("cannot get property name of %q", member.Name))
  37. }
  38. return name
  39. }
  40. func isDirectType(s string) bool {
  41. return isAtomicType(s) || isListType(s) && isAtomicType(getCoreType(s))
  42. }
  43. func isAtomicType(s string) bool {
  44. switch s {
  45. case "String", "int", "double", "bool":
  46. return true
  47. default:
  48. return false
  49. }
  50. }
  51. func isListType(s string) bool {
  52. return strings.HasPrefix(s, "List<")
  53. }
  54. func isClassListType(s string) bool {
  55. return strings.HasPrefix(s, "List<") && !isAtomicType(getCoreType(s))
  56. }
  57. func getCoreType(s string) string {
  58. if isAtomicType(s) {
  59. return s
  60. }
  61. if isListType(s) {
  62. s = strings.Replace(s, "List<", "", -1)
  63. return strings.Replace(s, ">", "", -1)
  64. }
  65. return s
  66. }
  67. func fileExists(path string) bool {
  68. _, err := os.Stat(path)
  69. return !os.IsNotExist(err)
  70. }
  71. func buildSpecType(tp spec.Type, name string) spec.Type {
  72. switch v := tp.(type) {
  73. case spec.PrimitiveType:
  74. return spec.PrimitiveType{RawName: name}
  75. case spec.MapType:
  76. return spec.MapType{RawName: name, Key: v.Key, Value: v.Value}
  77. case spec.ArrayType:
  78. return spec.ArrayType{RawName: name, Value: v.Value}
  79. case spec.InterfaceType:
  80. return spec.InterfaceType{RawName: name}
  81. case spec.PointerType:
  82. return spec.PointerType{RawName: name, Type: v.Type}
  83. }
  84. return tp
  85. }
  86. func specTypeToDart(tp spec.Type) (string, error) {
  87. switch v := tp.(type) {
  88. case spec.DefineStruct:
  89. return tp.Name(), nil
  90. case spec.PrimitiveType:
  91. r, ok := primitiveType(tp.Name())
  92. if !ok {
  93. return "", errors.New("unsupported primitive type " + tp.Name())
  94. }
  95. return r, nil
  96. case spec.MapType:
  97. valueType, err := specTypeToDart(v.Value)
  98. if err != nil {
  99. return "", err
  100. }
  101. return fmt.Sprintf("Map<String, %s>", valueType), nil
  102. case spec.ArrayType:
  103. if tp.Name() == "[]byte" {
  104. return "List<int>", nil
  105. }
  106. valueType, err := specTypeToDart(v.Value)
  107. if err != nil {
  108. return "", err
  109. }
  110. s := getBaseType(valueType)
  111. if len(s) != 0 {
  112. return s, nil
  113. }
  114. return fmt.Sprintf("List<%s>", valueType), nil
  115. case spec.InterfaceType:
  116. return "Object", nil
  117. case spec.PointerType:
  118. return specTypeToDart(v.Type)
  119. }
  120. return "", errors.New("unsupported primitive type " + tp.Name())
  121. }
  122. func getBaseType(valueType string) string {
  123. switch valueType {
  124. case "int":
  125. return "List<int>"
  126. case "double":
  127. return "List<double>"
  128. case "boolean":
  129. return "List<bool>"
  130. case "String":
  131. return "List<String>"
  132. default:
  133. return ""
  134. }
  135. }
  136. func primitiveType(tp string) (string, bool) {
  137. switch tp {
  138. case "string":
  139. return "String", true
  140. case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64", "rune":
  141. return "int", true
  142. case "float32", "float64":
  143. return "double", true
  144. case "bool":
  145. return "bool", true
  146. }
  147. return "", false
  148. }