Skip to content

Commit

Permalink
patch: save missing templates to disk (zeromicro#1463)
Browse files Browse the repository at this point in the history
Co-authored-by: anqiansong <anqiansong@bytedance.com>
  • Loading branch information
kesonan and anqiansong authored Jan 18, 2022
1 parent e57fa8f commit c903966
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
2 changes: 2 additions & 0 deletions tools/goctl/api/gogen/genmiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func genMiddleware(dir string, cfg *config.Config, api *spec.ApiSpec) error {
subdir: middlewareDir,
filename: filename + ".go",
templateName: "contextTemplate",
category: category,
templateFile: middlewareImplementCodeFile,
builtinTemplate: middlewareImplementCode,
data: map[string]string{
"name": strings.Title(name),
Expand Down
11 changes: 8 additions & 3 deletions tools/goctl/api/gogen/genroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ func genRoutes(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error
return err
}

gt := template.Must(template.New("groupTemplate").Parse(routesAdditionTemplate))
templateText, err := pathx.LoadTemplate(category, routesAdditionTemplateFile, routesAdditionTemplate)
if err != nil {
return err
}

gt := template.Must(template.New("groupTemplate").Parse(templateText))
for _, g := range groups {
var gbuilder strings.Builder
gbuilder.WriteString("[]rest.Route{")
Expand Down Expand Up @@ -139,8 +144,8 @@ rest.WithPrefix("%s"),`, g.prefix)
subdir: handlerDir,
filename: routeFilename,
templateName: "routesTemplate",
category: "",
templateFile: "",
category: category,
templateFile: routesTemplateFile,
builtinTemplate: routesTemplate,
data: map[string]string{
"importPackages": genRouteImports(rootPkg, api),
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/api/gogen/gentypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func genTypes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
subdir: typesDir,
filename: typeFilename,
templateName: "typesTemplate",
category: "",
templateFile: "",
category: category,
templateFile: typesTemplateFile,
builtinTemplate: typesTemplate,
data: map[string]interface{}{
"types": val,
Expand Down
34 changes: 21 additions & 13 deletions tools/goctl/api/gogen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,30 @@ import (
)

const (
category = "api"
configTemplateFile = "config.tpl"
contextTemplateFile = "context.tpl"
etcTemplateFile = "etc.tpl"
handlerTemplateFile = "handler.tpl"
logicTemplateFile = "logic.tpl"
mainTemplateFile = "main.tpl"
category = "api"
configTemplateFile = "config.tpl"
contextTemplateFile = "context.tpl"
etcTemplateFile = "etc.tpl"
handlerTemplateFile = "handler.tpl"
logicTemplateFile = "logic.tpl"
mainTemplateFile = "main.tpl"
middlewareImplementCodeFile = "middleware.tpl"
routesTemplateFile = "routes.tpl"
routesAdditionTemplateFile = "route-addition.tpl"
typesTemplateFile = "types.tpl"
)

var templates = map[string]string{
configTemplateFile: configTemplate,
contextTemplateFile: contextTemplate,
etcTemplateFile: etcTemplate,
handlerTemplateFile: handlerTemplate,
logicTemplateFile: logicTemplate,
mainTemplateFile: mainTemplate,
configTemplateFile: configTemplate,
contextTemplateFile: contextTemplate,
etcTemplateFile: etcTemplate,
handlerTemplateFile: handlerTemplate,
logicTemplateFile: logicTemplate,
mainTemplateFile: mainTemplate,
middlewareImplementCodeFile: middlewareImplementCode,
routesTemplateFile: routesTemplate,
routesAdditionTemplateFile: routesAdditionTemplate,
typesTemplateFile: typesTemplate,
}

// Category returns the category of the api files.
Expand Down

0 comments on commit c903966

Please sign in to comment.