Skip to content

Commit

Permalink
fix parse error case root path /
Browse files Browse the repository at this point in the history
  • Loading branch information
kesonan committed Jan 9, 2024
1 parent 198fa28 commit bc98f35
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tools/goctl/pkg/parser/api/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ func (p *Parser) parsePathExpr() *ast.PathExpr {
}

values = append(values, p.curTok)
if p.peekTokenIs(token.LPAREN, token.Returns, token.AT_DOC, token.AT_HANDLER, token.SEMICOLON, token.RBRACE){
break
}
if p.notExpectPeekTokenGotComment(p.curTokenNode().PeekFirstLeadingComment(), token.COLON, token.IDENT, token.INT) {
return nil
}
Expand Down
100 changes: 100 additions & 0 deletions tools/goctl/pkg/parser/api/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,19 @@ func TestParser_Parse_service(t *testing.T) {
LBrace: ast.NewTokenNode(token.Token{Type: token.LBRACE, Text: "{"}),
RBrace: ast.NewTokenNode(token.Token{Type: token.RBRACE, Text: "}"}),
Routes: []*ast.ServiceItemStmt{
{
AtHandler: &ast.AtHandlerStmt{
AtHandler: ast.NewTokenNode(token.Token{Type: token.AT_HANDLER, Text: "@handler"}),
Name: ast.NewTokenNode(token.Token{Type: token.IDENT, Text: "root"}),
},
Route: &ast.RouteStmt{
Method: ast.NewTokenNode(token.Token{Type: token.IDENT, Text: "get"}),
Path: &ast.PathExpr{Value: ast.NewTokenNode(token.Token{
Type: token.PATH,
Text: "/",
})},
},
},
{
AtHandler: &ast.AtHandlerStmt{
AtHandler: ast.NewTokenNode(token.Token{Type: token.AT_HANDLER, Text: "@handler"}),
Expand Down Expand Up @@ -557,6 +570,93 @@ func TestParser_Parse_service(t *testing.T) {
LBrace: ast.NewTokenNode(token.Token{Type: token.LBRACE, Text: "{"}),
RBrace: ast.NewTokenNode(token.Token{Type: token.RBRACE, Text: "}"}),
Routes: []*ast.ServiceItemStmt{
{
AtDoc: &ast.AtDocLiteralStmt{
AtDoc: ast.NewTokenNode(token.Token{Type: token.AT_DOC, Text: "@doc"}),
Value: ast.NewTokenNode(token.Token{Type: token.STRING, Text: `"bar"`}),
},
AtHandler: &ast.AtHandlerStmt{
AtHandler: ast.NewTokenNode(token.Token{Type: token.AT_HANDLER, Text: "@handler"}),
Name: ast.NewTokenNode(token.Token{Type: token.IDENT, Text: "root"}),
},
Route: &ast.RouteStmt{
Method: ast.NewTokenNode(token.Token{Type: token.IDENT, Text: "get"}),
Path: &ast.PathExpr{
Value: ast.NewTokenNode(token.Token{
Type: token.PATH,
Text: "/",
}),
},
Request: &ast.BodyStmt{
LParen: ast.NewTokenNode(token.Token{Type: token.LPAREN, Text: "("}),
Body: &ast.BodyExpr{
Value: ast.NewTokenNode(token.Token{Type: token.IDENT, Text: "Foo"}),
},
RParen: ast.NewTokenNode(token.Token{Type: token.RPAREN, Text: ")"}),
},
},
},
{
AtDoc: &ast.AtDocLiteralStmt{
AtDoc: ast.NewTokenNode(token.Token{Type: token.AT_DOC, Text: "@doc"}),
Value: ast.NewTokenNode(token.Token{Type: token.STRING, Text: `"bar"`}),
},
AtHandler: &ast.AtHandlerStmt{
AtHandler: ast.NewTokenNode(token.Token{Type: token.AT_HANDLER, Text: "@handler"}),
Name: ast.NewTokenNode(token.Token{Type: token.IDENT, Text: "root2"}),
},
Route: &ast.RouteStmt{
Method: ast.NewTokenNode(token.Token{Type: token.IDENT, Text: "get"}),
Path: &ast.PathExpr{
Value: ast.NewTokenNode(token.Token{
Type: token.PATH,
Text: "/",
}),
},
Returns: ast.NewTokenNode(token.Token{Type: token.IDENT, Text: "returns"}),
Response: &ast.BodyStmt{
LParen: ast.NewTokenNode(token.Token{Type: token.LPAREN, Text: "("}),
Body: &ast.BodyExpr{
Value: ast.NewTokenNode(token.Token{Type: token.IDENT, Text: "Foo"}),
},
RParen: ast.NewTokenNode(token.Token{Type: token.RPAREN, Text: ")"}),
},
},
},
{
AtDoc: &ast.AtDocLiteralStmt{
AtDoc: ast.NewTokenNode(token.Token{Type: token.AT_DOC, Text: "@doc"}),
Value: ast.NewTokenNode(token.Token{Type: token.STRING, Text: `"bar"`}),
},
AtHandler: &ast.AtHandlerStmt{
AtHandler: ast.NewTokenNode(token.Token{Type: token.AT_HANDLER, Text: "@handler"}),
Name: ast.NewTokenNode(token.Token{Type: token.IDENT, Text: "root3"}),
},
Route: &ast.RouteStmt{
Method: ast.NewTokenNode(token.Token{Type: token.IDENT, Text: "get"}),
Path: &ast.PathExpr{
Value: ast.NewTokenNode(token.Token{
Type: token.PATH,
Text: "/",
}),
},
Request: &ast.BodyStmt{
LParen: ast.NewTokenNode(token.Token{Type: token.LPAREN, Text: "("}),
Body: &ast.BodyExpr{
Value: ast.NewTokenNode(token.Token{Type: token.IDENT, Text: "Foo"}),
},
RParen: ast.NewTokenNode(token.Token{Type: token.RPAREN, Text: ")"}),
},
Returns: ast.NewTokenNode(token.Token{Type: token.IDENT, Text: "returns"}),
Response: &ast.BodyStmt{
LParen: ast.NewTokenNode(token.Token{Type: token.LPAREN, Text: "("}),
Body: &ast.BodyExpr{
Value: ast.NewTokenNode(token.Token{Type: token.IDENT, Text: "Bar"}),
},
RParen: ast.NewTokenNode(token.Token{Type: token.RPAREN, Text: ")"}),
},
},
},
{
AtDoc: &ast.AtDocLiteralStmt{
AtDoc: ast.NewTokenNode(token.Token{Type: token.AT_DOC, Text: "@doc"}),
Expand Down
9 changes: 9 additions & 0 deletions tools/goctl/pkg/parser/api/parser/testdata/example.api
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ type (
NestDemoResp2 {
*Nest `json:"nest"`
}
RootReq{

}
RootResp{

}
)

@server (
Expand Down Expand Up @@ -130,6 +136,9 @@ service example {
)
@handler postPath
post /example/path (PostPathReq) returns (PostPathResp)

@handler root
post / (RootReq) returns (RootResp)
}

@server (
Expand Down
15 changes: 15 additions & 0 deletions tools/goctl/pkg/parser/api/parser/testdata/service_test.api
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
service foo {
@handler root
get /

@handler bar
get /ping

Expand All @@ -7,6 +10,18 @@ service foo {
}

service bar {
@doc "bar"
@handler root
get / (Foo)

@doc "bar"
@handler root2
get / returns (Foo)

@doc "bar"
@handler root3
get / (Foo) returns (Bar)

@doc "bar"
@handler foo
get /foo/:bar (Foo)
Expand Down

0 comments on commit bc98f35

Please sign in to comment.