genapi.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package dartgen
  2. import (
  3. "os"
  4. "strings"
  5. "text/template"
  6. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/api/spec"
  7. )
  8. const apiTemplate = `import 'api.dart';
  9. import '../data/{{with .Info}}{{getBaseName .Title}}{{end}}.dart';
  10. {{with .Service}}
  11. /// {{.Name}}
  12. {{range .Routes}}
  13. /// --{{.Path}}--
  14. ///
  15. /// 请求: {{with .RequestType}}{{.Name}}{{end}}
  16. /// 返回: {{with .ResponseType}}{{.Name}}{{end}}
  17. Future {{pathToFuncName .Path}}( {{if ne .Method "get"}}{{with .RequestType}}{{.Name}} request,{{end}}{{end}}
  18. {Function({{with .ResponseType}}{{.Name}}{{end}}) ok,
  19. Function(String) fail,
  20. Function eventually}) async {
  21. await api{{if eq .Method "get"}}Get{{else}}Post{{end}}('{{.Path}}',{{if ne .Method "get"}}request,{{end}}
  22. ok: (data) {
  23. if (ok != null) ok({{with .ResponseType}}{{.Name}}.fromJson(data){{end}});
  24. }, fail: fail, eventually: eventually);
  25. }
  26. {{end}}
  27. {{end}}`
  28. const apiTemplateV2 = `import 'api.dart';
  29. import '../data/{{with .Service}}{{.Name}}{{end}}.dart';
  30. {{with .Service}}
  31. /// {{.Name}}
  32. {{range $i, $Route := .Routes}}
  33. /// --{{.Path}}--
  34. ///
  35. /// request: {{with .RequestType}}{{.Name}}{{end}}
  36. /// response: {{with .ResponseType}}{{.Name}}{{end}}
  37. Future {{normalizeHandlerName .Handler}}(
  38. {{if hasUrlPathParams $Route}}{{extractPositionalParamsFromPath $Route}},{{end}}
  39. {{if ne .Method "get"}}{{with .RequestType}}{{.Name}} request,{{end}}{{end}}
  40. {Function({{with .ResponseType}}{{.Name}}{{end}})? ok,
  41. Function(String)? fail,
  42. Function? eventually}) async {
  43. await api{{if eq .Method "get"}}Get{{else}}Post{{end}}({{makeDartRequestUrlPath $Route}},{{if ne .Method "get"}}request,{{end}}
  44. ok: (data) {
  45. if (ok != null) ok({{with .ResponseType}}{{.Name}}.fromJson(data){{end}});
  46. }, fail: fail, eventually: eventually);
  47. }
  48. {{end}}
  49. {{end}}`
  50. func genApi(dir string, api *spec.ApiSpec, isLegacy bool) error {
  51. err := os.MkdirAll(dir, 0o755)
  52. if err != nil {
  53. return err
  54. }
  55. err = genApiFile(dir, isLegacy)
  56. if err != nil {
  57. return err
  58. }
  59. file, err := os.OpenFile(dir+strings.ToLower(api.Service.Name+".dart"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
  60. if err != nil {
  61. return err
  62. }
  63. defer file.Close()
  64. t := template.New("apiTemplate")
  65. t = t.Funcs(funcMap)
  66. tpl := apiTemplateV2
  67. if isLegacy {
  68. tpl = apiTemplate
  69. }
  70. t, err = t.Parse(tpl)
  71. if err != nil {
  72. return err
  73. }
  74. return t.Execute(file, api)
  75. }
  76. func genApiFile(dir string, isLegacy bool) error {
  77. path := dir + "api.dart"
  78. if fileExists(path) {
  79. return nil
  80. }
  81. apiFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
  82. if err != nil {
  83. return err
  84. }
  85. defer apiFile.Close()
  86. tpl := apiFileContentV2
  87. if isLegacy {
  88. tpl = apiFileContent
  89. }
  90. _, err = apiFile.WriteString(tpl)
  91. return err
  92. }