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
|
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>
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
_ "example.com/anon1"
|
||||||
|
_ "example.com/anon2"
|
||||||
"github.com/google/wire"
|
"github.com/google/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -221,17 +227,19 @@ type importInfo struct {
|
|||||||
|
|
||||||
// gen is the file-wide generator state.
|
// gen is the file-wide generator state.
|
||||||
type gen struct {
|
type gen struct {
|
||||||
pkg *packages.Package
|
pkg *packages.Package
|
||||||
buf bytes.Buffer
|
buf bytes.Buffer
|
||||||
imports map[string]importInfo
|
imports map[string]importInfo
|
||||||
values map[ast.Expr]string
|
anonImports map[string]bool
|
||||||
|
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,
|
||||||
imports: make(map[string]importInfo),
|
anonImports: make(map[string]bool),
|
||||||
values: make(map[ast.Expr]string),
|
imports: make(map[string]importInfo),
|
||||||
|
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()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user