@ -1,7 +1,10 @@
package parser
package parser
import (
import (
"bufio"
"bytes"
"fmt"
"fmt"
"io"
"strings"
"strings"
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
@ -54,32 +57,49 @@ type serviceEntityParser struct {
}
}
func ( p * serviceEntityParser ) parseLine ( line string , api * spec . ApiSpec , annos [ ] spec . Annotation ) error {
func ( p * serviceEntityParser ) parseLine ( line string , api * spec . ApiSpec , annos [ ] spec . Annotation ) error {
fields := strings . Fields ( line )
line = strings . TrimSpace ( line )
if len ( fields ) < 2 {
return fmt . Errorf ( "wrong line %q" , line )
var buffer = new ( bytes . Buffer )
buffer . WriteString ( line )
reader := bufio . NewReader ( buffer )
var builder strings . Builder
var fields [ ] string
for {
ch , _ , err := reader . ReadRune ( )
if err != nil {
if err == io . EOF {
break
}
return err
}
}
method := fields [ 0 ]
switch {
pathAndRequest := fields [ 1 ]
case isSpace ( ch ) , ch == leftParenthesis , ch == rightParenthesis , ch == semicolon :
pos := strings . Index ( pathAndRequest , "(" )
if builder . Len ( ) == 0 {
if pos < 0 {
continue
return fmt . Errorf ( "wrong line %q" , line )
}
}
path := strings . TrimSpace ( pathAndRequest [ : pos ] )
token := builder . String ( )
pathAndRequest = pathAndRequest [ pos + 1 : ]
builder . Reset ( )
pos = strings . Index ( pathAndRequest , ")" )
fields = append ( fields , token )
if pos < 0 {
default :
return fmt . Errorf ( "wrong line %q" , line )
builder . WriteRune ( ch )
}
}
req := pathAndRequest [ : pos ]
}
if len ( fields ) < 3 {
return fmt . Errorf ( "wrong line %q, %q" , line , routeSyntax )
}
method := fields [ 0 ]
path := fields [ 1 ]
req := fields [ 2 ]
var returns string
var returns string
if len ( fields ) > 2 {
if len ( fields ) > 4 {
returns = fields [ 2 ]
returns = fields [ 4 ]
if fields [ 3 ] != returnsTag {
return fmt . Errorf ( "wrong line %q, %q" , line , routeSyntax )
}
}
}
returns = strings . ReplaceAll ( returns , "returns" , "" )
returns = strings . ReplaceAll ( returns , "(" , "" )
returns = strings . ReplaceAll ( returns , ")" , "" )
returns = strings . TrimSpace ( returns )
p . acceptRoute ( spec . Route {
p . acceptRoute ( spec . Route {
Annotations : annos ,
Annotations : annos ,