Skip to content
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

Simplify with "slices" and "maps" instead of "sort" #896

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"go/ast"
"go/types"
"io"
"maps"
"os"
"path/filepath"
"regexp"
"sort"
"slices"
"strings"
"text/template"
"unicode"
Expand Down Expand Up @@ -391,26 +392,18 @@ func (g *Generator) expecterName() string {
return g.mockName() + "_Expecter"
}

func (g *Generator) sortedImportNames() (importNames []string) {
for name := range g.nameToPackagePath {
importNames = append(importNames, name)
}
sort.Strings(importNames)
return
}

func (g *Generator) generateImports(ctx context.Context) {
log := zerolog.Ctx(ctx)

log.Debug().Msgf("generating imports")

pkgPath := g.nameToPackagePath[g.iface.Pkg.Name()]
// Sort by import name so that we get a deterministic order
for _, name := range g.sortedImportNames() {
logImport := log.With().Str(logging.LogKeyImport, g.nameToPackagePath[name]).Logger()
for _, name := range slices.Sorted(maps.Keys(g.nameToPackagePath)) {
path := g.nameToPackagePath[name]
logImport := log.With().Str(logging.LogKeyImport, path).Logger()
logImport.Debug().Msgf("found import")

path := g.nameToPackagePath[name]
if !g.config.KeepTree && g.config.InPackage && path == pkgPath {
logImport.Debug().Msgf("import (%s) equals interface's package path (%s), skipping", path, pkgPath)
continue
Expand Down
22 changes: 5 additions & 17 deletions pkg/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"go/types"
"os"
"path/filepath"
"sort"
"slices"
"strings"

"github.com/rs/zerolog"
Expand Down Expand Up @@ -224,13 +224,15 @@ func (p *Parser) Find(name string) (*Interface, error) {
}

func (p *Parser) Interfaces() []*Interface {
ifaces := make(sortableIFaceList, 0)
var ifaces []*Interface
for _, entry := range p.files {
declaredIfaces := entry.interfaces
ifaces = p.packageInterfaces(entry.pkg.Types, entry.fileName, declaredIfaces, ifaces)
}

sort.Sort(ifaces)
slices.SortFunc(ifaces, func(a, b *Interface) int {
return strings.Compare(a.Name, b.Name)
})
return ifaces
}

Expand Down Expand Up @@ -335,20 +337,6 @@ func (iface *Interface) Methods() []*Method {
return methods
}

type sortableIFaceList []*Interface

func (s sortableIFaceList) Len() int {
return len(s)
}

func (s sortableIFaceList) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

func (s sortableIFaceList) Less(i, j int) bool {
return strings.Compare(s[i].Name, s[j].Name) == -1
}

type NodeVisitor struct {
declaredInterfaces []string
disableFuncMocks bool
Expand Down
Loading