mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
Support for referencing types in different API files using format (#1630)
This commit is contained in:
parent
209ffb934b
commit
c55694d957
@ -28,10 +28,11 @@ const (
|
|||||||
// GoFormatApi format api file
|
// GoFormatApi format api file
|
||||||
func GoFormatApi(c *cli.Context) error {
|
func GoFormatApi(c *cli.Context) error {
|
||||||
useStdin := c.Bool("stdin")
|
useStdin := c.Bool("stdin")
|
||||||
|
skipCheckDeclare := c.Bool("declare")
|
||||||
|
|
||||||
var be errorx.BatchError
|
var be errorx.BatchError
|
||||||
if useStdin {
|
if useStdin {
|
||||||
if err := apiFormatByStdin(); err != nil {
|
if err := apiFormatByStdin(skipCheckDeclare); err != nil {
|
||||||
be.Add(err)
|
be.Add(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -47,7 +48,7 @@ func GoFormatApi(c *cli.Context) error {
|
|||||||
|
|
||||||
err = filepath.Walk(dir, func(path string, fi os.FileInfo, errBack error) (err error) {
|
err = filepath.Walk(dir, func(path string, fi os.FileInfo, errBack error) (err error) {
|
||||||
if strings.HasSuffix(path, ".api") {
|
if strings.HasSuffix(path, ".api") {
|
||||||
if err := ApiFormatByPath(path); err != nil {
|
if err := ApiFormatByPath(path, skipCheckDeclare); err != nil {
|
||||||
be.Add(util.WrapErr(err, fi.Name()))
|
be.Add(util.WrapErr(err, fi.Name()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,13 +65,13 @@ func GoFormatApi(c *cli.Context) error {
|
|||||||
return be.Err()
|
return be.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func apiFormatByStdin() error {
|
func apiFormatByStdin(skipCheckDeclare bool) error {
|
||||||
data, err := ioutil.ReadAll(os.Stdin)
|
data, err := ioutil.ReadAll(os.Stdin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := apiFormat(string(data))
|
result, err := apiFormat(string(data), skipCheckDeclare)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -80,7 +81,7 @@ func apiFormatByStdin() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ApiFormatByPath format api from file path
|
// ApiFormatByPath format api from file path
|
||||||
func ApiFormatByPath(apiFilePath string) error {
|
func ApiFormatByPath(apiFilePath string, skipCheckDeclare bool) error {
|
||||||
data, err := ioutil.ReadFile(apiFilePath)
|
data, err := ioutil.ReadFile(apiFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -91,12 +92,12 @@ func ApiFormatByPath(apiFilePath string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := apiFormat(string(data), abs)
|
result, err := apiFormat(string(data), skipCheckDeclare, abs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = parser.ParseContent(result, abs)
|
_, err = parser.ParseContentWithParserSkipCheckTypeDeclaration(result, abs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -104,8 +105,13 @@ func ApiFormatByPath(apiFilePath string) error {
|
|||||||
return ioutil.WriteFile(apiFilePath, []byte(result), os.ModePerm)
|
return ioutil.WriteFile(apiFilePath, []byte(result), os.ModePerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
func apiFormat(data string, filename ...string) (string, error) {
|
func apiFormat(data string, skipCheckDeclare bool, filename ...string) (string, error) {
|
||||||
_, err := parser.ParseContent(data, filename...)
|
var err error
|
||||||
|
if skipCheckDeclare {
|
||||||
|
_, err = parser.ParseContentWithParserSkipCheckTypeDeclaration(data, filename...)
|
||||||
|
} else {
|
||||||
|
_, err = parser.ParseContent(data, filename...)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ type Request struct {
|
|||||||
}
|
}
|
||||||
type Response struct {
|
type Response struct {
|
||||||
Message string ` + "`" + `json:"message"` + "`" + `
|
Message string ` + "`" + `json:"message"` + "`" + `
|
||||||
|
Students []Student ` + "`" + `json:"students"` + "`" + `
|
||||||
}
|
}
|
||||||
service A-api {
|
service A-api {
|
||||||
@server(
|
@server(
|
||||||
@ -26,7 +27,8 @@ handler: GreetHandler
|
|||||||
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
||||||
}
|
}
|
||||||
type Response {
|
type Response {
|
||||||
Message string ` + "`" + `json:"message"` + "`" + `
|
Message string ` + "`" + `json:"message"` + "`" + `
|
||||||
|
Students []Student ` + "`" + `json:"students"` + "`" + `
|
||||||
}
|
}
|
||||||
service A-api {
|
service A-api {
|
||||||
@server(
|
@server(
|
||||||
@ -37,7 +39,9 @@ service A-api {
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestFormat(t *testing.T) {
|
func TestFormat(t *testing.T) {
|
||||||
r, err := apiFormat(notFormattedStr)
|
r, err := apiFormat(notFormattedStr, true)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, formattedStr, r)
|
assert.Equal(t, formattedStr, r)
|
||||||
|
_, err = apiFormat(notFormattedStr, false)
|
||||||
|
assert.Errorf(t, err, " line 7:13 can not found declaration 'Student' in context")
|
||||||
}
|
}
|
||||||
|
@ -86,7 +86,7 @@ func DoGenProject(apiFile, dir, style string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := apiformat.ApiFormatByPath(apiFile); err != nil {
|
if err := apiformat.ApiFormatByPath(apiFile, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,8 @@ type (
|
|||||||
debug bool
|
debug bool
|
||||||
log console.Console
|
log console.Console
|
||||||
antlr.DefaultErrorListener
|
antlr.DefaultErrorListener
|
||||||
src string
|
src string
|
||||||
|
skipCheckTypeDeclaration bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParserOption defines an function with argument Parser
|
// ParserOption defines an function with argument Parser
|
||||||
@ -136,9 +137,11 @@ func (p *Parser) parse(filename, content string) (*Api, error) {
|
|||||||
apiAstList = append(apiAstList, nestedApi)
|
apiAstList = append(apiAstList, nestedApi)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = p.checkTypeDeclaration(apiAstList)
|
if !p.skipCheckTypeDeclaration {
|
||||||
if err != nil {
|
err = p.checkTypeDeclaration(apiAstList)
|
||||||
return nil, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
allApi := p.memberFill(apiAstList)
|
allApi := p.memberFill(apiAstList)
|
||||||
@ -483,3 +486,9 @@ func WithParserPrefix(prefix string) ParserOption {
|
|||||||
p.linePrefix = prefix
|
p.linePrefix = prefix
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithParserSkipCheckTypeDeclaration() ParserOption {
|
||||||
|
return func(p *Parser) {
|
||||||
|
p.skipCheckTypeDeclaration = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -33,9 +33,13 @@ func Parse(filename string) (*spec.ApiSpec, error) {
|
|||||||
return spec, nil
|
return spec, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseContent parses the api content
|
func parseContent(content string, skipCheckTypeDeclaration bool, filename ...string) (*spec.ApiSpec, error) {
|
||||||
func ParseContent(content string, filename ...string) (*spec.ApiSpec, error) {
|
var astParser *ast.Parser
|
||||||
astParser := ast.NewParser()
|
if skipCheckTypeDeclaration {
|
||||||
|
astParser = ast.NewParser(ast.WithParserSkipCheckTypeDeclaration())
|
||||||
|
} else {
|
||||||
|
astParser = ast.NewParser()
|
||||||
|
}
|
||||||
ast, err := astParser.ParseContent(content, filename...)
|
ast, err := astParser.ParseContent(content, filename...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -51,6 +55,16 @@ func ParseContent(content string, filename ...string) (*spec.ApiSpec, error) {
|
|||||||
return spec, nil
|
return spec, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseContent parses the api content
|
||||||
|
func ParseContent(content string, filename ...string) (*spec.ApiSpec, error) {
|
||||||
|
return parseContent(content, false, filename...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseContentWithParserSkipCheckTypeDeclaration parses the api content with skip check type declaration
|
||||||
|
func ParseContentWithParserSkipCheckTypeDeclaration(content string, filename ...string) (*spec.ApiSpec, error) {
|
||||||
|
return parseContent(content, true, filename...)
|
||||||
|
}
|
||||||
|
|
||||||
func (p parser) convert2Spec() error {
|
func (p parser) convert2Spec() error {
|
||||||
p.fillInfo()
|
p.fillInfo()
|
||||||
p.fillSyntax()
|
p.fillSyntax()
|
||||||
|
@ -160,6 +160,10 @@ var commands = []cli.Command{
|
|||||||
Name: "stdin",
|
Name: "stdin",
|
||||||
Usage: "use stdin to input api doc content, press \"ctrl + d\" to send EOF",
|
Usage: "use stdin to input api doc content, press \"ctrl + d\" to send EOF",
|
||||||
},
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "declare",
|
||||||
|
Usage: "use to skip check api types already declare",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Action: format.GoFormatApi,
|
Action: format.GoFormatApi,
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user