瀏覽代碼

Mark deprecated syntax (#1148)

anqiansong 3 年之前
父節點
當前提交
a944a7fd7e
共有 2 個文件被更改,包括 21 次插入5 次删除
  1. 20 4
      tools/goctl/api/parser/g4/ast/service.go
  2. 1 1
      tools/goctl/api/parser/parser.go

+ 20 - 4
tools/goctl/api/parser/g4/ast/service.go

@@ -212,24 +212,40 @@ func (v *ApiVisitor) VisitRoute(ctx *api.RouteContext) interface{} {
 // VisitBody implements from api.BaseApiParserVisitor
 func (v *ApiVisitor) VisitBody(ctx *api.BodyContext) interface{} {
 	if ctx.ID() == nil {
+		if v.debug {
+			msg := fmt.Sprintf(
+				`%s line %d:  expr "()" is deprecated, if there has no request body, please omit it`,
+				v.prefix,
+				ctx.GetStart().GetLine(),
+			)
+			v.log.Warning(msg)
+		}
 		return nil
 	}
 
-	idRxpr := v.newExprWithTerminalNode(ctx.ID())
-	if api.IsGolangKeyWord(idRxpr.Text()) {
-		v.panic(idRxpr, fmt.Sprintf("expecting 'ID', but found golang keyword '%s'", idRxpr.Text()))
+	idExpr := v.newExprWithTerminalNode(ctx.ID())
+	if api.IsGolangKeyWord(idExpr.Text()) {
+		v.panic(idExpr, fmt.Sprintf("expecting 'ID', but found golang keyword '%s'", idExpr.Text()))
 	}
 
 	return &Body{
 		Lp:   v.newExprWithToken(ctx.GetLp()),
 		Rp:   v.newExprWithToken(ctx.GetRp()),
-		Name: &Literal{Literal: idRxpr},
+		Name: &Literal{Literal: idExpr},
 	}
 }
 
 // VisitReplybody implements from api.BaseApiParserVisitor
 func (v *ApiVisitor) VisitReplybody(ctx *api.ReplybodyContext) interface{} {
 	if ctx.DataType() == nil {
+		if v.debug {
+			msg := fmt.Sprintf(
+				`%s line %d:  expr "returns ()" or "()" is deprecated, if there has no response body, please omit it`,
+				v.prefix,
+				ctx.GetStart().GetLine(),
+			)
+			v.log.Warning(msg)
+		}
 		return nil
 	}
 

+ 1 - 1
tools/goctl/api/parser/parser.go

@@ -17,7 +17,7 @@ type parser struct {
 
 // Parse parses the api file
 func Parse(filename string) (*spec.ApiSpec, error) {
-	astParser := ast.NewParser(ast.WithParserPrefix(filepath.Base(filename)))
+	astParser := ast.NewParser(ast.WithParserPrefix(filepath.Base(filename)), ast.WithParserDebug())
 	ast, err := astParser.Parse(filename)
 	if err != nil {
 		return nil, err