-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor some things for the needs of github.com/go-chai/chai #1094
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -810,6 +810,10 @@ func (operation *Operation) parseCombinedObjectSchema(refType string, astFile *a | |
}), nil | ||
} | ||
|
||
func (operation *Operation) ParseAPIObjectSchema(schemaType, refType string, astFile *ast.File) (*spec.Schema, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to use this function for creating a swagger schema for a type name |
||
return operation.parseObjectSchema(refType, astFile) | ||
} | ||
|
||
func (operation *Operation) parseAPIObjectSchema(schemaType, refType string, astFile *ast.File) (*spec.Schema, error) { | ||
switch schemaType { | ||
case OBJECT: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1281,21 +1281,55 @@ func defineTypeOfExample(schemaType, arrayType, exampleValue string) (interface{ | |
return nil, fmt.Errorf("%s is unsupported type in example value %s", schemaType, exampleValue) | ||
} | ||
|
||
// GetAllGoFileInfo gets all Go source files information for given searchDir. | ||
func (parser *Parser) getAllGoFileInfo(packageDir, searchDir string) error { | ||
return filepath.Walk(searchDir, func(path string, f os.FileInfo, _ error) error { | ||
if err := parser.Skip(path, f); err != nil { | ||
// GetAllGoFileInfoAndParseTypes gets all Go source files information for given searchDir and parses the types from them. | ||
func (parser *Parser) GetAllGoFileInfoAndParseTypes(searchDir string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need this in order to be able to go through packages one by one and incrementally parse the types from them. I saw that ParseAPIMultiSearchDir() first collects all types from the astFiles and then parses them all at once via parser.packages.ParseTypes(), but in my case that would mean having to do a double pass through all routes, which I'd rather not have to do and instead have a function that parses the types of a single package. |
||
return filepath.Walk(searchDir, func(path string, f os.FileInfo, e error) error { | ||
astFile, err := parser.getGoFileInfo(searchDir, searchDir, path, f, e) | ||
if err != nil { | ||
return err | ||
} else if f.IsDir() { | ||
} | ||
|
||
if astFile == nil { | ||
return nil | ||
} | ||
|
||
relPath, err := filepath.Rel(searchDir, path) | ||
parser.packages.parseTypesFromFile(astFile, searchDir, make(map[*TypeSpecDef]*Schema)) | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
func (parser *Parser) getGoFileInfo(packageDir, searchDir string, path string, f os.FileInfo, _ error) (*ast.File, error) { | ||
if err := parser.Skip(path, f); err != nil { | ||
return nil, err | ||
} else if f.IsDir() { | ||
return nil, nil | ||
} | ||
|
||
relPath, err := filepath.Rel(searchDir, path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
astFile, err := parser.parseFile(filepath.ToSlash(filepath.Dir(filepath.Clean(filepath.Join(packageDir, relPath)))), path, nil) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return astFile, nil | ||
} | ||
|
||
// GetAllGoFileInfo gets all Go source files information for given searchDir. | ||
func (parser *Parser) getAllGoFileInfo(packageDir, searchDir string) error { | ||
return filepath.Walk(searchDir, func(path string, f os.FileInfo, e error) error { | ||
_, err := parser.getGoFileInfo(packageDir, searchDir, path, f, e) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return parser.parseFile(filepath.ToSlash(filepath.Dir(filepath.Clean(filepath.Join(packageDir, relPath)))), path, nil) | ||
return nil | ||
}) | ||
} | ||
|
||
|
@@ -1321,7 +1355,7 @@ func (parser *Parser) getAllGoFileInfoFromDeps(pkg *depth.Pkg) error { | |
} | ||
|
||
path := filepath.Join(srcDir, f.Name()) | ||
if err := parser.parseFile(pkg.Name, path, nil); err != nil { | ||
if _, err := parser.parseFile(pkg.Name, path, nil); err != nil { | ||
return err | ||
} | ||
} | ||
|
@@ -1335,23 +1369,23 @@ func (parser *Parser) getAllGoFileInfoFromDeps(pkg *depth.Pkg) error { | |
return nil | ||
} | ||
|
||
func (parser *Parser) parseFile(packageDir, path string, src interface{}) error { | ||
func (parser *Parser) parseFile(packageDir, path string, src interface{}) (*ast.File, error) { | ||
if strings.HasSuffix(strings.ToLower(path), "_test.go") || filepath.Ext(path) != ".go" { | ||
return nil | ||
return nil, nil | ||
} | ||
|
||
// positions are relative to FileSet | ||
astFile, err := goparser.ParseFile(token.NewFileSet(), path, src, goparser.ParseComments) | ||
if err != nil { | ||
return fmt.Errorf("ParseFile error:%+v", err) | ||
return nil, fmt.Errorf("ParseFile error:%+v", err) | ||
} | ||
|
||
err = parser.packages.CollectAstFile(packageDir, path, astFile) | ||
if err != nil { | ||
return err | ||
return nil, err | ||
} | ||
|
||
return nil | ||
return astFile, nil | ||
} | ||
|
||
func (parser *Parser) checkOperationIDUniqueness() error { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved out of the Build function the parts that deal with generating the files that store the generated swagger spec, because in my case I don't generate the specs from the swag binary, but I want to create the same files that swag does.