genpacket.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package tsgen
  2. import (
  3. "errors"
  4. "fmt"
  5. "path"
  6. "strings"
  7. "text/template"
  8. "zero/tools/goctl/api/spec"
  9. apiutil "zero/tools/goctl/api/util"
  10. "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, localTypes, 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, localTypes []spec.Type, caller string, prefixForType func(string) string) (string, error) {
  108. var builder strings.Builder
  109. for _, route := range api.Service.Routes {
  110. handler, ok := apiutil.GetAnnotationValue(route.Annotations, "server", "handler")
  111. if !ok {
  112. return "", fmt.Errorf("missing handler annotation for route %q", route.Path)
  113. }
  114. handler = util.Untitle(handler)
  115. handler = strings.Replace(handler, "Handler", "", 1)
  116. comment := commentForRoute(route)
  117. if len(comment) > 0 {
  118. fmt.Fprintf(&builder, "%s\n", comment)
  119. }
  120. fmt.Fprintf(&builder, "export function %s(%s) {\n", handler, paramsForRoute(route, prefixForType))
  121. writeIndent(&builder, 1)
  122. responseGeneric := "<null>"
  123. if len(route.ResponseType.Name) > 0 {
  124. val, err := goTypeToTs(route.ResponseType.Name, prefixForType)
  125. if err != nil {
  126. return "", err
  127. }
  128. responseGeneric = fmt.Sprintf("<%s>", val)
  129. }
  130. fmt.Fprintf(&builder, `return %s.%s%s(%s)`, caller, strings.ToLower(route.Method),
  131. util.Title(responseGeneric), callParamsForRoute(route))
  132. builder.WriteString("\n}\n\n")
  133. }
  134. apis := builder.String()
  135. return apis, nil
  136. }
  137. func paramsForRoute(route spec.Route, prefixForType func(string) string) string {
  138. hasParams := pathHasParams(route)
  139. hasBody := hasRequestBody(route)
  140. rt, err := goTypeToTs(route.RequestType.Name, prefixForType)
  141. if err != nil {
  142. println(err.Error())
  143. return ""
  144. }
  145. if hasParams && hasBody {
  146. return fmt.Sprintf("params: %s, req: %s", rt+"Params", rt)
  147. } else if hasParams {
  148. return fmt.Sprintf("params: %s", rt+"Params")
  149. } else if hasBody {
  150. return fmt.Sprintf("req: %s", rt)
  151. }
  152. return ""
  153. }
  154. func commentForRoute(route spec.Route) string {
  155. var builder strings.Builder
  156. comment, _ := apiutil.GetAnnotationValue(route.Annotations, "doc", "summary")
  157. builder.WriteString("/**")
  158. builder.WriteString("\n * @description " + comment)
  159. hasParams := pathHasParams(route)
  160. hasBody := hasRequestBody(route)
  161. if hasParams && hasBody {
  162. builder.WriteString("\n * @param params")
  163. builder.WriteString("\n * @param req")
  164. } else if hasParams {
  165. builder.WriteString("\n * @param params")
  166. } else if hasBody {
  167. builder.WriteString("\n * @param req")
  168. }
  169. builder.WriteString("\n */")
  170. return builder.String()
  171. }
  172. func callParamsForRoute(route spec.Route) string {
  173. hasParams := pathHasParams(route)
  174. hasBody := hasRequestBody(route)
  175. if hasParams && hasBody {
  176. return fmt.Sprintf("%s, %s, %s", pathForRoute(route), "params", "req")
  177. } else if hasParams {
  178. return fmt.Sprintf("%s, %s", pathForRoute(route), "params")
  179. } else if hasBody {
  180. return fmt.Sprintf("%s, %s", pathForRoute(route), "req")
  181. }
  182. return pathForRoute(route)
  183. }
  184. func pathForRoute(route spec.Route) string {
  185. return "\"" + route.Path + "\""
  186. }
  187. func pathHasParams(route spec.Route) bool {
  188. return len(route.RequestType.Members) != len(route.RequestType.GetBodyMembers())
  189. }
  190. func hasRequestBody(route spec.Route) bool {
  191. return len(route.RequestType.Name) > 0 && len(route.RequestType.GetBodyMembers()) > 0
  192. }