support type def without struct token (#210)
* add comment support * add comment support * 1. group support multi level folder 2. remove force flag * bug fix * refactor parser and remove deprecated code * refactor parser and remove deprecated code * refactor parser and remove deprecated code * refactor parser and remove deprecated code * refactor parser and remove deprecated code * refactor parser and remove deprecated code * refactor parser and remove deprecated code * support type def without struct token * support type def without struct token * support type def without struct token * support type def without struct token * support type def without struct token * support type def without struct token * support type def without struct token * optimized * optimized * optimized Co-authored-by: kim <xutao@xiaoheiban.cn>
This commit is contained in:
@@ -7,6 +7,9 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -15,6 +18,7 @@ const (
|
||||
tokenType = "type"
|
||||
tokenService = "service"
|
||||
tokenServiceAnnotation = "@server"
|
||||
tokenStruct = "struct"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -72,7 +76,7 @@ func ParseApi(src string) (*ApiStruct, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *apiRootState) process(api *ApiStruct, token string) (apiFileState, error) {
|
||||
func (s *apiRootState) process(api *ApiStruct, _ string) (apiFileState, error) {
|
||||
var builder strings.Builder
|
||||
for {
|
||||
ch, err := s.readSkipComment()
|
||||
@@ -124,7 +128,7 @@ func (s *apiInfoState) process(api *ApiStruct, token string) (apiFileState, erro
|
||||
return nil, err
|
||||
}
|
||||
|
||||
api.Info += "\n" + token + line
|
||||
api.Info += newline + token + line
|
||||
token = ""
|
||||
if strings.TrimSpace(line) == string(rightParenthesis) {
|
||||
return &apiRootState{s.baseState}, nil
|
||||
@@ -139,12 +143,12 @@ func (s *apiImportState) process(api *ApiStruct, token string) (apiFileState, er
|
||||
}
|
||||
|
||||
line = token + line
|
||||
line = removeComment(line)
|
||||
line = util.RemoveComment(line)
|
||||
if len(strings.Fields(line)) != 2 {
|
||||
return nil, errors.New("import syntax error: " + line)
|
||||
}
|
||||
|
||||
api.Imports += "\n" + line
|
||||
api.Imports += newline + line
|
||||
return &apiRootState{s.baseState}, nil
|
||||
}
|
||||
|
||||
@@ -156,11 +160,14 @@ func (s *apiTypeState) process(api *ApiStruct, token string) (apiFileState, erro
|
||||
return nil, err
|
||||
}
|
||||
|
||||
api.Type += "\n\n" + token + line
|
||||
line = token + line
|
||||
if blockCount <= 1 {
|
||||
line = mayInsertStructKeyword(line)
|
||||
}
|
||||
api.Type += newline + newline + line
|
||||
line = strings.TrimSpace(line)
|
||||
line = util.RemoveComment(line)
|
||||
token = ""
|
||||
line = strings.TrimSpace(line)
|
||||
line = removeComment(line)
|
||||
line = strings.TrimSpace(line)
|
||||
|
||||
if strings.HasSuffix(line, leftBrace) {
|
||||
blockCount++
|
||||
@@ -191,10 +198,9 @@ func (s *apiServiceState) process(api *ApiStruct, token string) (apiFileState, e
|
||||
|
||||
line = token + line
|
||||
token = ""
|
||||
api.Service += "\n" + line
|
||||
line = strings.TrimSpace(line)
|
||||
line = removeComment(line)
|
||||
api.Service += newline + line
|
||||
line = strings.TrimSpace(line)
|
||||
line = util.RemoveComment(line)
|
||||
|
||||
if strings.HasSuffix(line, leftBrace) {
|
||||
blockCount++
|
||||
@@ -215,10 +221,30 @@ func (s *apiServiceState) process(api *ApiStruct, token string) (apiFileState, e
|
||||
}
|
||||
}
|
||||
|
||||
func removeComment(line string) string {
|
||||
var commentIdx = strings.Index(line, "//")
|
||||
if commentIdx >= 0 {
|
||||
return line[:commentIdx]
|
||||
func mayInsertStructKeyword(line string) string {
|
||||
line = util.RemoveComment(line)
|
||||
if !strings.HasSuffix(line, leftBrace) {
|
||||
return line
|
||||
}
|
||||
return line
|
||||
|
||||
fields := strings.Fields(line)
|
||||
if stringx.Contains(fields, tokenStruct) || stringx.Contains(fields, tokenStruct+leftBrace) || len(fields) <= 1 {
|
||||
return line
|
||||
}
|
||||
|
||||
var insertIndex int
|
||||
if fields[0] == tokenType {
|
||||
insertIndex = 2
|
||||
} else {
|
||||
insertIndex = 1
|
||||
}
|
||||
if insertIndex >= len(fields) {
|
||||
return line
|
||||
}
|
||||
|
||||
var result []string
|
||||
result = append(result, fields[:insertIndex]...)
|
||||
result = append(result, tokenStruct)
|
||||
result = append(result, fields[insertIndex:]...)
|
||||
return strings.Join(result, " ")
|
||||
}
|
||||
|
||||
@@ -15,9 +15,8 @@ import (
|
||||
)
|
||||
|
||||
type Parser struct {
|
||||
r *bufio.Reader
|
||||
typeDef string
|
||||
api *ApiStruct
|
||||
r *bufio.Reader
|
||||
api *ApiStruct
|
||||
}
|
||||
|
||||
func NewParser(filename string) (*Parser, error) {
|
||||
@@ -73,15 +72,14 @@ func NewParser(filename string) (*Parser, error) {
|
||||
var buffer = new(bytes.Buffer)
|
||||
buffer.WriteString(apiStruct.Service)
|
||||
return &Parser{
|
||||
r: bufio.NewReader(buffer),
|
||||
typeDef: apiStruct.Type,
|
||||
api: apiStruct,
|
||||
r: bufio.NewReader(buffer),
|
||||
api: apiStruct,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *Parser) Parse() (api *spec.ApiSpec, err error) {
|
||||
api = new(spec.ApiSpec)
|
||||
var sp = StructParser{Src: p.typeDef}
|
||||
var sp = StructParser{Src: p.api.Type}
|
||||
types, err := sp.Parse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -16,4 +16,5 @@ const (
|
||||
multilineBeginTag = '>'
|
||||
multilineEndTag = '<'
|
||||
semicolon = ';'
|
||||
newline = "\n"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user