util.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package gogen
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "strings"
  7. "text/template"
  8. "github.com/wuntsong-org/go-zero-plus/core/collection"
  9. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/api/spec"
  10. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/api/util"
  11. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/pkg/golang"
  12. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
  13. )
  14. type fileGenConfig struct {
  15. dir string
  16. subdir string
  17. filename string
  18. templateName string
  19. category string
  20. templateFile string
  21. builtinTemplate string
  22. data any
  23. }
  24. func genFile(c fileGenConfig) error {
  25. fp, created, err := util.MaybeCreateFile(c.dir, c.subdir, c.filename)
  26. if err != nil {
  27. return err
  28. }
  29. if !created {
  30. return nil
  31. }
  32. defer fp.Close()
  33. var text string
  34. if len(c.category) == 0 || len(c.templateFile) == 0 {
  35. text = c.builtinTemplate
  36. } else {
  37. text, err = pathx.LoadTemplate(c.category, c.templateFile, c.builtinTemplate)
  38. if err != nil {
  39. return err
  40. }
  41. }
  42. t := template.Must(template.New(c.templateName).Parse(text))
  43. buffer := new(bytes.Buffer)
  44. err = t.Execute(buffer, c.data)
  45. if err != nil {
  46. return err
  47. }
  48. code := golang.FormatCode(buffer.String())
  49. _, err = fp.WriteString(code)
  50. return err
  51. }
  52. func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, indent int) error {
  53. util.WriteIndent(writer, indent)
  54. var err error
  55. if len(comment) > 0 {
  56. comment = strings.TrimPrefix(comment, "//")
  57. comment = "//" + comment
  58. _, err = fmt.Fprintf(writer, "%s %s %s %s\n", strings.Title(name), tp.Name(), tag, comment)
  59. } else {
  60. _, err = fmt.Fprintf(writer, "%s %s %s\n", strings.Title(name), tp.Name(), tag)
  61. }
  62. return err
  63. }
  64. func getAuths(api *spec.ApiSpec) []string {
  65. authNames := collection.NewSet()
  66. for _, g := range api.Service.Groups {
  67. jwt := g.GetAnnotation("jwt")
  68. if len(jwt) > 0 {
  69. authNames.Add(jwt)
  70. }
  71. }
  72. return authNames.KeysStr()
  73. }
  74. func getJwtTrans(api *spec.ApiSpec) []string {
  75. jwtTransList := collection.NewSet()
  76. for _, g := range api.Service.Groups {
  77. jt := g.GetAnnotation(jwtTransKey)
  78. if len(jt) > 0 {
  79. jwtTransList.Add(jt)
  80. }
  81. }
  82. return jwtTransList.KeysStr()
  83. }
  84. func getMiddleware(api *spec.ApiSpec) []string {
  85. result := collection.NewSet()
  86. for _, g := range api.Service.Groups {
  87. middleware := g.GetAnnotation("middleware")
  88. if len(middleware) > 0 {
  89. for _, item := range strings.Split(middleware, ",") {
  90. result.Add(strings.TrimSpace(item))
  91. }
  92. }
  93. }
  94. return result.KeysStr()
  95. }
  96. func responseGoTypeName(r spec.Route, pkg ...string) string {
  97. if r.ResponseType == nil {
  98. return ""
  99. }
  100. resp := golangExpr(r.ResponseType, pkg...)
  101. switch r.ResponseType.(type) {
  102. case spec.DefineStruct:
  103. if !strings.HasPrefix(resp, "*") {
  104. return "*" + resp
  105. }
  106. }
  107. return resp
  108. }
  109. func requestGoTypeName(r spec.Route, pkg ...string) string {
  110. if r.RequestType == nil {
  111. return ""
  112. }
  113. return golangExpr(r.RequestType, pkg...)
  114. }
  115. func golangExpr(ty spec.Type, pkg ...string) string {
  116. switch v := ty.(type) {
  117. case spec.PrimitiveType:
  118. return v.RawName
  119. case spec.DefineStruct:
  120. if len(pkg) > 1 {
  121. panic("package cannot be more than 1")
  122. }
  123. if len(pkg) == 0 {
  124. return v.RawName
  125. }
  126. return fmt.Sprintf("%s.%s", pkg[0], strings.Title(v.RawName))
  127. case spec.ArrayType:
  128. if len(pkg) > 1 {
  129. panic("package cannot be more than 1")
  130. }
  131. if len(pkg) == 0 {
  132. return v.RawName
  133. }
  134. return fmt.Sprintf("[]%s", golangExpr(v.Value, pkg...))
  135. case spec.MapType:
  136. if len(pkg) > 1 {
  137. panic("package cannot be more than 1")
  138. }
  139. if len(pkg) == 0 {
  140. return v.RawName
  141. }
  142. return fmt.Sprintf("map[%s]%s", v.Key, golangExpr(v.Value, pkg...))
  143. case spec.PointerType:
  144. if len(pkg) > 1 {
  145. panic("package cannot be more than 1")
  146. }
  147. if len(pkg) == 0 {
  148. return v.RawName
  149. }
  150. return fmt.Sprintf("*%s", golangExpr(v.Type, pkg...))
  151. case spec.InterfaceType:
  152. return v.RawName
  153. }
  154. return ""
  155. }