From f285c073b52078f165ca19f3ceb192f0f50f3934 Mon Sep 17 00:00:00 2001 From: Kumbirai Tanekha Date: Mon, 7 Jan 2019 17:04:35 +0000 Subject: [PATCH] internal/wire: copy over anonymous imports (#101) Adds Kumbirai Tanekha to A+C. Fixes #94 --- AUTHORS | 1 + CONTRIBUTORS | 1 + .../wire/testdata/PkgImport/anon1/anon1.go | 15 ++++++++ .../wire/testdata/PkgImport/anon2/anon2.go | 15 ++++++++ internal/wire/testdata/PkgImport/foo/wire.go | 2 ++ .../wire/testdata/PkgImport/want/wire_gen.go | 5 +++ internal/wire/wire.go | 35 +++++++++++++++---- 7 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 internal/wire/testdata/PkgImport/anon1/anon1.go create mode 100644 internal/wire/testdata/PkgImport/anon2/anon2.go diff --git a/AUTHORS b/AUTHORS index 1cc9bb4..4d8d4b3 100644 --- a/AUTHORS +++ b/AUTHORS @@ -12,6 +12,7 @@ Google LLC ktr +Kumbirai Tanekha Oleg Kovalov Yoichiro Shimizu Zachary Romero diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 2b09493..00a94f8 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -34,6 +34,7 @@ Christina Austin <4240737+clausti@users.noreply.github.com> Eno Compton Issac Trotts ktr +Kumbirai Tanekha Oleg Kovalov Robert van Gent Ross Light diff --git a/internal/wire/testdata/PkgImport/anon1/anon1.go b/internal/wire/testdata/PkgImport/anon1/anon1.go new file mode 100644 index 0000000..5ee862f --- /dev/null +++ b/internal/wire/testdata/PkgImport/anon1/anon1.go @@ -0,0 +1,15 @@ +// Copyright 2018 The Wire Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package anon1 diff --git a/internal/wire/testdata/PkgImport/anon2/anon2.go b/internal/wire/testdata/PkgImport/anon2/anon2.go new file mode 100644 index 0000000..79fd5e8 --- /dev/null +++ b/internal/wire/testdata/PkgImport/anon2/anon2.go @@ -0,0 +1,15 @@ +// Copyright 2018 The Wire Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package anon2 diff --git a/internal/wire/testdata/PkgImport/foo/wire.go b/internal/wire/testdata/PkgImport/foo/wire.go index 197e98a..576bf94 100644 --- a/internal/wire/testdata/PkgImport/foo/wire.go +++ b/internal/wire/testdata/PkgImport/foo/wire.go @@ -17,6 +17,8 @@ package main import ( + _ "example.com/anon1" + _ "example.com/anon2" "github.com/google/wire" ) diff --git a/internal/wire/testdata/PkgImport/want/wire_gen.go b/internal/wire/testdata/PkgImport/want/wire_gen.go index bebce37..e7b662a 100644 --- a/internal/wire/testdata/PkgImport/want/wire_gen.go +++ b/internal/wire/testdata/PkgImport/want/wire_gen.go @@ -9,6 +9,11 @@ import ( "example.com/bar" ) +import ( + _ "example.com/anon1" + _ "example.com/anon2" +) + // Injectors from wire.go: func injectFooBar() FooBar { diff --git a/internal/wire/wire.go b/internal/wire/wire.go index 656b067..089dc7f 100644 --- a/internal/wire/wire.go +++ b/internal/wire/wire.go @@ -171,6 +171,12 @@ func generateInjectors(g *gen, pkg *packages.Package) (injectorFiles []*ast.File continue } } + + for _, impt := range f.Imports { + if impt.Name != nil && impt.Name.Name == "_" { + g.anonImports[impt.Path.Value] = true + } + } } if len(ec.errors) > 0 { return nil, ec.errors @@ -221,17 +227,19 @@ type importInfo struct { // gen is the file-wide generator state. type gen struct { - pkg *packages.Package - buf bytes.Buffer - imports map[string]importInfo - values map[ast.Expr]string + pkg *packages.Package + buf bytes.Buffer + imports map[string]importInfo + anonImports map[string]bool + values map[ast.Expr]string } func newGen(pkg *packages.Package) *gen { return &gen{ - pkg: pkg, - imports: make(map[string]importInfo), - values: make(map[ast.Expr]string), + pkg: pkg, + anonImports: make(map[string]bool), + imports: make(map[string]importInfo), + values: make(map[ast.Expr]string), } } @@ -265,6 +273,19 @@ func (g *gen) frame() []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() }