remove duplicated code in goctl (#369)
This commit is contained in:
10
readme.md
10
readme.md
@@ -157,11 +157,11 @@ GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/tal-tech/
|
|||||||
|
|
||||||
## 7. 文档
|
## 7. 文档
|
||||||
|
|
||||||
* API 文档 (逐步完善中)
|
* API 文档
|
||||||
|
|
||||||
[https://www.yuque.com/tal-tech/go-zero](https://www.yuque.com/tal-tech/go-zero)
|
[https://www.yuque.com/tal-tech/go-zero](https://www.yuque.com/tal-tech/go-zero)
|
||||||
|
|
||||||
* awesome 系列
|
* awesome 系列(全部收录于『微服务实践』公众号)
|
||||||
* [快速构建高并发微服务](https://github.com/tal-tech/zero-doc/blob/main/doc/shorturl.md)
|
* [快速构建高并发微服务](https://github.com/tal-tech/zero-doc/blob/main/doc/shorturl.md)
|
||||||
* [快速构建高并发微服务 - 多 RPC 版](https://github.com/tal-tech/zero-doc/blob/main/docs/zero/bookstore.md)
|
* [快速构建高并发微服务 - 多 RPC 版](https://github.com/tal-tech/zero-doc/blob/main/docs/zero/bookstore.md)
|
||||||
* [goctl 使用帮助](https://github.com/tal-tech/zero-doc/blob/main/doc/goctl.md)
|
* [goctl 使用帮助](https://github.com/tal-tech/zero-doc/blob/main/doc/goctl.md)
|
||||||
@@ -172,6 +172,12 @@ GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/tal-tech/
|
|||||||
* [基于 prometheus 的微服务指标监控](https://github.com/tal-tech/zero-doc/blob/main/doc/metric.md)
|
* [基于 prometheus 的微服务指标监控](https://github.com/tal-tech/zero-doc/blob/main/doc/metric.md)
|
||||||
* [文本序列化和反序列化](https://github.com/tal-tech/zero-doc/blob/main/doc/mapping.md)
|
* [文本序列化和反序列化](https://github.com/tal-tech/zero-doc/blob/main/doc/mapping.md)
|
||||||
* [快速构建 jwt 鉴权认证](https://github.com/tal-tech/zero-doc/blob/main/doc/jwt.md)
|
* [快速构建 jwt 鉴权认证](https://github.com/tal-tech/zero-doc/blob/main/doc/jwt.md)
|
||||||
|
|
||||||
|
* 精选 `goctl` 插件
|
||||||
|
|
||||||
|
* [goctl-swagger](https://github.com/zeromicro/goctl-swagger) 一键生成 `api` 的 `swagger` 文档
|
||||||
|
* [goctl-android](https://github.com/zeromicro/goctl-android) 生成 `java (android)` 端 `http client` 请求代码
|
||||||
|
* [goctl-go-compact](https://github.com/zeromicro/goctl-go-compact) 合并 `api` 里同一个 `group` 里的 `handler` 到一个 `go` 文件
|
||||||
|
|
||||||
## 8. 微信公众号
|
## 8. 微信公众号
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,11 @@
|
|||||||
package gogen
|
package gogen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||||
)
|
)
|
||||||
@@ -39,38 +35,24 @@ func genConfig(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fp, created, err := util.MaybeCreateFile(dir, configDir, filename+".go")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !created {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
defer fp.Close()
|
|
||||||
|
|
||||||
var authNames = getAuths(api)
|
var authNames = getAuths(api)
|
||||||
var auths []string
|
var auths []string
|
||||||
for _, item := range authNames {
|
for _, item := range authNames {
|
||||||
auths = append(auths, fmt.Sprintf("%s %s", item, jwtTemplate))
|
auths = append(auths, fmt.Sprintf("%s %s", item, jwtTemplate))
|
||||||
}
|
}
|
||||||
|
|
||||||
var authImportStr = fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl)
|
var authImportStr = fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl)
|
||||||
text, err := ctlutil.LoadTemplate(category, configTemplateFile, configTemplate)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
t := template.Must(template.New("configTemplate").Parse(text))
|
return genFile(fileGenConfig{
|
||||||
buffer := new(bytes.Buffer)
|
dir: dir,
|
||||||
err = t.Execute(buffer, map[string]string{
|
subdir: configDir,
|
||||||
"authImport": authImportStr,
|
filename: filename + ".go",
|
||||||
"auth": strings.Join(auths, "\n"),
|
templateName: "configTemplate",
|
||||||
|
category: category,
|
||||||
|
templateFile: configTemplateFile,
|
||||||
|
builtinTemplate: configTemplate,
|
||||||
|
data: map[string]string{
|
||||||
|
"authImport": authImportStr,
|
||||||
|
"auth": strings.Join(auths, "\n"),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
formatCode := formatCode(buffer.String())
|
|
||||||
_, err = fp.WriteString(formatCode)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,12 @@
|
|||||||
package gogen
|
package gogen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"text/template"
|
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,15 +25,6 @@ func genEtc(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fp, created, err := util.MaybeCreateFile(dir, etcDir, fmt.Sprintf("%s.yaml", filename))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !created {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
defer fp.Close()
|
|
||||||
|
|
||||||
service := api.Service
|
service := api.Service
|
||||||
host, ok := util.GetAnnotationValue(service.Groups[0].Annotations, "server", "host")
|
host, ok := util.GetAnnotationValue(service.Groups[0].Annotations, "server", "host")
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -47,23 +35,18 @@ func genEtc(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
|||||||
port = strconv.Itoa(defaultPort)
|
port = strconv.Itoa(defaultPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := ctlutil.LoadTemplate(category, etcTemplateFile, etcTemplate)
|
return genFile(fileGenConfig{
|
||||||
if err != nil {
|
dir: dir,
|
||||||
return err
|
subdir: etcDir,
|
||||||
}
|
filename: fmt.Sprintf("%s.yaml", filename),
|
||||||
|
templateName: "etcTemplate",
|
||||||
t := template.Must(template.New("etcTemplate").Parse(text))
|
category: category,
|
||||||
buffer := new(bytes.Buffer)
|
templateFile: etcTemplateFile,
|
||||||
err = t.Execute(buffer, map[string]string{
|
builtinTemplate: etcTemplate,
|
||||||
"serviceName": service.Name,
|
data: map[string]string{
|
||||||
"host": host,
|
"serviceName": service.Name,
|
||||||
"port": port,
|
"host": host,
|
||||||
|
"port": port,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
formatCode := formatCode(buffer.String())
|
|
||||||
_, err = fp.WriteString(formatCode)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
package gogen
|
package gogen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
@@ -82,30 +80,16 @@ func doGenToFile(dir, handler string, cfg *config.Config, group spec.Group,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
filename = filename + ".go"
|
return genFile(fileGenConfig{
|
||||||
fp, created, err := apiutil.MaybeCreateFile(dir, getHandlerFolderPath(group, route), filename)
|
dir: dir,
|
||||||
if err != nil {
|
subdir: getHandlerFolderPath(group, route),
|
||||||
return err
|
filename: filename + ".go",
|
||||||
}
|
templateName: "handlerTemplate",
|
||||||
if !created {
|
category: category,
|
||||||
return nil
|
templateFile: handlerTemplateFile,
|
||||||
}
|
builtinTemplate: handlerTemplate,
|
||||||
defer fp.Close()
|
data: handleObj,
|
||||||
|
})
|
||||||
text, err := util.LoadTemplate(category, handlerTemplateFile, handlerTemplate)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer := new(bytes.Buffer)
|
|
||||||
err = template.Must(template.New("handlerTemplate").Parse(text)).Execute(buffer, handleObj)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
formatCode := formatCode(buffer.String())
|
|
||||||
_, err = fp.WriteString(formatCode)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func genHandlers(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
func genHandlers(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package gogen
|
package gogen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||||
@@ -61,17 +59,6 @@ func genLogicByRoute(dir string, cfg *config.Config, group spec.Group, route spe
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
goFile = goFile + ".go"
|
|
||||||
fp, created, err := util.MaybeCreateFile(dir, getLogicFolderPath(group, route), goFile)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !created {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
defer fp.Close()
|
|
||||||
|
|
||||||
parentPkg, err := getParentPackage(dir)
|
parentPkg, err := getParentPackage(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -93,28 +80,23 @@ func genLogicByRoute(dir string, cfg *config.Config, group spec.Group, route spe
|
|||||||
requestString = "req " + "types." + strings.Title(route.RequestType.Name)
|
requestString = "req " + "types." + strings.Title(route.RequestType.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := ctlutil.LoadTemplate(category, logicTemplateFile, logicTemplate)
|
return genFile(fileGenConfig{
|
||||||
if err != nil {
|
dir: dir,
|
||||||
return err
|
subdir: getLogicFolderPath(group, route),
|
||||||
}
|
filename: goFile + ".go",
|
||||||
|
templateName: "logicTemplate",
|
||||||
t := template.Must(template.New("logicTemplate").Parse(text))
|
category: category,
|
||||||
buffer := new(bytes.Buffer)
|
templateFile: logicTemplateFile,
|
||||||
err = t.Execute(fp, map[string]string{
|
builtinTemplate: logicTemplate,
|
||||||
"imports": imports,
|
data: map[string]string{
|
||||||
"logic": strings.Title(logic),
|
"imports": imports,
|
||||||
"function": strings.Title(strings.TrimSuffix(logic, "Logic")),
|
"logic": strings.Title(logic),
|
||||||
"responseType": responseString,
|
"function": strings.Title(strings.TrimSuffix(logic, "Logic")),
|
||||||
"returnString": returnString,
|
"responseType": responseString,
|
||||||
"request": requestString,
|
"returnString": returnString,
|
||||||
|
"request": requestString,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
formatCode := formatCode(buffer.String())
|
|
||||||
_, err = fp.WriteString(formatCode)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getLogicFolderPath(group spec.Group, route spec.Route) string {
|
func getLogicFolderPath(group spec.Group, route spec.Route) string {
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
package gogen
|
package gogen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
@@ -52,39 +49,24 @@ func genMain(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
goFile := filename + ".go"
|
|
||||||
fp, created, err := util.MaybeCreateFile(dir, "", goFile)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !created {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
defer fp.Close()
|
|
||||||
|
|
||||||
parentPkg, err := getParentPackage(dir)
|
parentPkg, err := getParentPackage(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := ctlutil.LoadTemplate(category, mainTemplateFile, mainTemplate)
|
return genFile(fileGenConfig{
|
||||||
if err != nil {
|
dir: dir,
|
||||||
return err
|
subdir: "",
|
||||||
}
|
filename: filename + ".go",
|
||||||
|
templateName: "mainTemplate",
|
||||||
t := template.Must(template.New("mainTemplate").Parse(text))
|
category: category,
|
||||||
buffer := new(bytes.Buffer)
|
templateFile: mainTemplateFile,
|
||||||
err = t.Execute(buffer, map[string]string{
|
builtinTemplate: mainTemplate,
|
||||||
"importPackages": genMainImports(parentPkg),
|
data: map[string]string{
|
||||||
"serviceName": api.Service.Name,
|
"importPackages": genMainImports(parentPkg),
|
||||||
|
"serviceName": api.Service.Name,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
formatCode := formatCode(buffer.String())
|
|
||||||
_, err = fp.WriteString(formatCode)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func genMainImports(parentPkg string) string {
|
func genMainImports(parentPkg string) string {
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
package gogen
|
package gogen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
)
|
)
|
||||||
@@ -37,36 +34,26 @@ func genMiddleware(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
|||||||
var middlewares = getMiddleware(api)
|
var middlewares = getMiddleware(api)
|
||||||
for _, item := range middlewares {
|
for _, item := range middlewares {
|
||||||
middlewareFilename := strings.TrimSuffix(strings.ToLower(item), "middleware") + "_middleware"
|
middlewareFilename := strings.TrimSuffix(strings.ToLower(item), "middleware") + "_middleware"
|
||||||
formatName, err := format.FileNamingFormat(cfg.NamingFormat, middlewareFilename)
|
filename, err := format.FileNamingFormat(cfg.NamingFormat, middlewareFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
filename := formatName + ".go"
|
|
||||||
fp, created, err := util.MaybeCreateFile(dir, middlewareDir, filename)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !created {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
defer fp.Close()
|
|
||||||
|
|
||||||
name := strings.TrimSuffix(item, "Middleware") + "Middleware"
|
name := strings.TrimSuffix(item, "Middleware") + "Middleware"
|
||||||
t := template.Must(template.New("contextTemplate").Parse(middlewareImplementCode))
|
err = genFile(fileGenConfig{
|
||||||
buffer := new(bytes.Buffer)
|
dir: dir,
|
||||||
err = t.Execute(buffer, map[string]string{
|
subdir: middlewareDir,
|
||||||
"name": strings.Title(name),
|
filename: filename + ".go",
|
||||||
|
templateName: "contextTemplate",
|
||||||
|
builtinTemplate: middlewareImplementCode,
|
||||||
|
data: map[string]string{
|
||||||
|
"name": strings.Title(name),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
formatCode := formatCode(buffer.String())
|
|
||||||
_, err = fp.WriteString(formatCode)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package gogen
|
package gogen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@@ -132,28 +131,19 @@ func genRoutes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
|||||||
filename := path.Join(dir, handlerDir, routeFilename)
|
filename := path.Join(dir, handlerDir, routeFilename)
|
||||||
os.Remove(filename)
|
os.Remove(filename)
|
||||||
|
|
||||||
fp, created, err := apiutil.MaybeCreateFile(dir, handlerDir, routeFilename)
|
return genFile(fileGenConfig{
|
||||||
if err != nil {
|
dir: dir,
|
||||||
return err
|
subdir: handlerDir,
|
||||||
}
|
filename: routeFilename,
|
||||||
if !created {
|
templateName: "routesTemplate",
|
||||||
return nil
|
category: "",
|
||||||
}
|
templateFile: "",
|
||||||
defer fp.Close()
|
builtinTemplate: routesTemplate,
|
||||||
|
data: map[string]string{
|
||||||
t := template.Must(template.New("routesTemplate").Parse(routesTemplate))
|
"importPackages": genRouteImports(parentPkg, api),
|
||||||
buffer := new(bytes.Buffer)
|
"routesAdditions": strings.TrimSpace(builder.String()),
|
||||||
err = t.Execute(buffer, map[string]string{
|
},
|
||||||
"importPackages": genRouteImports(parentPkg, api),
|
|
||||||
"routesAdditions": strings.TrimSpace(builder.String()),
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
formatCode := formatCode(buffer.String())
|
|
||||||
_, err = fp.WriteString(formatCode)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
|
func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
package gogen
|
package gogen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
@@ -33,7 +30,6 @@ func NewServiceContext(c {{.config}}) *ServiceContext {
|
|||||||
{{.middlewareAssignment}}
|
{{.middlewareAssignment}}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -43,15 +39,6 @@ func genServiceContext(dir string, cfg *config.Config, api *spec.ApiSpec) error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fp, created, err := util.MaybeCreateFile(dir, contextDir, filename+".go")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !created {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
defer fp.Close()
|
|
||||||
|
|
||||||
var authNames = getAuths(api)
|
var authNames = getAuths(api)
|
||||||
var auths []string
|
var auths []string
|
||||||
for _, item := range authNames {
|
for _, item := range authNames {
|
||||||
@@ -63,11 +50,6 @@ func genServiceContext(dir string, cfg *config.Config, api *spec.ApiSpec) error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := ctlutil.LoadTemplate(category, contextTemplateFile, contextTemplate)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var middlewareStr string
|
var middlewareStr string
|
||||||
var middlewareAssignment string
|
var middlewareAssignment string
|
||||||
var middlewares = getMiddleware(api)
|
var middlewares = getMiddleware(api)
|
||||||
@@ -75,7 +57,8 @@ func genServiceContext(dir string, cfg *config.Config, api *spec.ApiSpec) error
|
|||||||
for _, item := range middlewares {
|
for _, item := range middlewares {
|
||||||
middlewareStr += fmt.Sprintf("%s rest.Middleware\n", item)
|
middlewareStr += fmt.Sprintf("%s rest.Middleware\n", item)
|
||||||
name := strings.TrimSuffix(item, "Middleware") + "Middleware"
|
name := strings.TrimSuffix(item, "Middleware") + "Middleware"
|
||||||
middlewareAssignment += fmt.Sprintf("%s: %s,\n", item, fmt.Sprintf("middleware.New%s().%s", strings.Title(name), "Handle"))
|
middlewareAssignment += fmt.Sprintf("%s: %s,\n", item,
|
||||||
|
fmt.Sprintf("middleware.New%s().%s", strings.Title(name), "Handle"))
|
||||||
}
|
}
|
||||||
|
|
||||||
var configImport = "\"" + ctlutil.JoinPackages(parentPkg, configDir) + "\""
|
var configImport = "\"" + ctlutil.JoinPackages(parentPkg, configDir) + "\""
|
||||||
@@ -84,19 +67,19 @@ func genServiceContext(dir string, cfg *config.Config, api *spec.ApiSpec) error
|
|||||||
configImport += fmt.Sprintf("\n\t\"%s/rest\"", vars.ProjectOpenSourceUrl)
|
configImport += fmt.Sprintf("\n\t\"%s/rest\"", vars.ProjectOpenSourceUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
t := template.Must(template.New("contextTemplate").Parse(text))
|
return genFile(fileGenConfig{
|
||||||
buffer := new(bytes.Buffer)
|
dir: dir,
|
||||||
err = t.Execute(buffer, map[string]string{
|
subdir: contextDir,
|
||||||
"configImport": configImport,
|
filename: filename + ".go",
|
||||||
"config": "config.Config",
|
templateName: "contextTemplate",
|
||||||
"middleware": middlewareStr,
|
category: category,
|
||||||
"middlewareAssignment": middlewareAssignment,
|
templateFile: contextTemplateFile,
|
||||||
|
builtinTemplate: contextTemplate,
|
||||||
|
data: map[string]string{
|
||||||
|
"configImport": configImport,
|
||||||
|
"config": "config.Config",
|
||||||
|
"middleware": middlewareStr,
|
||||||
|
"middlewareAssignment": middlewareAssignment,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
formatCode := formatCode(buffer.String())
|
|
||||||
_, err = fp.WriteString(formatCode)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
package gogen
|
package gogen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
|
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||||
@@ -59,29 +57,19 @@ func genTypes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
|||||||
filename := path.Join(dir, typesDir, typeFilename)
|
filename := path.Join(dir, typesDir, typeFilename)
|
||||||
os.Remove(filename)
|
os.Remove(filename)
|
||||||
|
|
||||||
fp, created, err := apiutil.MaybeCreateFile(dir, typesDir, typeFilename)
|
return genFile(fileGenConfig{
|
||||||
if err != nil {
|
dir: dir,
|
||||||
return err
|
subdir: typesDir,
|
||||||
}
|
filename: typeFilename,
|
||||||
|
templateName: "typesTemplate",
|
||||||
if !created {
|
category: "",
|
||||||
return nil
|
templateFile: "",
|
||||||
}
|
builtinTemplate: typesTemplate,
|
||||||
defer fp.Close()
|
data: map[string]interface{}{
|
||||||
|
"types": val,
|
||||||
t := template.Must(template.New("typesTemplate").Parse(typesTemplate))
|
"containsTime": api.ContainsTime(),
|
||||||
buffer := new(bytes.Buffer)
|
},
|
||||||
err = t.Execute(buffer, map[string]interface{}{
|
|
||||||
"types": val,
|
|
||||||
"containsTime": api.ContainsTime(),
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
formatCode := formatCode(buffer.String())
|
|
||||||
_, err = fp.WriteString(formatCode)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertTypeCase(types []spec.Type, t string) (string, error) {
|
func convertTypeCase(types []spec.Type, t string) (string, error) {
|
||||||
|
|||||||
@@ -1,18 +1,64 @@
|
|||||||
package gogen
|
package gogen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
goformat "go/format"
|
goformat "go/format"
|
||||||
"io"
|
"io"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/core/collection"
|
"github.com/tal-tech/go-zero/core/collection"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||||
|
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/ctx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/ctx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type fileGenConfig struct {
|
||||||
|
dir string
|
||||||
|
subdir string
|
||||||
|
filename string
|
||||||
|
templateName string
|
||||||
|
category string
|
||||||
|
templateFile string
|
||||||
|
builtinTemplate string
|
||||||
|
data interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func genFile(c fileGenConfig) error {
|
||||||
|
fp, created, err := util.MaybeCreateFile(c.dir, c.subdir, c.filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !created {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
defer fp.Close()
|
||||||
|
|
||||||
|
var text string
|
||||||
|
if len(c.category) == 0 || len(c.templateFile) == 0 {
|
||||||
|
text = c.builtinTemplate
|
||||||
|
} else {
|
||||||
|
text, err = ctlutil.LoadTemplate(c.category, c.templateFile, c.builtinTemplate)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t := template.Must(template.New(c.templateName).Parse(text))
|
||||||
|
buffer := new(bytes.Buffer)
|
||||||
|
err = t.Execute(buffer, c.data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
code := formatCode(buffer.String())
|
||||||
|
_, err = fp.WriteString(code)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func getParentPackage(dir string) (string, error) {
|
func getParentPackage(dir string) (string, error) {
|
||||||
abs, err := filepath.Abs(dir)
|
abs, err := filepath.Abs(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user