internal/wire: use set to determine which argument to use in zero-call injectors (#223)

Fixes #222
This commit is contained in:
Ross Light
2019-11-27 15:23:39 -08:00
committed by GitHub
parent a5347c86bc
commit b730ad0fbf
6 changed files with 75 additions and 9 deletions

View File

@@ -0,0 +1,25 @@
// Copyright 2019 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 main
import "fmt"
func main() {
fmt.Println(injectStringer("Hello, World!"))
}
type MyString string
func (s MyString) String() string { return string(s) }

View File

@@ -0,0 +1,28 @@
// Copyright 2019 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.
//+build wireinject
package main
import (
"fmt"
"github.com/google/wire"
)
func injectStringer(s MyString) fmt.Stringer {
wire.Build(wire.Bind(new(fmt.Stringer), new(MyString)))
return nil
}

View File

@@ -0,0 +1 @@
example.com/foo

View File

@@ -0,0 +1 @@
Hello, World!

View File

@@ -0,0 +1,16 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate wire
//+build !wireinject
package main
import (
"fmt"
)
// Injectors from wire.go:
func injectStringer(s MyString) fmt.Stringer {
return s
}

View File

@@ -367,12 +367,12 @@ func (g *gen) inject(pos token.Pos, name string, sig *types.Signature, set *Prov
}
// Perform one pass to collect all imports, followed by the real pass.
injectPass(name, sig, calls, &injectorGen{
injectPass(name, sig, calls, set, &injectorGen{
g: g,
errVar: disambiguate("err", g.nameInFileScope),
discard: true,
})
injectPass(name, sig, calls, &injectorGen{
injectPass(name, sig, calls, set, &injectorGen{
g: g,
errVar: disambiguate("err", g.nameInFileScope),
discard: false,
@@ -586,7 +586,7 @@ type injectorGen struct {
// injectPass generates an injector given the output from analysis.
// The sig passed in should be verified.
func injectPass(name string, sig *types.Signature, calls []call, ig *injectorGen) {
func injectPass(name string, sig *types.Signature, calls []call, set *ProviderSet, ig *injectorGen) {
params := sig.Params()
injectSig, err := funcOutput(sig)
if err != nil {
@@ -643,12 +643,7 @@ func injectPass(name string, sig *types.Signature, calls []call, ig *injectorGen
}
}
if len(calls) == 0 {
for i := 0; i < params.Len(); i++ {
if types.Identical(injectSig.out, params.At(i).Type()) {
ig.p("\treturn %s", ig.paramNames[i])
break
}
}
ig.p("\treturn %s", ig.paramNames[set.For(injectSig.out).Arg().Index])
} else {
ig.p("\treturn %s", ig.localNames[len(calls)-1])
}