You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/api/parser/parser.go

106 lines
2.2 KiB
Go

4 years ago
package parser
import (
"bufio"
"bytes"
"errors"
4 years ago
"fmt"
"io"
"io/ioutil"
"path/filepath"
"strings"
4 years ago
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/util"
4 years ago
)
type Parser struct {
r *bufio.Reader
typeDef string
api *ApiStruct
4 years ago
}
func NewParser(filename string) (*Parser, error) {
apiAbsPath, err := filepath.Abs(filename)
if err != nil {
return nil, err
}
4 years ago
api, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
apiStruct, err := ParseApi(string(api))
4 years ago
if err != nil {
return nil, err
}
for _, item := range strings.Split(apiStruct.Imports, "\n") {
importLine := strings.TrimSpace(item)
if len(importLine) > 0 {
item := strings.TrimPrefix(importLine, "import")
item = strings.TrimSpace(item)
item = strings.TrimPrefix(item, `"`)
item = strings.TrimSuffix(item, `"`)
var path = item
if !util.FileExists(item) {
path = filepath.Join(filepath.Dir(apiAbsPath), item)
}
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.New("import api file not exist: " + item)
}
importStruct, err := ParseApi(string(content))
if err != nil {
return nil, err
}
if len(importStruct.Imports) > 0 {
return nil, errors.New("import api should not import another api file recursive")
}
apiStruct.Type += "\n" + importStruct.Type
apiStruct.Service += "\n" + importStruct.Service
}
}
if len(strings.TrimSpace(apiStruct.Service)) == 0 {
return nil, errors.New("api has no service defined")
}
4 years ago
var buffer = new(bytes.Buffer)
buffer.WriteString(apiStruct.Service)
4 years ago
return &Parser{
r: bufio.NewReader(buffer),
typeDef: apiStruct.Type,
api: apiStruct,
4 years ago
}, nil
}
func (p *Parser) Parse() (api *spec.ApiSpec, err error) {
api = new(spec.ApiSpec)
var sp = StructParser{Src: p.typeDef}
types, err := sp.Parse()
4 years ago
if err != nil {
return nil, err
}
4 years ago
api.Types = types
var lineNumber = p.api.serviceBeginLine
4 years ago
st := newRootState(p.r, &lineNumber)
for {
st, err = st.process(api)
if err == io.EOF {
return api, p.validate(api)
}
if err != nil {
return nil, fmt.Errorf("near line: %d, %s", lineNumber, err.Error())
}
if st == nil {
return api, p.validate(api)
}
}
}