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 anonymous import handling #359

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 1 addition & 4 deletions internal/wire/testdata/PkgImport/want/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 11 additions & 23 deletions internal/wire/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ func generateInjectors(g *gen, pkg *packages.Package) (injectorFiles []*ast.File

for _, impt := range f.Imports {
if impt.Name != nil && impt.Name.Name == "_" {
g.anonImports[impt.Path.Value] = true
g.imports[strings.Trim(impt.Path.Value, `"`)] = importInfo{
name: "_",
differs: true,
}
}
}
}
Expand Down Expand Up @@ -241,19 +244,17 @@ type importInfo struct {

// gen is the file-wide generator state.
type gen struct {
pkg *packages.Package
buf bytes.Buffer
imports map[string]importInfo
anonImports map[string]bool
values map[ast.Expr]string
pkg *packages.Package
buf bytes.Buffer
imports map[string]importInfo
values map[ast.Expr]string
}

func newGen(pkg *packages.Package) *gen {
return &gen{
pkg: pkg,
anonImports: make(map[string]bool),
imports: make(map[string]importInfo),
values: make(map[ast.Expr]string),
pkg: pkg,
imports: make(map[string]importInfo),
values: make(map[ast.Expr]string),
}
}

Expand Down Expand Up @@ -290,19 +291,6 @@ func (g *gen) frame(tags string) []byte {
}
buf.WriteString(")\n\n")
}
if len(g.anonImports) > 0 {
buf.WriteString("import (\n")
anonImps := make([]string, 0, len(g.anonImports))
for path := range g.anonImports {
anonImps = append(anonImps, path)
}
sort.Strings(anonImps)

for _, path := range anonImps {
fmt.Fprintf(&buf, "\t_ %s\n", path)
}
buf.WriteString(")\n\n")
}
buf.Write(g.buf.Bytes())
return buf.Bytes()
}
Expand Down