From 12327fa07d4f26a2d51bef518f49c9675e9a4396 Mon Sep 17 00:00:00 2001 From: kingxt Date: Sat, 7 Nov 2020 21:25:52 +0800 Subject: [PATCH] break generator when happen error (#192) Co-authored-by: kim --- tools/goctl/api/gogen/genconfig.go | 3 ++- tools/goctl/api/gogen/genetc.go | 1 + tools/goctl/api/gogen/genhandlers.go | 2 +- tools/goctl/api/gogen/genmain.go | 3 ++- tools/goctl/api/gogen/genmiddleware.go | 3 ++- tools/goctl/api/gogen/genroutes.go | 11 ++++++----- tools/goctl/api/gogen/gensvc.go | 3 ++- tools/goctl/api/gogen/gentypes.go | 3 ++- 8 files changed, 18 insertions(+), 11 deletions(-) diff --git a/tools/goctl/api/gogen/genconfig.go b/tools/goctl/api/gogen/genconfig.go index 0e3f0562..a42442b9 100644 --- a/tools/goctl/api/gogen/genconfig.go +++ b/tools/goctl/api/gogen/genconfig.go @@ -60,8 +60,9 @@ func genConfig(dir string, api *spec.ApiSpec) error { "auth": strings.Join(auths, "\n"), }) if err != nil { - return nil + return err } + formatCode := formatCode(buffer.String()) _, err = fp.WriteString(formatCode) return err diff --git a/tools/goctl/api/gogen/genetc.go b/tools/goctl/api/gogen/genetc.go index ad6b312f..bc2398b0 100644 --- a/tools/goctl/api/gogen/genetc.go +++ b/tools/goctl/api/gogen/genetc.go @@ -55,6 +55,7 @@ func genEtc(dir string, api *spec.ApiSpec) error { if err != nil { return err } + formatCode := formatCode(buffer.String()) _, err = fp.WriteString(formatCode) return err diff --git a/tools/goctl/api/gogen/genhandlers.go b/tools/goctl/api/gogen/genhandlers.go index 34a7649e..7c94b0d8 100644 --- a/tools/goctl/api/gogen/genhandlers.go +++ b/tools/goctl/api/gogen/genhandlers.go @@ -103,7 +103,7 @@ func doGenToFile(dir, handler string, group spec.Group, route spec.Route, handle buffer := new(bytes.Buffer) err = template.Must(template.New("handlerTemplate").Parse(text)).Execute(buffer, handleObj) if err != nil { - return nil + return err } formatCode := formatCode(buffer.String()) diff --git a/tools/goctl/api/gogen/genmain.go b/tools/goctl/api/gogen/genmain.go index 9b19af3c..e37480f5 100644 --- a/tools/goctl/api/gogen/genmain.go +++ b/tools/goctl/api/gogen/genmain.go @@ -72,8 +72,9 @@ func genMain(dir string, api *spec.ApiSpec) error { "serviceName": api.Service.Name, }) if err != nil { - return nil + return err } + formatCode := formatCode(buffer.String()) _, err = fp.WriteString(formatCode) return err diff --git a/tools/goctl/api/gogen/genmiddleware.go b/tools/goctl/api/gogen/genmiddleware.go index 642a039c..c6991f8e 100644 --- a/tools/goctl/api/gogen/genmiddleware.go +++ b/tools/goctl/api/gogen/genmiddleware.go @@ -49,8 +49,9 @@ func genMiddleware(dir string, middlewares []string) error { "name": strings.Title(name), }) if err != nil { - return nil + return err } + formatCode := formatCode(buffer.String()) _, err = fp.WriteString(formatCode) return err diff --git a/tools/goctl/api/gogen/genroutes.go b/tools/goctl/api/gogen/genroutes.go index f8303778..d63d3dfa 100644 --- a/tools/goctl/api/gogen/genroutes.go +++ b/tools/goctl/api/gogen/genroutes.go @@ -52,7 +52,7 @@ type ( jwtEnabled bool signatureEnabled bool authName string - middleware []string + middlewares []string } route struct { method string @@ -92,9 +92,9 @@ func genRoutes(dir string, api *spec.ApiSpec, force bool) error { } var routes string - if len(g.middleware) > 0 { + if len(g.middlewares) > 0 { gbuilder.WriteString("\n}...,") - var params = g.middleware + var params = g.middlewares for i := range params { params[i] = "serverCtx." + params[i] } @@ -143,8 +143,9 @@ func genRoutes(dir string, api *spec.ApiSpec, force bool) error { "routesAdditions": strings.TrimSpace(builder.String()), }) if err != nil { - return nil + return err } + formatCode := formatCode(buffer.String()) _, err = fp.WriteString(formatCode) return err @@ -206,7 +207,7 @@ func getRoutes(api *spec.ApiSpec) ([]group, error) { } if value, ok := apiutil.GetAnnotationValue(g.Annotations, "server", "middleware"); ok { for _, item := range strings.Split(value, ",") { - groupedRoutes.middleware = append(groupedRoutes.middleware, item) + groupedRoutes.middlewares = append(groupedRoutes.middlewares, item) } } routes = append(routes, groupedRoutes) diff --git a/tools/goctl/api/gogen/gensvc.go b/tools/goctl/api/gogen/gensvc.go index 7e7176e9..aba4d9f2 100644 --- a/tools/goctl/api/gogen/gensvc.go +++ b/tools/goctl/api/gogen/gensvc.go @@ -90,8 +90,9 @@ func genServiceContext(dir string, api *spec.ApiSpec) error { "middlewareAssignment": middlewareAssignment, }) if err != nil { - return nil + return err } + formatCode := formatCode(buffer.String()) _, err = fp.WriteString(formatCode) return err diff --git a/tools/goctl/api/gogen/gentypes.go b/tools/goctl/api/gogen/gentypes.go index 71421d07..99469ffe 100644 --- a/tools/goctl/api/gogen/gentypes.go +++ b/tools/goctl/api/gogen/gentypes.go @@ -71,8 +71,9 @@ func genTypes(dir string, api *spec.ApiSpec, force bool) error { "containsTime": api.ContainsTime(), }) if err != nil { - return nil + return err } + formatCode := formatCode(buffer.String()) _, err = fp.WriteString(formatCode) return err