refactor (#878)
* refactor(tools ): refactor cod Improve code readability and performance * fix(tools ): fix len bug Co-authored-by: ksco <hyang@33.cn>
This commit is contained in:
@@ -45,7 +45,7 @@ func (v *ApiVisitor) VisitApi(ctx *api.ApiContext) interface{} {
|
|||||||
func (v *ApiVisitor) acceptService(root, final *Api) {
|
func (v *ApiVisitor) acceptService(root, final *Api) {
|
||||||
for _, service := range root.Service {
|
for _, service := range root.Service {
|
||||||
if _, ok := final.serviceM[service.ServiceApi.Name.Text()]; !ok && len(final.serviceM) > 0 {
|
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)
|
v.duplicateServerItemCheck(service)
|
||||||
|
|
||||||
@@ -75,11 +75,11 @@ func (v *ApiVisitor) acceptService(root, final *Api) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if handlerExpr == nil {
|
if handlerExpr == nil {
|
||||||
v.panic(route.Route.Method, fmt.Sprintf("mismtached handler"))
|
v.panic(route.Route.Method, "mismatched handler")
|
||||||
}
|
}
|
||||||
|
|
||||||
if handlerExpr.Text() == "" {
|
if handlerExpr.Text() == "" {
|
||||||
v.panic(handlerExpr, fmt.Sprintf("mismtached handler"))
|
v.panic(handlerExpr, "mismatched handler")
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := final.handlerM[handlerExpr.Text()]; ok {
|
if _, ok := final.handlerM[handlerExpr.Text()]; ok {
|
||||||
@@ -119,7 +119,7 @@ func (v *ApiVisitor) acceptInfo(root, final *Api) {
|
|||||||
if root.Info != nil {
|
if root.Info != nil {
|
||||||
infoM := map[string]PlaceHolder{}
|
infoM := map[string]PlaceHolder{}
|
||||||
if final.Info != nil {
|
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 {
|
for _, value := range root.Info.Kvs {
|
||||||
@@ -147,7 +147,7 @@ func (v *ApiVisitor) acceptImport(root, final *Api) {
|
|||||||
func (v *ApiVisitor) acceptSyntax(root, final *Api) {
|
func (v *ApiVisitor) acceptSyntax(root, final *Api) {
|
||||||
if root.Syntax != nil {
|
if root.Syntax != nil {
|
||||||
if final.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
|
final.Syntax = root.Syntax
|
||||||
|
|||||||
@@ -193,11 +193,7 @@ func (e *defaultExpr) Stop() int {
|
|||||||
|
|
||||||
func (e *defaultExpr) Equal(expr Expr) bool {
|
func (e *defaultExpr) Equal(expr Expr) bool {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
if expr != nil {
|
return expr == nil
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if 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 {
|
func (v *ApiVisitor) getHiddenTokensToLeft(t TokenStream, channel int, containsCommentOfDefaultChannel bool) []Expr {
|
||||||
ct := t.GetParser().GetTokenStream().(*antlr.CommonTokenStream)
|
ct := t.GetParser().GetTokenStream().(*antlr.CommonTokenStream)
|
||||||
tokens := ct.GetHiddenTokensToLeft(t.GetStart().GetTokenIndex(), channel)
|
tokens := ct.GetHiddenTokensToLeft(t.GetStart().GetTokenIndex(), channel)
|
||||||
var tmp []antlr.Token
|
tmp := make([]antlr.Token, len(tokens))
|
||||||
for _, each := range tokens {
|
copy(tmp, tokens)
|
||||||
tmp = append(tmp, each)
|
|
||||||
}
|
|
||||||
|
|
||||||
var list []Expr
|
var list []Expr
|
||||||
for _, each := range tmp {
|
for _, each := range tmp {
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func (p parser) convert2Spec() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p parser) fillInfo() {
|
func (p parser) fillInfo() {
|
||||||
properties := make(map[string]string, 0)
|
properties := make(map[string]string)
|
||||||
if p.ast.Info != nil {
|
if p.ast.Info != nil {
|
||||||
p.spec.Info = spec.Info{}
|
p.spec.Info = spec.Info{}
|
||||||
for _, kv := range p.ast.Info.Kvs {
|
for _, kv := range p.ast.Info.Kvs {
|
||||||
@@ -258,7 +258,7 @@ func (p parser) fillService() error {
|
|||||||
route.ResponseType = p.astTypeToSpec(astRoute.Route.Reply.Name)
|
route.ResponseType = p.astTypeToSpec(astRoute.Route.Reply.Name)
|
||||||
}
|
}
|
||||||
if astRoute.AtDoc != nil {
|
if astRoute.AtDoc != nil {
|
||||||
properties := make(map[string]string, 0)
|
properties := make(map[string]string)
|
||||||
for _, kv := range astRoute.AtDoc.Kv {
|
for _, kv := range astRoute.AtDoc.Kv {
|
||||||
properties[kv.Key.Text()] = kv.Value.Text()
|
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 {
|
func (p parser) fillRouteAtServer(astRoute *ast.ServiceRoute, route *spec.Route) error {
|
||||||
if astRoute.AtServer != nil {
|
if astRoute.AtServer != nil {
|
||||||
properties := make(map[string]string, 0)
|
properties := make(map[string]string)
|
||||||
for _, kv := range astRoute.AtServer.Kv {
|
for _, kv := range astRoute.AtServer.Kv {
|
||||||
properties[kv.Key.Text()] = kv.Value.Text()
|
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) {
|
func (p parser) fillAtServer(item *ast.Service, group *spec.Group) {
|
||||||
if item.AtServer != nil {
|
if item.AtServer != nil {
|
||||||
properties := make(map[string]string, 0)
|
properties := make(map[string]string)
|
||||||
for _, kv := range item.AtServer.Kv {
|
for _, kv := range item.AtServer.Kv {
|
||||||
properties[kv.Key.Text()] = kv.Value.Text()
|
properties[kv.Key.Text()] = kv.Value.Text()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ func mkdir(ctx *ctx.ProjectContext, proto parser.Proto) (DirContext, error) {
|
|||||||
svcDir := filepath.Join(internalDir, "svc")
|
svcDir := filepath.Join(internalDir, "svc")
|
||||||
pbDir := filepath.Join(ctx.WorkDir, proto.GoPackage)
|
pbDir := filepath.Join(ctx.WorkDir, proto.GoPackage)
|
||||||
callDir := filepath.Join(ctx.WorkDir, strings.ToLower(stringx.From(proto.Service.Name).ToCamel()))
|
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()))
|
callDir = filepath.Join(ctx.WorkDir, strings.ToLower(stringx.From(proto.Service.Name+"_client").ToCamel()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user