Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Commit b730ad0

Browse files
authored
internal/wire: use set to determine which argument to use in zero-call injectors (#223)
Fixes #222
1 parent a5347c8 commit b730ad0

File tree

6 files changed

+75
-9
lines changed

6 files changed

+75
-9
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2019 The Wire Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import "fmt"
18+
19+
func main() {
20+
fmt.Println(injectStringer("Hello, World!"))
21+
}
22+
23+
type MyString string
24+
25+
func (s MyString) String() string { return string(s) }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2019 The Wire Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//+build wireinject
16+
17+
package main
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/google/wire"
23+
)
24+
25+
func injectStringer(s MyString) fmt.Stringer {
26+
wire.Build(wire.Bind(new(fmt.Stringer), new(MyString)))
27+
return nil
28+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example.com/foo
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, World!

internal/wire/testdata/ReturnArgumentAsInterface/want/wire_gen.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/wire/wire.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,12 @@ func (g *gen) inject(pos token.Pos, name string, sig *types.Signature, set *Prov
367367
}
368368

369369
// Perform one pass to collect all imports, followed by the real pass.
370-
injectPass(name, sig, calls, &injectorGen{
370+
injectPass(name, sig, calls, set, &injectorGen{
371371
g: g,
372372
errVar: disambiguate("err", g.nameInFileScope),
373373
discard: true,
374374
})
375-
injectPass(name, sig, calls, &injectorGen{
375+
injectPass(name, sig, calls, set, &injectorGen{
376376
g: g,
377377
errVar: disambiguate("err", g.nameInFileScope),
378378
discard: false,
@@ -586,7 +586,7 @@ type injectorGen struct {
586586

587587
// injectPass generates an injector given the output from analysis.
588588
// The sig passed in should be verified.
589-
func injectPass(name string, sig *types.Signature, calls []call, ig *injectorGen) {
589+
func injectPass(name string, sig *types.Signature, calls []call, set *ProviderSet, ig *injectorGen) {
590590
params := sig.Params()
591591
injectSig, err := funcOutput(sig)
592592
if err != nil {
@@ -643,12 +643,7 @@ func injectPass(name string, sig *types.Signature, calls []call, ig *injectorGen
643643
}
644644
}
645645
if len(calls) == 0 {
646-
for i := 0; i < params.Len(); i++ {
647-
if types.Identical(injectSig.out, params.At(i).Type()) {
648-
ig.p("\treturn %s", ig.paramNames[i])
649-
break
650-
}
651-
}
646+
ig.p("\treturn %s", ig.paramNames[set.For(injectSig.out).Arg().Index])
652647
} else {
653648
ig.p("\treturn %s", ig.localNames[len(calls)-1])
654649
}

0 commit comments

Comments
 (0)