Skip to content

Commit

Permalink
internal/wire: use set to determine which argument to use in zero-cal…
Browse files Browse the repository at this point in the history
…l injectors

Fixes google#222
  • Loading branch information
zombiezen committed Nov 24, 2019
1 parent a5347c8 commit a5bb544
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 9 deletions.
25 changes: 25 additions & 0 deletions internal/wire/testdata/ReturnArgumentAsInterface/foo/foo.go
@@ -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 changes: 28 additions & 0 deletions internal/wire/testdata/ReturnArgumentAsInterface/foo/wire.go
@@ -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 change: 1 addition & 0 deletions internal/wire/testdata/ReturnArgumentAsInterface/pkg
@@ -0,0 +1 @@
example.com/foo
@@ -0,0 +1 @@
Hello, World!
16 changes: 16 additions & 0 deletions internal/wire/testdata/ReturnArgumentAsInterface/want/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions internal/wire/wire.go
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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])
}
Expand Down

0 comments on commit a5bb544

Please sign in to comment.