internal/wire: use set to determine which argument to use in zero-call injectors (#223)
Fixes #222
This commit is contained in:
25
internal/wire/testdata/ReturnArgumentAsInterface/foo/foo.go
vendored
Normal file
25
internal/wire/testdata/ReturnArgumentAsInterface/foo/foo.go
vendored
Normal 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) }
|
||||||
28
internal/wire/testdata/ReturnArgumentAsInterface/foo/wire.go
vendored
Normal file
28
internal/wire/testdata/ReturnArgumentAsInterface/foo/wire.go
vendored
Normal 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
|
||||||
|
}
|
||||||
1
internal/wire/testdata/ReturnArgumentAsInterface/pkg
vendored
Normal file
1
internal/wire/testdata/ReturnArgumentAsInterface/pkg
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
example.com/foo
|
||||||
1
internal/wire/testdata/ReturnArgumentAsInterface/want/program_out.txt
vendored
Normal file
1
internal/wire/testdata/ReturnArgumentAsInterface/want/program_out.txt
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Hello, World!
|
||||||
16
internal/wire/testdata/ReturnArgumentAsInterface/want/wire_gen.go
vendored
Normal file
16
internal/wire/testdata/ReturnArgumentAsInterface/want/wire_gen.go
vendored
Normal 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
|
||||||
|
}
|
||||||
@@ -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.
|
// 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,
|
g: g,
|
||||||
errVar: disambiguate("err", g.nameInFileScope),
|
errVar: disambiguate("err", g.nameInFileScope),
|
||||||
discard: true,
|
discard: true,
|
||||||
})
|
})
|
||||||
injectPass(name, sig, calls, &injectorGen{
|
injectPass(name, sig, calls, set, &injectorGen{
|
||||||
g: g,
|
g: g,
|
||||||
errVar: disambiguate("err", g.nameInFileScope),
|
errVar: disambiguate("err", g.nameInFileScope),
|
||||||
discard: false,
|
discard: false,
|
||||||
@@ -586,7 +586,7 @@ type injectorGen struct {
|
|||||||
|
|
||||||
// injectPass generates an injector given the output from analysis.
|
// injectPass generates an injector given the output from analysis.
|
||||||
// The sig passed in should be verified.
|
// 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()
|
params := sig.Params()
|
||||||
injectSig, err := funcOutput(sig)
|
injectSig, err := funcOutput(sig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -643,12 +643,7 @@ func injectPass(name string, sig *types.Signature, calls []call, ig *injectorGen
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(calls) == 0 {
|
if len(calls) == 0 {
|
||||||
for i := 0; i < params.Len(); i++ {
|
ig.p("\treturn %s", ig.paramNames[set.For(injectSig.out).Arg().Index])
|
||||||
if types.Identical(injectSig.out, params.At(i).Type()) {
|
|
||||||
ig.p("\treturn %s", ig.paramNames[i])
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
ig.p("\treturn %s", ig.localNames[len(calls)-1])
|
ig.p("\treturn %s", ig.localNames[len(calls)-1])
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user