util.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package dartgen
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "github.com/tal-tech/go-zero/tools/goctl/api/spec"
  8. "github.com/tal-tech/go-zero/tools/goctl/api/util"
  9. )
  10. func lowCamelCase(s string) string {
  11. if len(s) < 1 {
  12. return ""
  13. }
  14. s = util.ToCamelCase(util.ToSnakeCase(s))
  15. return util.ToLower(s[:1]) + s[1:]
  16. }
  17. func pathToFuncName(path string) string {
  18. if !strings.HasPrefix(path, "/") {
  19. path = "/" + path
  20. }
  21. if !strings.HasPrefix(path, "/api") {
  22. path = "/api" + path
  23. }
  24. path = strings.Replace(path, "/", "_", -1)
  25. path = strings.Replace(path, "-", "_", -1)
  26. camel := util.ToCamelCase(path)
  27. return util.ToLower(camel[:1]) + camel[1:]
  28. }
  29. func tagGet(tag, k string) string {
  30. tags, err := spec.Parse(tag)
  31. if err != nil {
  32. panic(k + " not exist")
  33. }
  34. v, err := tags.Get(k)
  35. if err != nil {
  36. panic(k + " value not exist")
  37. }
  38. return v.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, errors.New("unsupported primitive type " + tp.Name())
  113. }
  114. return s, 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. }