Skip to content

Commit

Permalink
Improve test coverage and code readability. (#967)
Browse files Browse the repository at this point in the history
* improve code coverage and linting
  • Loading branch information
ubogdan authored Aug 4, 2021
1 parent f2f643a commit cb2bdb6
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 158 deletions.
153 changes: 107 additions & 46 deletions operation.go

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,21 @@ func TestParseMultiDescription(t *testing.T) {
assert.Contains(t, string(b), expected)
}

func TestParseDescriptionMarkdown(t *testing.T) {
operation := NewOperation(nil)
operation.parser.markdownFileDir = "example/markdown"

comment := `@description.markdown admin.md`

err := operation.ParseComment(comment, nil)
assert.NoError(t, err)

comment = `@description.markdown missing.md`

err = operation.ParseComment(comment, nil)
assert.Error(t, err)
}

func TestParseSummary(t *testing.T) {
comment := `@summary line one`
operation := NewOperation(nil)
Expand Down
39 changes: 24 additions & 15 deletions packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"strings"
)

//PackagesDefinitions map[package import path]*PackageDefinitions
// PackagesDefinitions map[package import path]*PackageDefinitions.
type PackagesDefinitions struct {
files map[*ast.File]*AstFileInfo
packages map[string]*PackageDefinitions
uniqueDefinitions map[string]*TypeSpecDef
}

//NewPackagesDefinitions create object PackagesDefinitions
// NewPackagesDefinitions create object PackagesDefinitions.
func NewPackagesDefinitions() *PackagesDefinitions {
return &PackagesDefinitions{
files: make(map[*ast.File]*AstFileInfo),
Expand All @@ -22,7 +22,7 @@ func NewPackagesDefinitions() *PackagesDefinitions {
}
}

//CollectAstFile collect ast.file
// CollectAstFile collect ast.file.
func (pkgs *PackagesDefinitions) CollectAstFile(packageDir, path string, astFile *ast.File) {
if pkgs.files == nil {
pkgs.files = make(map[*ast.File]*AstFileInfo)
Expand Down Expand Up @@ -53,18 +53,19 @@ func (pkgs *PackagesDefinitions) CollectAstFile(packageDir, path string, astFile
}
}

//RangeFiles for range the collection of ast.File
// RangeFiles for range the collection of ast.File.
func (pkgs *PackagesDefinitions) RangeFiles(handle func(filename string, file *ast.File) error) error {
for file, info := range pkgs.files {
if err := handle(info.Path, file); err != nil {
return err
}
}

return nil
}

//ParseTypes parse types
//@Return parsed definitions
// ParseTypes parse types
// @Return parsed definitions.
func (pkgs *PackagesDefinitions) ParseTypes() (map[*TypeSpecDef]*Schema, error) {
parsedSchemas := make(map[*TypeSpecDef]*Schema)
for astFile, info := range pkgs.files {
Expand Down Expand Up @@ -108,6 +109,7 @@ func (pkgs *PackagesDefinitions) ParseTypes() (map[*TypeSpecDef]*Schema, error)
}
}
}

return parsedSchemas, nil
}

Expand All @@ -119,13 +121,14 @@ func (pkgs *PackagesDefinitions) findTypeSpec(pkgPath string, typeName string) *
}
}
}

return nil
}

// findPackagePathFromImports finds out the package path of a package via ranging imports of a ast.File
// @pkg the name of the target package
// @file current ast.File in which to search imports
// @return the package path of a package of @pkg
// @return the package path of a package of @pkg.
func (pkgs *PackagesDefinitions) findPackagePathFromImports(pkg string, file *ast.File) string {
if file == nil {
return ""
Expand All @@ -142,10 +145,14 @@ func (pkgs *PackagesDefinitions) findPackagePathFromImports(pkg string, file *as
if imp.Name != nil {
if imp.Name.Name == pkg {
return strings.Trim(imp.Path.Value, `"`)
} else if imp.Name.Name == "_" {
}
if imp.Name.Name == "_" {
hasAnonymousPkg = true
}
} else if pkgs.packages != nil {

continue
}
if pkgs.packages != nil {
path := strings.Trim(imp.Path.Value, `"`)
if pd, ok := pkgs.packages[path]; ok {
if pd.Name == pkg {
Expand All @@ -155,7 +162,7 @@ func (pkgs *PackagesDefinitions) findPackagePathFromImports(pkg string, file *as
}
}

//match unnamed package
// match unnamed package
if hasAnonymousPkg && pkgs.packages != nil {
for _, imp := range file.Imports {
if imp.Name == nil {
Expand All @@ -171,23 +178,24 @@ func (pkgs *PackagesDefinitions) findPackagePathFromImports(pkg string, file *as
}
}
}

return ""
}

// FindTypeSpec finds out TypeSpecDef of a type by typeName
// @typeName the name of the target type, if it starts with a package name, find its own package path from imports on top of @file
// @file the ast.file in which @typeName is used
// @pkgPath the package path of @file
// @pkgPath the package path of @file.
func (pkgs *PackagesDefinitions) FindTypeSpec(typeName string, file *ast.File) *TypeSpecDef {
if IsGolangPrimitiveType(typeName) {
return nil
} else if file == nil { // for test
}
if file == nil { // for test
return pkgs.uniqueDefinitions[typeName]
}

if strings.ContainsRune(typeName, '.') {
parts := strings.Split(typeName, ".")

parts := strings.Split(typeName, ".")
if len(parts) > 1 { //nolint:nestif
isAliasPkgName := func(file *ast.File, pkgName string) bool {
if file != nil && file.Imports != nil {
for _, pkg := range file.Imports {
Expand All @@ -210,6 +218,7 @@ func (pkgs *PackagesDefinitions) FindTypeSpec(typeName string, file *ast.File) *
if len(pkgPath) == 0 && parts[0] == file.Name.Name {
pkgPath = pkgs.files[file].PackagePath
}

return pkgs.findTypeSpec(pkgPath, parts[1])
}

Expand Down
Loading

0 comments on commit cb2bdb6

Please sign in to comment.