feat(goctl): api dart support flutter v2 (#1603)
0. support null-safety code gen 1. supports -legacy flag for legacy code gen 2. supports -hostname flag for server hostname 3. use dart official format 4. fix some some bugs Resolves: #1602
This commit is contained in:
@@ -2,13 +2,14 @@ package dartgen
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
|
||||
)
|
||||
|
||||
const apiTemplate = `import 'api.dart';
|
||||
import '../data/{{with .Info}}{{.Title}}{{end}}.dart';
|
||||
import '../data/{{with .Info}}{{getBaseName .Title}}{{end}}.dart';
|
||||
{{with .Service}}
|
||||
/// {{.Name}}
|
||||
{{range .Routes}}
|
||||
@@ -22,24 +23,45 @@ Future {{pathToFuncName .Path}}( {{if ne .Method "get"}}{{with .RequestType}}{{.
|
||||
Function eventually}) async {
|
||||
await api{{if eq .Method "get"}}Get{{else}}Post{{end}}('{{.Path}}',{{if ne .Method "get"}}request,{{end}}
|
||||
ok: (data) {
|
||||
if (ok != null) ok({{with .ResponseType}}{{.Name}}{{end}}.fromJson(data));
|
||||
if (ok != null) ok({{with .ResponseType}}{{.Name}}.fromJson(data){{end}});
|
||||
}, fail: fail, eventually: eventually);
|
||||
}
|
||||
{{end}}
|
||||
{{end}}`
|
||||
|
||||
func genApi(dir string, api *spec.ApiSpec) error {
|
||||
const apiTemplateV2 = `import 'api.dart';
|
||||
import '../data/{{with .Info}}{{getBaseName .Title}}{{end}}.dart';
|
||||
{{with .Service}}
|
||||
/// {{.Name}}
|
||||
{{range .Routes}}
|
||||
/// --{{.Path}}--
|
||||
///
|
||||
/// request: {{with .RequestType}}{{.Name}}{{end}}
|
||||
/// response: {{with .ResponseType}}{{.Name}}{{end}}
|
||||
Future {{pathToFuncName .Path}}( {{if ne .Method "get"}}{{with .RequestType}}{{.Name}} request,{{end}}{{end}}
|
||||
{Function({{with .ResponseType}}{{.Name}}{{end}})? ok,
|
||||
Function(String)? fail,
|
||||
Function? eventually}) async {
|
||||
await api{{if eq .Method "get"}}Get{{else}}Post{{end}}('{{.Path}}',{{if ne .Method "get"}}request,{{end}}
|
||||
ok: (data) {
|
||||
if (ok != null) ok({{with .ResponseType}}{{.Name}}.fromJson(data){{end}});
|
||||
}, fail: fail, eventually: eventually);
|
||||
}
|
||||
{{end}}
|
||||
{{end}}`
|
||||
|
||||
func genApi(dir string, api *spec.ApiSpec, isLegacy bool) error {
|
||||
err := os.MkdirAll(dir, 0o755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = genApiFile(dir)
|
||||
err = genApiFile(dir, isLegacy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(dir+api.Service.Name+".dart", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
|
||||
file, err := os.OpenFile(dir+strings.ToLower(api.Service.Name+".dart"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -47,7 +69,11 @@ func genApi(dir string, api *spec.ApiSpec) error {
|
||||
defer file.Close()
|
||||
t := template.New("apiTemplate")
|
||||
t = t.Funcs(funcMap)
|
||||
t, err = t.Parse(apiTemplate)
|
||||
tpl := apiTemplateV2
|
||||
if isLegacy {
|
||||
tpl = apiTemplate
|
||||
}
|
||||
t, err = t.Parse(tpl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -55,7 +81,7 @@ func genApi(dir string, api *spec.ApiSpec) error {
|
||||
return t.Execute(file, api)
|
||||
}
|
||||
|
||||
func genApiFile(dir string) error {
|
||||
func genApiFile(dir string, isLegacy bool) error {
|
||||
path := dir + "api.dart"
|
||||
if fileExists(path) {
|
||||
return nil
|
||||
@@ -66,6 +92,10 @@ func genApiFile(dir string) error {
|
||||
}
|
||||
|
||||
defer apiFile.Close()
|
||||
_, err = apiFile.WriteString(apiFileContent)
|
||||
tpl := apiFileContentV2
|
||||
if isLegacy {
|
||||
tpl = apiFileContent
|
||||
}
|
||||
_, err = apiFile.WriteString(tpl)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user