Explorar el Código

refactor (#878)

* refactor(tools ): refactor cod

Improve code readability and performance

* fix(tools ): fix len bug

Co-authored-by: ksco <hyang@33.cn>
_ksco hace 3 años
padre
commit
cc264dcf55

+ 5 - 5
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

+ 3 - 9
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 {

+ 4 - 4
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()
 		}

+ 1 - 1
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()))
 	}