Support for referencing types in different API files using format (#1630)

This commit is contained in:
chensy
2022-03-12 15:17:31 +08:00
committed by GitHub
parent 209ffb934b
commit c55694d957
6 changed files with 56 additions and 19 deletions

View File

@@ -19,7 +19,8 @@ type (
debug bool
log console.Console
antlr.DefaultErrorListener
src string
src string
skipCheckTypeDeclaration bool
}
// ParserOption defines an function with argument Parser
@@ -136,9 +137,11 @@ func (p *Parser) parse(filename, content string) (*Api, error) {
apiAstList = append(apiAstList, nestedApi)
}
err = p.checkTypeDeclaration(apiAstList)
if err != nil {
return nil, err
if !p.skipCheckTypeDeclaration {
err = p.checkTypeDeclaration(apiAstList)
if err != nil {
return nil, err
}
}
allApi := p.memberFill(apiAstList)
@@ -483,3 +486,9 @@ func WithParserPrefix(prefix string) ParserOption {
p.linePrefix = prefix
}
}
func WithParserSkipCheckTypeDeclaration() ParserOption {
return func(p *Parser) {
p.skipCheckTypeDeclaration = true
}
}

View File

@@ -33,9 +33,13 @@ func Parse(filename string) (*spec.ApiSpec, error) {
return spec, nil
}
// ParseContent parses the api content
func ParseContent(content string, filename ...string) (*spec.ApiSpec, error) {
astParser := ast.NewParser()
func parseContent(content string, skipCheckTypeDeclaration bool, filename ...string) (*spec.ApiSpec, error) {
var astParser *ast.Parser
if skipCheckTypeDeclaration {
astParser = ast.NewParser(ast.WithParserSkipCheckTypeDeclaration())
} else {
astParser = ast.NewParser()
}
ast, err := astParser.ParseContent(content, filename...)
if err != nil {
return nil, err
@@ -51,6 +55,16 @@ func ParseContent(content string, filename ...string) (*spec.ApiSpec, error) {
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 {
p.fillInfo()
p.fillSyntax()