master
anqiansong 4 years ago committed by GitHub
parent ea01cc78f0
commit 9f6a574f97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -86,12 +86,17 @@ func ApiFormatByPath(apiFilePath string) error {
return err
}
result, err := apiFormat(string(data))
abs, err := filepath.Abs(apiFilePath)
if err != nil {
return err
}
result, err := apiFormat(string(data), abs)
if err != nil {
return err
}
_, err = parser.ParseContent(result)
_, err = parser.ParseContent(result, abs)
if err != nil {
return err
}
@ -99,8 +104,8 @@ func ApiFormatByPath(apiFilePath string) error {
return ioutil.WriteFile(apiFilePath, []byte(result), os.ModePerm)
}
func apiFormat(data string) (string, error) {
_, err := parser.ParseContent(data)
func apiFormat(data string, filename ...string) (string, error) {
_, err := parser.ParseContent(data, filename...)
if err != nil {
return "", err
}

@ -71,6 +71,12 @@ func (p *Parser) Accept(fn func(p *api.ApiParserParser, visitor *ApiVisitor) int
// Parse is used to parse the api from the specified file name
func (p *Parser) Parse(filename string) (*Api, error) {
abs, err := filepath.Abs(filename)
if err != nil {
return nil, err
}
p.src = abs
data, err := p.readContent(filename)
if err != nil {
return nil, err
@ -80,8 +86,19 @@ func (p *Parser) Parse(filename string) (*Api, error) {
}
// ParseContent is used to parse the api from the specified content
func (p *Parser) ParseContent(content string) (*Api, error) {
return p.parse("", content)
func (p *Parser) ParseContent(content string, filename ...string) (*Api, error) {
var f string
if len(filename) > 0 {
f = filename[0]
abs, err := filepath.Abs(f)
if err != nil {
return nil, err
}
p.src = abs
}
return p.parse(f, content)
}
// parse is used to parse api from the content
@ -417,13 +434,7 @@ func (p *Parser) checkType(linePrefix string, types map[string]TypeExpr, expr Da
func (p *Parser) readContent(filename string) (string, error) {
filename = strings.ReplaceAll(filename, `"`, "")
abs, err := filepath.Abs(filename)
if err != nil {
return "", err
}
p.src = abs
data, err := ioutil.ReadFile(abs)
data, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}

@ -34,9 +34,9 @@ func Parse(filename string) (*spec.ApiSpec, error) {
}
// ParseContent parses the api content
func ParseContent(content string) (*spec.ApiSpec, error) {
func ParseContent(content string, filename ...string) (*spec.ApiSpec, error) {
astParser := ast.NewParser()
ast, err := astParser.ParseContent(content)
ast, err := astParser.ParseContent(content, filename...)
if err != nil {
return nil, err
}

@ -64,8 +64,13 @@ func (g *DefaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf
return err
}
etcFileName, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
if err != nil {
return err
}
return util.With("main").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
"serviceName": strings.ToLower(ctx.GetServiceName().ToCamel()),
"serviceName": etcFileName,
"imports": strings.Join(imports, util.NL),
"pkg": proto.PbPackage,
"serviceNew": stringx.From(proto.Service.Name).ToCamel(),

Loading…
Cancel
Save