optimize code (#417)
* optimize code * optimize code * optimize code * optimize code
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/core/logx"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -30,33 +29,30 @@ Future {{pathToFuncName .Path}}( {{if ne .Method "get"}}{{with .RequestType}}{{.
|
|||||||
{{end}}`
|
{{end}}`
|
||||||
|
|
||||||
func genApi(dir string, api *spec.ApiSpec) error {
|
func genApi(dir string, api *spec.ApiSpec) error {
|
||||||
e := os.MkdirAll(dir, 0755)
|
err := os.MkdirAll(dir, 0755)
|
||||||
if e != nil {
|
if err != nil {
|
||||||
logx.Error(e)
|
return err
|
||||||
return e
|
|
||||||
}
|
|
||||||
e = genApiFile(dir)
|
|
||||||
if e != nil {
|
|
||||||
logx.Error(e)
|
|
||||||
return e
|
|
||||||
}
|
}
|
||||||
|
|
||||||
file, e := os.OpenFile(dir+api.Info.Title+".dart", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
err = genApiFile(dir)
|
||||||
if e != nil {
|
if err != nil {
|
||||||
logx.Error(e)
|
return err
|
||||||
return e
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file, err := os.OpenFile(dir+api.Service.Name+".dart", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
t := template.New("apiTemplate")
|
t := template.New("apiTemplate")
|
||||||
t = t.Funcs(funcMap)
|
t = t.Funcs(funcMap)
|
||||||
t, e = t.Parse(apiTemplate)
|
t, err = t.Parse(apiTemplate)
|
||||||
if e != nil {
|
if err != nil {
|
||||||
logx.Error(e)
|
return err
|
||||||
return e
|
|
||||||
}
|
}
|
||||||
t.Execute(file, api)
|
|
||||||
return nil
|
return t.Execute(file, api)
|
||||||
}
|
}
|
||||||
|
|
||||||
func genApiFile(dir string) error {
|
func genApiFile(dir string) error {
|
||||||
@@ -64,12 +60,12 @@ func genApiFile(dir string) error {
|
|||||||
if fileExists(path) {
|
if fileExists(path) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
apiFile, e := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
apiFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
if e != nil {
|
if err != nil {
|
||||||
logx.Error(e)
|
return err
|
||||||
return e
|
|
||||||
}
|
}
|
||||||
|
|
||||||
defer apiFile.Close()
|
defer apiFile.Close()
|
||||||
apiFile.WriteString(apiFileContent)
|
_, err = apiFile.WriteString(apiFileContent)
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/core/logx"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,30 +32,27 @@ class {{.Name}}{
|
|||||||
`
|
`
|
||||||
|
|
||||||
func genData(dir string, api *spec.ApiSpec) error {
|
func genData(dir string, api *spec.ApiSpec) error {
|
||||||
e := os.MkdirAll(dir, 0755)
|
err := os.MkdirAll(dir, 0755)
|
||||||
if e != nil {
|
if err != nil {
|
||||||
logx.Error(e)
|
return err
|
||||||
return e
|
|
||||||
}
|
|
||||||
e = genTokens(dir)
|
|
||||||
if e != nil {
|
|
||||||
logx.Error(e)
|
|
||||||
return e
|
|
||||||
}
|
}
|
||||||
|
|
||||||
file, e := os.OpenFile(dir+api.Info.Title+".dart", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
err = genTokens(dir)
|
||||||
if e != nil {
|
if err != nil {
|
||||||
logx.Error(e)
|
return err
|
||||||
return e
|
}
|
||||||
|
|
||||||
|
file, err := os.OpenFile(dir+api.Service.Name+".dart", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
t := template.New("dataTemplate")
|
t := template.New("dataTemplate")
|
||||||
t = t.Funcs(funcMap)
|
t = t.Funcs(funcMap)
|
||||||
t, e = t.Parse(dataTemplate)
|
t, err = t.Parse(dataTemplate)
|
||||||
if e != nil {
|
if err != nil {
|
||||||
logx.Error(e)
|
return err
|
||||||
return e
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return t.Execute(file, api)
|
return t.Execute(file, api)
|
||||||
@@ -67,12 +63,13 @@ func genTokens(dir string) error {
|
|||||||
if fileExists(path) {
|
if fileExists(path) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
tokensFile, e := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
||||||
if e != nil {
|
tokensFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
logx.Error(e)
|
if err != nil {
|
||||||
return e
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer tokensFile.Close()
|
defer tokensFile.Close()
|
||||||
tokensFile.WriteString(tokensFileContent)
|
_, err = tokensFile.WriteString(tokensFileContent)
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,27 +3,9 @@ package dartgen
|
|||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/core/logx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func genVars(dir string) error {
|
const varTemplate = `import 'dart:convert';
|
||||||
e := os.MkdirAll(dir, 0755)
|
|
||||||
if e != nil {
|
|
||||||
logx.Error(e)
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
if !fileExists(dir + "vars.dart") {
|
|
||||||
e = ioutil.WriteFile(dir+"vars.dart", []byte(`const serverHost='demo-crm.xiaoheiban.cn';`), 0644)
|
|
||||||
if e != nil {
|
|
||||||
logx.Error(e)
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !fileExists(dir + "kv.dart") {
|
|
||||||
e = ioutil.WriteFile(dir+"kv.dart", []byte(`import 'dart:convert';
|
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import '../data/tokens.dart';
|
import '../data/tokens.dart';
|
||||||
|
|
||||||
@@ -56,10 +38,25 @@ Future<Tokens> getTokens() async {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`), 0644)
|
`
|
||||||
if e != nil {
|
|
||||||
logx.Error(e)
|
func genVars(dir string) error {
|
||||||
return e
|
err := os.MkdirAll(dir, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !fileExists(dir + "vars.dart") {
|
||||||
|
err = ioutil.WriteFile(dir+"vars.dart", []byte(`const serverHost='demo-crm.xiaoheiban.cn';`), 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !fileExists(dir + "kv.dart") {
|
||||||
|
err = ioutil.WriteFile(dir+"kv.dart", []byte(varTemplate), 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ func lowCamelCase(s string) string {
|
|||||||
if len(s) < 1 {
|
if len(s) < 1 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
s = util.ToCamelCase(util.ToSnakeCase(s))
|
s = util.ToCamelCase(util.ToSnakeCase(s))
|
||||||
return util.ToLower(s[:1]) + s[1:]
|
return util.ToLower(s[:1]) + s[1:]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ var funcMap = template.FuncMap{
|
|||||||
"lowCamelCase": lowCamelCase,
|
"lowCamelCase": lowCamelCase,
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiFileContent = `import 'dart:io';
|
const (
|
||||||
|
apiFileContent = `import 'dart:io';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import '../vars/kv.dart';
|
import '../vars/kv.dart';
|
||||||
import '../vars/vars.dart';
|
import '../vars/vars.dart';
|
||||||
@@ -97,7 +98,8 @@ Future _apiRequest(String method, String path, dynamic data,
|
|||||||
if (eventually != null) eventually();
|
if (eventually != null) eventually();
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
const tokensFileContent = `class Tokens {
|
|
||||||
|
tokensFileContent = `class Tokens {
|
||||||
/// 用于访问的token, 每次请求都必须带在Header里面
|
/// 用于访问的token, 每次请求都必须带在Header里面
|
||||||
final String accessToken;
|
final String accessToken;
|
||||||
final int accessExpire;
|
final int accessExpire;
|
||||||
@@ -131,3 +133,4 @@ const tokensFileContent = `class Tokens {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user