optimize route parse (#70)

* rebase upstream

* rebase

* trim no need line

* trim no need line

* trim no need line

* update doc

* remove update

* optimized route parser

Co-authored-by: kingxt <dream4kingxt@163.com>
master
kingxt 4 years ago committed by GitHub
parent 2896ef1a49
commit ee45b0a459
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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
}
switch {
case isSpace(ch), ch == leftParenthesis, ch == rightParenthesis, ch == semicolon:
if builder.Len() == 0 {
continue
}
token := builder.String()
builder.Reset()
fields = append(fields, token)
default:
builder.WriteRune(ch)
}
}
if len(fields) < 3 {
return fmt.Errorf("wrong line %q, %q", line, routeSyntax)
} }
method := fields[0] method := fields[0]
pathAndRequest := fields[1] path := fields[1]
pos := strings.Index(pathAndRequest, "(") req := fields[2]
if pos < 0 {
return fmt.Errorf("wrong line %q", line)
}
path := strings.TrimSpace(pathAndRequest[:pos])
pathAndRequest = pathAndRequest[pos+1:]
pos = strings.Index(pathAndRequest, ")")
if pos < 0 {
return fmt.Errorf("wrong line %q", line)
}
req := pathAndRequest[:pos]
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,

@ -5,6 +5,8 @@ const (
serviceDirective = "service" serviceDirective = "service"
typeDirective = "type" typeDirective = "type"
typeStruct = "struct" typeStruct = "struct"
routeSyntax = "route syntax: [get/post/delete] /path(request) returns[(response)]"
returnsTag = "returns"
at = '@' at = '@'
colon = ':' colon = ':'
leftParenthesis = '(' leftParenthesis = '('
@ -13,4 +15,5 @@ const (
rightBrace = '}' rightBrace = '}'
multilineBeginTag = '>' multilineBeginTag = '>'
multilineEndTag = '<' multilineEndTag = '<'
semicolon = ';'
) )

Loading…
Cancel
Save