genapi.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package dartgen
  2. import (
  3. "os"
  4. "strings"
  5. "text/template"
  6. "github.com/zeromicro/go-zero/tools/goctl/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 .Info}}{{getBaseName .Title}}{{end}}.dart';
  30. {{with .Service}}
  31. /// {{.Name}}
  32. {{range .Routes}}
  33. /// --{{.Path}}--
  34. ///
  35. /// request: {{with .RequestType}}{{.Name}}{{end}}
  36. /// response: {{with .ResponseType}}{{.Name}}{{end}}
  37. Future {{pathToFuncName .Path}}( {{if ne .Method "get"}}{{with .RequestType}}{{.Name}} request,{{end}}{{end}}
  38. {Function({{with .ResponseType}}{{.Name}}{{end}})? ok,
  39. Function(String)? fail,
  40. Function? eventually}) async {
  41. await api{{if eq .Method "get"}}Get{{else}}Post{{end}}('{{.Path}}',{{if ne .Method "get"}}request,{{end}}
  42. ok: (data) {
  43. if (ok != null) ok({{with .ResponseType}}{{.Name}}.fromJson(data){{end}});
  44. }, fail: fail, eventually: eventually);
  45. }
  46. {{end}}
  47. {{end}}`
  48. func genApi(dir string, api *spec.ApiSpec, isLegacy bool) error {
  49. err := os.MkdirAll(dir, 0o755)
  50. if err != nil {
  51. return err
  52. }
  53. err = genApiFile(dir, isLegacy)
  54. if err != nil {
  55. return err
  56. }
  57. file, err := os.OpenFile(dir+strings.ToLower(api.Service.Name+".dart"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
  58. if err != nil {
  59. return err
  60. }
  61. defer file.Close()
  62. t := template.New("apiTemplate")
  63. t = t.Funcs(funcMap)
  64. tpl := apiTemplateV2
  65. if isLegacy {
  66. tpl = apiTemplate
  67. }
  68. t, err = t.Parse(tpl)
  69. if err != nil {
  70. return err
  71. }
  72. return t.Execute(file, api)
  73. }
  74. func genApiFile(dir string, isLegacy bool) error {
  75. path := dir + "api.dart"
  76. if fileExists(path) {
  77. return nil
  78. }
  79. apiFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
  80. if err != nil {
  81. return err
  82. }
  83. defer apiFile.Close()
  84. tpl := apiFileContentV2
  85. if isLegacy {
  86. tpl = apiFileContent
  87. }
  88. _, err = apiFile.WriteString(tpl)
  89. return err
  90. }