internal/wire: copy over anonymous imports (#101)
Adds Kumbirai Tanekha to A+C. Fixes #94
This commit is contained in:
committed by
Ross Light
parent
4243b011bd
commit
f285c073b5
1
AUTHORS
1
AUTHORS
@@ -12,6 +12,7 @@
|
||||
|
||||
Google LLC
|
||||
ktr <ktr@syfm.me>
|
||||
Kumbirai Tanekha <kumbirai.tanekha@gmail.com>
|
||||
Oleg Kovalov <iamolegkovalov@gmail.com>
|
||||
Yoichiro Shimizu <budougumi0617@gmail.com>
|
||||
Zachary Romero <zacromero3@gmail.com>
|
||||
|
||||
@@ -34,6 +34,7 @@ Christina Austin <4240737+clausti@users.noreply.github.com>
|
||||
Eno Compton <enocom@google.com>
|
||||
Issac Trotts <issactrotts@google.com> <issac.trotts@gmail.com>
|
||||
ktr <ktr@syfm.me>
|
||||
Kumbirai Tanekha <kumbirai.tanekha@gmail.com>
|
||||
Oleg Kovalov <iamolegkovalov@gmail.com>
|
||||
Robert van Gent <rvangent@google.com> <vangent@gmail.com>
|
||||
Ross Light <light@google.com> <ross@zombiezen.com>
|
||||
|
||||
15
internal/wire/testdata/PkgImport/anon1/anon1.go
vendored
Normal file
15
internal/wire/testdata/PkgImport/anon1/anon1.go
vendored
Normal 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
|
||||
15
internal/wire/testdata/PkgImport/anon2/anon2.go
vendored
Normal file
15
internal/wire/testdata/PkgImport/anon2/anon2.go
vendored
Normal 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
|
||||
2
internal/wire/testdata/PkgImport/foo/wire.go
vendored
2
internal/wire/testdata/PkgImport/foo/wire.go
vendored
@@ -17,6 +17,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "example.com/anon1"
|
||||
_ "example.com/anon2"
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
||||
|
||||
@@ -9,6 +9,11 @@ import (
|
||||
"example.com/bar"
|
||||
)
|
||||
|
||||
import (
|
||||
_ "example.com/anon1"
|
||||
_ "example.com/anon2"
|
||||
)
|
||||
|
||||
// Injectors from wire.go:
|
||||
|
||||
func injectFooBar() FooBar {
|
||||
|
||||
@@ -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
|
||||
@@ -224,12 +230,14 @@ type gen struct {
|
||||
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,
|
||||
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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user