Skip to content

Commit

Permalink
Remove unused imports code, fix some naming
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhail-eremin authored and dizzyfool committed Sep 7, 2020
1 parent 1db4006 commit a1145a1
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 173 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ All comments are optional.
Struct comments
type MyService struct {} //zenrpc


## Need to browse your api and do some test api calls?
We recommend to use [SMDBox](https://github.com/semrush/smdbox). It is Swagger-like JSON RPC API browser, compatible with smd scheme, generated by zenrpc.

# JSON-RPC 2.0 Supported Features

Expand Down
8 changes: 4 additions & 4 deletions parser/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ func GetDependenciesAstFiles(filename string) ([]*ast.File, error) {
astFiles := []*ast.File{}
done := map[string]bool{}
for _, pkg := range pkgs {
if _, ok := done[pkg.Name]; ok {
if _, ok := done[pkg.PkgPath]; ok {
continue
}
astFiles = append(astFiles, pkg.Syntax...)
done[pkg.Name] = true
done[pkg.PkgPath] = true
for _, childPack := range pkg.Imports {
if _, ok := done[childPack.Name]; ok {
if _, ok := done[childPack.PkgPath]; ok {
continue
}
astFiles = append(astFiles, childPack.Syntax...)
done[childPack.Name] = true
done[childPack.PkgPath] = true
}
}
return astFiles, nil
Expand Down
55 changes: 2 additions & 53 deletions parser/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,10 @@ package parser

import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"path/filepath"
"strings"
)

func (pi *PackageInfo) parseImports(imports []*ast.ImportSpec) error {
for _, i := range imports {
name, path := importNamePath(i)
realPath := tryFindPath(path, pi.Dir)
if realPath == "" {
// can't find path to package
continue
}

// read import dir
files, err := ioutil.ReadDir(realPath)
if err != nil {
return err
}

// for each file
pkgImports := []*ast.ImportSpec{}
for _, f := range files {
if f.IsDir() {
continue
}

if !strings.HasSuffix(f.Name(), goFileSuffix) ||
strings.HasSuffix(f.Name(), GenerateFileSuffix) || strings.HasSuffix(f.Name(), testFileSuffix) {
continue
}

filename := filepath.Join(realPath, f.Name())
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
return err
}

pi.Scopes[name] = append(pi.Scopes[name], f.Scope)
pkgImports = append(pkgImports, f.Imports...)
}

// collect unique imports from package files and call parseImports once for package
if err := pi.parseImports(uniqueImports(pkgImports)); err != nil {
return err
}
}

return nil
}

func importNamePath(i *ast.ImportSpec) (name, path string) {
func importNameOrAliasAndPath(i *ast.ImportSpec) (name, path string) {
path = i.Path.Value[1 : len(i.Path.Value)-1] // remove quotes ""
if i.Name != nil {
name = i.Name.Name
Expand Down Expand Up @@ -87,7 +36,7 @@ func uniqueImports(in []*ast.ImportSpec) (out []*ast.ImportSpec) {
// filterImports filter imports by namespace in structs
func filterImports(in []*ast.ImportSpec, names map[string]struct{}) (out []*ast.ImportSpec) {
for _, i := range in {
name, _ := importNamePath(i)
name, _ := importNameOrAliasAndPath(i)
if _, ok := names[name]; ok {
out = append(out, i)
}
Expand Down
44 changes: 0 additions & 44 deletions parser/import_unix.go

This file was deleted.

50 changes: 0 additions & 50 deletions parser/import_windows.go

This file was deleted.

29 changes: 15 additions & 14 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type PackageInfo struct {
Structs map[string]*Struct
Imports []*ast.ImportSpec

StructsNamespacesFromArgs map[string]struct{} // set of structs names from arguments for printing imports
ImportsForGeneration []*ast.ImportSpec
PackageNamesAndAliasesUsedInServices map[string]struct{} // set of structs names from arguments for printing imports
ImportsIncludedToGeneratedCode []*ast.ImportSpec
}

type Service struct {
Expand Down Expand Up @@ -138,8 +138,8 @@ func NewPackageInfo(filename string) (*PackageInfo, error) {
Structs: make(map[string]*Struct),
Imports: []*ast.ImportSpec{},

StructsNamespacesFromArgs: make(map[string]struct{}),
ImportsForGeneration: []*ast.ImportSpec{},
PackageNamesAndAliasesUsedInServices: make(map[string]struct{}),
ImportsIncludedToGeneratedCode: []*ast.ImportSpec{},
}, nil
}

Expand All @@ -159,20 +159,15 @@ func (pi *PackageInfo) Parse(filename string) error {
pi.collectImports(astFile)
}

// second loop: parse methods
// second loop: parse methods. It runs in separate loop because we need all services to be collected for this parsing
for _, f := range astFiles {
if err := pi.parseMethods(f); err != nil {
return err
}
}

// collect scopes from imported packages
pi.Imports = uniqueImports(pi.Imports)
pi.ImportsForGeneration = filterImports(pi.Imports, pi.StructsNamespacesFromArgs)
pi.Imports = filterImports(pi.Imports, uniqueStructsNamespaces(pi.Structs))
if err := pi.parseImports(pi.Imports); err != nil {
return err
}
// collect imports for generated code - only include imports that are explicitly imported in service code (all imports with definitions are more)
pi.collectImportsForGeneratedCode()

pi.parseStructs()

Expand All @@ -191,6 +186,11 @@ func (pi *PackageInfo) collectImports(astFile *ast.File) {
pi.Imports = append(pi.Imports, astFile.Imports...) // collect imports
}

func (pi *PackageInfo) collectImportsForGeneratedCode() {
// collect scopes from imported packages
pi.ImportsIncludedToGeneratedCode = filterImports(uniqueImports(pi.Imports), pi.PackageNamesAndAliasesUsedInServices)
}

func (pi *PackageInfo) collectServices(f *ast.File) {
for _, decl := range f.Decls {
gdecl, ok := decl.(*ast.GenDecl)
Expand Down Expand Up @@ -404,8 +404,8 @@ func (m *Method) parseArguments(pi *PackageInfo, fdecl *ast.FuncDecl, serviceNam

// collect namespaces (imports)
if s.Namespace != "" {
if _, ok := pi.StructsNamespacesFromArgs[s.Namespace]; !ok {
pi.StructsNamespacesFromArgs[s.Namespace] = struct{}{}
if _, ok := pi.PackageNamesAndAliasesUsedInServices[s.Namespace]; !ok {
pi.PackageNamesAndAliasesUsedInServices[s.Namespace] = struct{}{}
}
}

Expand Down Expand Up @@ -621,6 +621,7 @@ func parseType(expr ast.Expr) string {
}
}

// Returned value will be used as smd.{Value} variable from smd package
func parseSMDType(expr ast.Expr) (string, string) {
switch v := expr.(type) {
case *ast.StarExpr:
Expand Down
7 changes: 4 additions & 3 deletions testdata/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package model
import "github.com/semrush/zenrpc/v2/testdata/objects"

type Point struct {
objects.AbstractObject
X, Y int // coordinate
Z int `json:"-"`
objects.AbstractObject // embeded object
X, Y int // coordinate
Z int `json:"-"`
ConnectedObject objects.AbstractObject
}
1 change: 1 addition & 0 deletions testdata/objects/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package objects
type AbstractObject struct {
Name string
SomeField string
Measure float64
}
Loading

0 comments on commit a1145a1

Please sign in to comment.