util.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package dartgen
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path"
  7. "strings"
  8. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/api/spec"
  9. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/api/util"
  10. )
  11. const (
  12. formTagKey = "form"
  13. pathTagKey = "path"
  14. headerTagKey = "header"
  15. )
  16. func normalizeHandlerName(handlerName string) string {
  17. handler := strings.Replace(handlerName, "Handler", "", 1)
  18. handler = lowCamelCase(handler)
  19. return handler
  20. }
  21. func lowCamelCase(s string) string {
  22. if len(s) < 1 {
  23. return ""
  24. }
  25. s = util.ToCamelCase(util.ToSnakeCase(s))
  26. return util.ToLower(s[:1]) + s[1:]
  27. }
  28. func getBaseName(str string) string {
  29. return path.Base(str)
  30. }
  31. func getPropertyFromMember(member spec.Member) string {
  32. name, err := member.GetPropertyName()
  33. if err != nil {
  34. panic(fmt.Sprintf("cannot get property name of %q", member.Name))
  35. }
  36. return 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 isNumberType(s string) bool {
  50. switch s {
  51. case "int", "double":
  52. return true
  53. default:
  54. return false
  55. }
  56. }
  57. func isListType(s string) bool {
  58. return strings.HasPrefix(s, "List<")
  59. }
  60. func isClassListType(s string) bool {
  61. return isListType(s) && !isAtomicType(getCoreType(s))
  62. }
  63. func isAtomicListType(s string) bool {
  64. return isListType(s) && isAtomicType(getCoreType(s))
  65. }
  66. func isListItemsNullable(s string) bool {
  67. return isListType(s) && isNullableType(getCoreType(s))
  68. }
  69. func isMapType(s string) bool {
  70. return strings.HasPrefix(s, "Map<")
  71. }
  72. // Only interface types are nullable
  73. func isNullableType(s string) bool {
  74. return strings.HasSuffix(s, "?")
  75. }
  76. func appendNullCoalescing(member spec.Member) string {
  77. if isNullableType(member.Type.Name()) {
  78. return "m['" + getPropertyFromMember(member) + "'] == null ? null : "
  79. }
  80. return ""
  81. }
  82. // To be compatible with omitempty tags in Golang
  83. // Only set default value for non-nullable types
  84. func appendDefaultEmptyValue(s string) string {
  85. if isNullableType(s) {
  86. return ""
  87. }
  88. if isAtomicType(s) {
  89. switch s {
  90. case "String":
  91. return `?? ""`
  92. case "int":
  93. return "?? 0"
  94. case "double":
  95. return "?? 0.0"
  96. case "bool":
  97. return "?? false"
  98. default:
  99. panic(errors.New("unknown atomic type"))
  100. }
  101. }
  102. if isListType(s) {
  103. return "?? []"
  104. }
  105. if isMapType(s) {
  106. return "?? {}"
  107. }
  108. return ""
  109. }
  110. func getCoreType(s string) string {
  111. if isAtomicType(s) {
  112. return s
  113. }
  114. if isListType(s) {
  115. s = strings.Replace(s, "List<", "", -1)
  116. return strings.Replace(s, ">", "", -1)
  117. }
  118. return s
  119. }
  120. func fileExists(path string) bool {
  121. _, err := os.Stat(path)
  122. return !os.IsNotExist(err)
  123. }
  124. func buildSpecType(tp spec.Type, name string) spec.Type {
  125. switch v := tp.(type) {
  126. case spec.PrimitiveType:
  127. return spec.PrimitiveType{RawName: name}
  128. case spec.MapType:
  129. return spec.MapType{RawName: name, Key: v.Key, Value: v.Value}
  130. case spec.ArrayType:
  131. return spec.ArrayType{RawName: name, Value: v.Value}
  132. case spec.InterfaceType:
  133. return spec.InterfaceType{RawName: name}
  134. case spec.PointerType:
  135. return spec.PointerType{RawName: name, Type: v.Type}
  136. }
  137. return tp
  138. }
  139. func specTypeToDart(tp spec.Type) (string, error) {
  140. switch v := tp.(type) {
  141. case spec.DefineStruct:
  142. return tp.Name(), nil
  143. case spec.PrimitiveType:
  144. r, ok := primitiveType(tp.Name())
  145. if !ok {
  146. return "", errors.New("unsupported primitive type " + tp.Name())
  147. }
  148. return r, nil
  149. case spec.MapType:
  150. valueType, err := specTypeToDart(v.Value)
  151. if err != nil {
  152. return "", err
  153. }
  154. return fmt.Sprintf("Map<String, %s>", valueType), nil
  155. case spec.ArrayType:
  156. if tp.Name() == "[]byte" {
  157. return "List<int>", nil
  158. }
  159. valueType, err := specTypeToDart(v.Value)
  160. if err != nil {
  161. return "", err
  162. }
  163. s := getBaseType(valueType)
  164. if len(s) != 0 {
  165. return s, nil
  166. }
  167. return fmt.Sprintf("List<%s>", valueType), nil
  168. case spec.InterfaceType:
  169. return "Object?", nil
  170. case spec.PointerType:
  171. valueType, err := specTypeToDart(v.Type)
  172. if err != nil {
  173. return "", err
  174. }
  175. return fmt.Sprintf("%s?", valueType), nil
  176. }
  177. return "", errors.New("unsupported primitive type " + tp.Name())
  178. }
  179. func getBaseType(valueType string) string {
  180. switch valueType {
  181. case "int":
  182. return "List<int>"
  183. case "double":
  184. return "List<double>"
  185. case "boolean":
  186. return "List<bool>"
  187. case "String":
  188. return "List<String>"
  189. default:
  190. return ""
  191. }
  192. }
  193. func primitiveType(tp string) (string, bool) {
  194. switch tp {
  195. case "string":
  196. return "String", true
  197. case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64", "rune":
  198. return "int", true
  199. case "float32", "float64":
  200. return "double", true
  201. case "bool":
  202. return "bool", true
  203. }
  204. return "", false
  205. }
  206. func hasUrlPathParams(route spec.Route) bool {
  207. ds, ok := route.RequestType.(spec.DefineStruct)
  208. if !ok {
  209. return false
  210. }
  211. return len(route.RequestTypeName()) > 0 && len(ds.GetTagMembers(pathTagKey)) > 0
  212. }
  213. func extractPositionalParamsFromPath(route spec.Route) string {
  214. ds, ok := route.RequestType.(spec.DefineStruct)
  215. if !ok {
  216. return ""
  217. }
  218. var params []string
  219. for _, member := range ds.GetTagMembers(pathTagKey) {
  220. dartType := member.Type.Name()
  221. params = append(params, fmt.Sprintf("%s %s", dartType, getPropertyFromMember(member)))
  222. }
  223. return strings.Join(params, ", ")
  224. }
  225. func makeDartRequestUrlPath(route spec.Route) string {
  226. path := route.Path
  227. if route.RequestType == nil {
  228. return `"` + path + `"`
  229. }
  230. ds, ok := route.RequestType.(spec.DefineStruct)
  231. if !ok {
  232. return path
  233. }
  234. for _, member := range ds.GetTagMembers(pathTagKey) {
  235. paramName := member.Tags()[0].Name
  236. path = strings.ReplaceAll(path, ":"+paramName, "${"+getPropertyFromMember(member)+"}")
  237. }
  238. return `"` + path + `"`
  239. }