From cc264dcf55dd22436a4831196b8b8f3b834101bc Mon Sep 17 00:00:00 2001 From: _ksco Date: Wed, 11 Aug 2021 17:33:18 +0800 Subject: [PATCH] refactor (#878) * refactor(tools ): refactor cod Improve code readability and performance * fix(tools ): fix len bug Co-authored-by: ksco --- tools/goctl/api/parser/g4/ast/api.go | 10 +++++----- tools/goctl/api/parser/g4/ast/ast.go | 12 +++--------- tools/goctl/api/parser/parser.go | 8 ++++---- tools/goctl/rpc/generator/mkdir.go | 2 +- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/tools/goctl/api/parser/g4/ast/api.go b/tools/goctl/api/parser/g4/ast/api.go index 2b601d87..4bf94732 100644 --- a/tools/goctl/api/parser/g4/ast/api.go +++ b/tools/goctl/api/parser/g4/ast/api.go @@ -45,7 +45,7 @@ func (v *ApiVisitor) VisitApi(ctx *api.ApiContext) interface{} { func (v *ApiVisitor) acceptService(root, final *Api) { for _, service := range root.Service { if _, ok := final.serviceM[service.ServiceApi.Name.Text()]; !ok && len(final.serviceM) > 0 { - v.panic(service.ServiceApi.Name, fmt.Sprintf("mutiple service declaration")) + v.panic(service.ServiceApi.Name, "multiple service declaration") } v.duplicateServerItemCheck(service) @@ -75,11 +75,11 @@ func (v *ApiVisitor) acceptService(root, final *Api) { } if handlerExpr == nil { - v.panic(route.Route.Method, fmt.Sprintf("mismtached handler")) + v.panic(route.Route.Method, "mismatched handler") } if handlerExpr.Text() == "" { - v.panic(handlerExpr, fmt.Sprintf("mismtached handler")) + v.panic(handlerExpr, "mismatched handler") } if _, ok := final.handlerM[handlerExpr.Text()]; ok { @@ -119,7 +119,7 @@ func (v *ApiVisitor) acceptInfo(root, final *Api) { if root.Info != nil { infoM := map[string]PlaceHolder{} if final.Info != nil { - v.panic(root.Info.Info, fmt.Sprintf("mutiple info declaration")) + v.panic(root.Info.Info, "multiple info declaration") } for _, value := range root.Info.Kvs { @@ -147,7 +147,7 @@ func (v *ApiVisitor) acceptImport(root, final *Api) { func (v *ApiVisitor) acceptSyntax(root, final *Api) { if root.Syntax != nil { if final.Syntax != nil { - v.panic(root.Syntax.Syntax, fmt.Sprintf("mutiple syntax declaration")) + v.panic(root.Syntax.Syntax, "multiple syntax declaration") } final.Syntax = root.Syntax diff --git a/tools/goctl/api/parser/g4/ast/ast.go b/tools/goctl/api/parser/g4/ast/ast.go index 7a722ad4..77126875 100644 --- a/tools/goctl/api/parser/g4/ast/ast.go +++ b/tools/goctl/api/parser/g4/ast/ast.go @@ -193,11 +193,7 @@ func (e *defaultExpr) Stop() int { func (e *defaultExpr) Equal(expr Expr) bool { if e == nil { - if expr != nil { - return false - } - - return true + return expr == nil } if expr == nil { @@ -277,10 +273,8 @@ func (v *ApiVisitor) getComment(t TokenStream) Expr { func (v *ApiVisitor) getHiddenTokensToLeft(t TokenStream, channel int, containsCommentOfDefaultChannel bool) []Expr { ct := t.GetParser().GetTokenStream().(*antlr.CommonTokenStream) tokens := ct.GetHiddenTokensToLeft(t.GetStart().GetTokenIndex(), channel) - var tmp []antlr.Token - for _, each := range tokens { - tmp = append(tmp, each) - } + tmp := make([]antlr.Token, len(tokens)) + copy(tmp, tokens) var list []Expr for _, each := range tmp { diff --git a/tools/goctl/api/parser/parser.go b/tools/goctl/api/parser/parser.go index bcb9c112..597fe9e7 100644 --- a/tools/goctl/api/parser/parser.go +++ b/tools/goctl/api/parser/parser.go @@ -64,7 +64,7 @@ func (p parser) convert2Spec() error { } func (p parser) fillInfo() { - properties := make(map[string]string, 0) + properties := make(map[string]string) if p.ast.Info != nil { p.spec.Info = spec.Info{} for _, kv := range p.ast.Info.Kvs { @@ -258,7 +258,7 @@ func (p parser) fillService() error { route.ResponseType = p.astTypeToSpec(astRoute.Route.Reply.Name) } if astRoute.AtDoc != nil { - properties := make(map[string]string, 0) + properties := make(map[string]string) for _, kv := range astRoute.AtDoc.Kv { properties[kv.Key.Text()] = kv.Value.Text() } @@ -290,7 +290,7 @@ func (p parser) fillService() error { func (p parser) fillRouteAtServer(astRoute *ast.ServiceRoute, route *spec.Route) error { if astRoute.AtServer != nil { - properties := make(map[string]string, 0) + properties := make(map[string]string) for _, kv := range astRoute.AtServer.Kv { properties[kv.Key.Text()] = kv.Value.Text() } @@ -314,7 +314,7 @@ func (p parser) fillRouteAtServer(astRoute *ast.ServiceRoute, route *spec.Route) func (p parser) fillAtServer(item *ast.Service, group *spec.Group) { if item.AtServer != nil { - properties := make(map[string]string, 0) + properties := make(map[string]string) for _, kv := range item.AtServer.Kv { properties[kv.Key.Text()] = kv.Value.Text() } diff --git a/tools/goctl/rpc/generator/mkdir.go b/tools/goctl/rpc/generator/mkdir.go index b1aa6ece..aa7b72e2 100644 --- a/tools/goctl/rpc/generator/mkdir.go +++ b/tools/goctl/rpc/generator/mkdir.go @@ -60,7 +60,7 @@ func mkdir(ctx *ctx.ProjectContext, proto parser.Proto) (DirContext, error) { svcDir := filepath.Join(internalDir, "svc") pbDir := filepath.Join(ctx.WorkDir, proto.GoPackage) callDir := filepath.Join(ctx.WorkDir, strings.ToLower(stringx.From(proto.Service.Name).ToCamel())) - if strings.ToLower(proto.Service.Name) == strings.ToLower(proto.GoPackage) { + if strings.EqualFold(proto.Service.Name, proto.GoPackage) { callDir = filepath.Join(ctx.WorkDir, strings.ToLower(stringx.From(proto.Service.Name+"_client").ToCamel())) }