fix(goctl): support hyphen-only api prefixes (#5747)

Co-authored-by: zihao_wang <zihao_wang@geelark.com>
Co-authored-by: kevin <wanjunfeng@gmail.com>
This commit is contained in:
Even
2026-09-04 17:52:19 +00:00
committed by GitHub
parent ba0c5096c5
commit c8ec158f44
3 changed files with 23 additions and 4 deletions
+13 -4
View File
@@ -1172,12 +1172,12 @@ func (p *Parser) parseAtServerKVExpression() *ast.KVExpr {
slashTok := p.curTok
var pathText = slashTok.Text
if !p.advanceIfPeekTokenIs(token.IDENT) {
if !p.advanceIfPeekTokenIs(token.IDENT, token.SUB) {
return nil
}
pathText += p.curTok.Text
if p.peekTokenIs(token.SUB) { // parse abc-efg format
if p.peekTokenIs(token.SUB) && p.curTokenIs(token.IDENT) { // parse abc-efg format
if !p.nextToken() {
return nil
}
@@ -1303,12 +1303,21 @@ func (p *Parser) parseAtServerKVExpression() *ast.KVExpr {
slashTok := p.curTok
var pathText = valueTok.Text
pathText += slashTok.Text
if !p.advanceIfPeekTokenIs(token.IDENT) {
// Allow a trailing slash at the end of an @server path value.
if p.peekTok.Line() > p.curTok.Line() || p.peekTokenIs(token.RPAREN) {
valueTok = token.Token{
Text: pathText,
Position: valueTok.Position,
}
leadingCommentGroup = p.curTokenNode().LeadingCommentGroup
break
}
if !p.advanceIfPeekTokenIs(token.IDENT, token.SUB) {
return nil
}
pathText += p.curTok.Text
if p.peekTokenIs(token.SUB) { // parse abc-efg format
if p.peekTokenIs(token.SUB) && p.curTokenIs(token.IDENT) { // parse abc-efg format
if !p.nextToken() {
return nil
}
@@ -306,6 +306,10 @@ func TestParser_Parse_atServerStmt(t *testing.T) {
"prefix2": "v1/v2_test/v2-beta",
"prefix3": "v1/v2_",
"prefix4": "a-b-c",
"prefix5": "/-",
"prefix6": "/-/",
"prefix7": "/abc/",
"prefix8": "/comment/",
"summary": `"test"`,
"key": `"bar"`,
}
@@ -359,6 +363,8 @@ func TestParser_Parse_atServerStmt(t *testing.T) {
`@server(foo: m1,`,
`@server(foo: m1,)`,
`@server(foo: v1/v2-)`,
`@server(prefix:/--)`,
`@server(prefix:/-abc)`,
`@server(foo:"test")`,
}
for _, v := range testData {
@@ -18,6 +18,10 @@
prefix2: v1/v2_test/v2-beta
prefix3: v1/v2_
prefix4: a-b-c
prefix5: /-
prefix6: /-/
prefix7: /abc/
prefix8: /comment/ // trailing slash before a comment and the next key
summary:"test"
key:"bar"
)