internal/wire: copy over anonymous imports (#101)

Adds Kumbirai Tanekha to A+C.

Fixes #94
This commit is contained in:
Kumbirai Tanekha
2019-01-07 17:04:35 +00:00
committed by Ross Light
parent 4243b011bd
commit f285c073b5
7 changed files with 67 additions and 7 deletions

View File

@@ -12,6 +12,7 @@
Google LLC Google LLC
ktr <ktr@syfm.me> ktr <ktr@syfm.me>
Kumbirai Tanekha <kumbirai.tanekha@gmail.com>
Oleg Kovalov <iamolegkovalov@gmail.com> Oleg Kovalov <iamolegkovalov@gmail.com>
Yoichiro Shimizu <budougumi0617@gmail.com> Yoichiro Shimizu <budougumi0617@gmail.com>
Zachary Romero <zacromero3@gmail.com> Zachary Romero <zacromero3@gmail.com>

View File

@@ -34,6 +34,7 @@ Christina Austin <4240737+clausti@users.noreply.github.com>
Eno Compton <enocom@google.com> Eno Compton <enocom@google.com>
Issac Trotts <issactrotts@google.com> <issac.trotts@gmail.com> Issac Trotts <issactrotts@google.com> <issac.trotts@gmail.com>
ktr <ktr@syfm.me> ktr <ktr@syfm.me>
Kumbirai Tanekha <kumbirai.tanekha@gmail.com>
Oleg Kovalov <iamolegkovalov@gmail.com> Oleg Kovalov <iamolegkovalov@gmail.com>
Robert van Gent <rvangent@google.com> <vangent@gmail.com> Robert van Gent <rvangent@google.com> <vangent@gmail.com>
Ross Light <light@google.com> <ross@zombiezen.com> Ross Light <light@google.com> <ross@zombiezen.com>

View File

@@ -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

View File

@@ -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

View File

@@ -17,6 +17,8 @@
package main package main
import ( import (
_ "example.com/anon1"
_ "example.com/anon2"
"github.com/google/wire" "github.com/google/wire"
) )

View File

@@ -9,6 +9,11 @@ import (
"example.com/bar" "example.com/bar"
) )
import (
_ "example.com/anon1"
_ "example.com/anon2"
)
// Injectors from wire.go: // Injectors from wire.go:
func injectFooBar() FooBar { func injectFooBar() FooBar {

View File

@@ -171,6 +171,12 @@ func generateInjectors(g *gen, pkg *packages.Package) (injectorFiles []*ast.File
continue continue
} }
} }
for _, impt := range f.Imports {
if impt.Name != nil && impt.Name.Name == "_" {
g.anonImports[impt.Path.Value] = true
}
}
} }
if len(ec.errors) > 0 { if len(ec.errors) > 0 {
return nil, ec.errors return nil, ec.errors
@@ -224,12 +230,14 @@ type gen struct {
pkg *packages.Package pkg *packages.Package
buf bytes.Buffer buf bytes.Buffer
imports map[string]importInfo imports map[string]importInfo
anonImports map[string]bool
values map[ast.Expr]string values map[ast.Expr]string
} }
func newGen(pkg *packages.Package) *gen { func newGen(pkg *packages.Package) *gen {
return &gen{ return &gen{
pkg: pkg, pkg: pkg,
anonImports: make(map[string]bool),
imports: make(map[string]importInfo), imports: make(map[string]importInfo),
values: make(map[ast.Expr]string), values: make(map[ast.Expr]string),
} }
@@ -265,6 +273,19 @@ func (g *gen) frame() []byte {
} }
buf.WriteString(")\n\n") 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()) buf.Write(g.buf.Bytes())
return buf.Bytes() return buf.Bytes()
} }