genpacket.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package tsgen
  2. import (
  3. "errors"
  4. "fmt"
  5. "path"
  6. "strings"
  7. "text/template"
  8. "github.com/tal-tech/go-zero/tools/goctl/api/spec"
  9. apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
  10. "github.com/tal-tech/go-zero/tools/goctl/util"
  11. )
  12. const (
  13. handlerTemplate = `{{.imports}}
  14. {{.types}}
  15. {{.apis}}
  16. `
  17. )
  18. func genHandler(dir, webApi, caller string, api *spec.ApiSpec, unwrapApi bool) error {
  19. filename := strings.Replace(api.Service.Name, "-api", "", 1) + ".ts"
  20. if err := util.RemoveIfExist(path.Join(dir, filename)); err != nil {
  21. return err
  22. }
  23. fp, created, err := apiutil.MaybeCreateFile(dir, "", filename)
  24. if err != nil {
  25. return err
  26. }
  27. if !created {
  28. return nil
  29. }
  30. defer fp.Close()
  31. var localTypes []spec.Type
  32. for _, route := range api.Service.Routes() {
  33. rts := apiutil.GetLocalTypes(api, route)
  34. localTypes = append(localTypes, rts...)
  35. }
  36. var prefixForType = func(ty string) string {
  37. if _, pri := primitiveType(ty); pri {
  38. return ""
  39. }
  40. for _, item := range localTypes {
  41. if util.Title(item.Name) == ty {
  42. return ""
  43. }
  44. }
  45. return packagePrefix
  46. }
  47. types, err := genTypes(localTypes, func(name string) (*spec.Type, error) {
  48. for _, ty := range api.Types {
  49. if strings.ToLower(ty.Name) == strings.ToLower(name) {
  50. return &ty, nil
  51. }
  52. }
  53. return nil, errors.New("inline type " + name + " not exist, please correct api file")
  54. }, prefixForType)
  55. if err != nil {
  56. return err
  57. }
  58. imports := ""
  59. if len(caller) == 0 {
  60. caller = "webapi"
  61. }
  62. importCaller := caller
  63. if unwrapApi {
  64. importCaller = "{ " + importCaller + " }"
  65. }
  66. if len(webApi) > 0 {
  67. imports += `import ` + importCaller + ` from ` + "\"" + webApi + "\""
  68. }
  69. shardTypes := apiutil.GetSharedTypes(api)
  70. if len(shardTypes) != 0 {
  71. if len(imports) > 0 {
  72. imports += "\n"
  73. }
  74. outputFile := apiutil.ComponentName(api)
  75. imports += fmt.Sprintf(`import * as components from "%s"`, "./"+outputFile)
  76. }
  77. apis, err := genApi(api, caller, prefixForType)
  78. if err != nil {
  79. return err
  80. }
  81. t := template.Must(template.New("handlerTemplate").Parse(handlerTemplate))
  82. return t.Execute(fp, map[string]string{
  83. "webApi": webApi,
  84. "types": strings.TrimSpace(types),
  85. "imports": imports,
  86. "apis": strings.TrimSpace(apis),
  87. })
  88. }
  89. func genTypes(localTypes []spec.Type, inlineType func(string) (*spec.Type, error), prefixForType func(string) string) (string, error) {
  90. var builder strings.Builder
  91. var first bool
  92. for _, tp := range localTypes {
  93. if first {
  94. first = false
  95. } else {
  96. fmt.Fprintln(&builder)
  97. }
  98. if err := writeType(&builder, tp, func(name string) (s *spec.Type, err error) {
  99. return inlineType(name)
  100. }, prefixForType); err != nil {
  101. return "", err
  102. }
  103. }
  104. types := builder.String()
  105. return types, nil
  106. }
  107. func genApi(api *spec.ApiSpec, caller string, prefixForType func(string) string) (string, error) {
  108. var builder strings.Builder
  109. for _, group := range api.Service.Groups {
  110. for _, route := range group.Routes {
  111. handler, ok := apiutil.GetAnnotationValue(route.Annotations, "server", "handler")
  112. if !ok {
  113. return "", fmt.Errorf("missing handler annotation for route %q", route.Path)
  114. }
  115. handler = util.Untitle(handler)
  116. handler = strings.Replace(handler, "Handler", "", 1)
  117. comment := commentForRoute(route)
  118. if len(comment) > 0 {
  119. fmt.Fprintf(&builder, "%s\n", comment)
  120. }
  121. fmt.Fprintf(&builder, "export function %s(%s) {\n", handler, paramsForRoute(route, prefixForType))
  122. writeIndent(&builder, 1)
  123. responseGeneric := "<null>"
  124. if len(route.ResponseType.Name) > 0 {
  125. val, err := goTypeToTs(route.ResponseType.Name, prefixForType)
  126. if err != nil {
  127. return "", err
  128. }
  129. responseGeneric = fmt.Sprintf("<%s>", val)
  130. }
  131. fmt.Fprintf(&builder, `return %s.%s%s(%s)`, caller, strings.ToLower(route.Method),
  132. util.Title(responseGeneric), callParamsForRoute(route, group))
  133. builder.WriteString("\n}\n\n")
  134. }
  135. }
  136. apis := builder.String()
  137. return apis, nil
  138. }
  139. func paramsForRoute(route spec.Route, prefixForType func(string) string) string {
  140. hasParams := pathHasParams(route)
  141. hasBody := hasRequestBody(route)
  142. rt, err := goTypeToTs(route.RequestType.Name, prefixForType)
  143. if err != nil {
  144. fmt.Println(err.Error())
  145. return ""
  146. }
  147. if hasParams && hasBody {
  148. return fmt.Sprintf("params: %s, req: %s", rt+"Params", rt)
  149. } else if hasParams {
  150. return fmt.Sprintf("params: %s", rt+"Params")
  151. } else if hasBody {
  152. return fmt.Sprintf("req: %s", rt)
  153. }
  154. return ""
  155. }
  156. func commentForRoute(route spec.Route) string {
  157. var builder strings.Builder
  158. comment, _ := apiutil.GetAnnotationValue(route.Annotations, "doc", "summary")
  159. builder.WriteString("/**")
  160. builder.WriteString("\n * @description " + comment)
  161. hasParams := pathHasParams(route)
  162. hasBody := hasRequestBody(route)
  163. if hasParams && hasBody {
  164. builder.WriteString("\n * @param params")
  165. builder.WriteString("\n * @param req")
  166. } else if hasParams {
  167. builder.WriteString("\n * @param params")
  168. } else if hasBody {
  169. builder.WriteString("\n * @param req")
  170. }
  171. builder.WriteString("\n */")
  172. return builder.String()
  173. }
  174. func callParamsForRoute(route spec.Route, group spec.Group) string {
  175. hasParams := pathHasParams(route)
  176. hasBody := hasRequestBody(route)
  177. if hasParams && hasBody {
  178. return fmt.Sprintf("%s, %s, %s", pathForRoute(route, group), "params", "req")
  179. } else if hasParams {
  180. return fmt.Sprintf("%s, %s", pathForRoute(route, group), "params")
  181. } else if hasBody {
  182. return fmt.Sprintf("%s, %s", pathForRoute(route, group), "req")
  183. }
  184. return pathForRoute(route, group)
  185. }
  186. func pathForRoute(route spec.Route, group spec.Group) string {
  187. value, ok := apiutil.GetAnnotationValue(group.Annotations, "server", pathPrefix)
  188. if !ok {
  189. return "\"" + route.Path + "\""
  190. } else {
  191. value = strings.TrimPrefix(value, `"`)
  192. value = strings.TrimSuffix(value, `"`)
  193. return fmt.Sprintf(`"%s/%s"`, value, strings.TrimPrefix(route.Path, "/"))
  194. }
  195. }
  196. func pathHasParams(route spec.Route) bool {
  197. return len(route.RequestType.Members) != len(route.RequestType.GetBodyMembers())
  198. }
  199. func hasRequestBody(route spec.Route) bool {
  200. return len(route.RequestType.Name) > 0 && len(route.RequestType.GetBodyMembers()) > 0
  201. }