Bind: takes a pointer for the second argument (#152)
This commit is contained in:
@@ -19,7 +19,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(inject(&Foo{"hello"}).Name)
|
||||
fmt.Println(inject(Foo{"hello"}).Name)
|
||||
}
|
||||
|
||||
type Fooer interface {
|
||||
@@ -30,7 +30,7 @@ type Foo struct {
|
||||
f string
|
||||
}
|
||||
|
||||
func (f *Foo) Foo() string {
|
||||
func (f Foo) Foo() string {
|
||||
return f.f
|
||||
}
|
||||
|
||||
|
||||
@@ -20,11 +20,10 @@ import (
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
||||
func inject(foo *Foo) *Bar {
|
||||
// Currently fails because wire.Bind can't see injector args (#547).
|
||||
func inject(foo Foo) *Bar {
|
||||
wire.Build(
|
||||
NewBar,
|
||||
wire.Bind(new(Fooer), &Foo{}),
|
||||
wire.Bind(new(Fooer), new(Foo)),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ package main
|
||||
|
||||
// Injectors from wire.go:
|
||||
|
||||
func inject(foo *Foo) *Bar {
|
||||
func inject(foo Foo) *Bar {
|
||||
bar := NewBar(foo)
|
||||
return bar
|
||||
}
|
||||
|
||||
43
internal/wire/testdata/BindInjectorArgPointer/foo/foo.go
vendored
Normal file
43
internal/wire/testdata/BindInjectorArgPointer/foo/foo.go
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
// 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(inject(&Foo{"hello"}).Name)
|
||||
}
|
||||
|
||||
type Fooer interface {
|
||||
Foo() string
|
||||
}
|
||||
|
||||
type Foo struct {
|
||||
f string
|
||||
}
|
||||
|
||||
func (f *Foo) Foo() string {
|
||||
return f.f
|
||||
}
|
||||
|
||||
type Bar struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func NewBar(fooer Fooer) *Bar {
|
||||
return &Bar{Name: fooer.Foo()}
|
||||
}
|
||||
29
internal/wire/testdata/BindInjectorArgPointer/foo/wire.go
vendored
Normal file
29
internal/wire/testdata/BindInjectorArgPointer/foo/wire.go
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
// 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 (
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
||||
func inject(foo *Foo) *Bar {
|
||||
wire.Build(
|
||||
NewBar,
|
||||
wire.Bind(new(Fooer), new(*Foo)),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
1
internal/wire/testdata/BindInjectorArgPointer/pkg
vendored
Normal file
1
internal/wire/testdata/BindInjectorArgPointer/pkg
vendored
Normal file
@@ -0,0 +1 @@
|
||||
example.com/foo
|
||||
1
internal/wire/testdata/BindInjectorArgPointer/want/program_out.txt
vendored
Normal file
1
internal/wire/testdata/BindInjectorArgPointer/want/program_out.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
hello
|
||||
13
internal/wire/testdata/BindInjectorArgPointer/want/wire_gen.go
vendored
Normal file
13
internal/wire/testdata/BindInjectorArgPointer/want/wire_gen.go
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
// Code generated by Wire. DO NOT EDIT.
|
||||
|
||||
//go:generate wire
|
||||
//+build !wireinject
|
||||
|
||||
package main
|
||||
|
||||
// Injectors from wire.go:
|
||||
|
||||
func inject(foo *Foo) *Bar {
|
||||
bar := NewBar(foo)
|
||||
return bar
|
||||
}
|
||||
@@ -26,7 +26,7 @@ import (
|
||||
func inject() io.Writer {
|
||||
wire.Build(
|
||||
wire.Value(os.Stdout),
|
||||
wire.Bind(new(io.Writer), new(os.File)),
|
||||
wire.Bind(new(io.Writer), new(*os.File)),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ var mockAppSet = wire.NewSet(
|
||||
// For each mocked dependency, add a provider and use wire.Bind to bind
|
||||
// the concrete type to the relevant interface.
|
||||
newMockTimer,
|
||||
wire.Bind(new(timer), new(mockTimer)),
|
||||
wire.Bind(new(timer), new(*mockTimer)),
|
||||
)
|
||||
|
||||
type timer interface {
|
||||
|
||||
@@ -39,4 +39,4 @@ func provideBar() *Bar {
|
||||
|
||||
var Set = wire.NewSet(
|
||||
provideBar,
|
||||
wire.Bind((*foo.Fooer)(nil), (*Bar)(nil)))
|
||||
wire.Bind(new(foo.Fooer), new(*Bar)))
|
||||
|
||||
@@ -42,4 +42,4 @@ func provideBar() *Bar {
|
||||
|
||||
var Set = wire.NewSet(
|
||||
provideBar,
|
||||
wire.Bind((*Fooer)(nil), (*Bar)(nil)))
|
||||
wire.Bind(new(Fooer), new(*Bar)))
|
||||
|
||||
@@ -22,6 +22,6 @@ import (
|
||||
|
||||
func injectFooer() Fooer {
|
||||
// wrong: string doesn't implement Fooer.
|
||||
wire.Build(wire.Bind((*Fooer)(nil), "foo"))
|
||||
wire.Build(wire.Bind(new(Fooer), new(string)))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -22,6 +22,6 @@ import (
|
||||
|
||||
func injectFooer() Fooer {
|
||||
// wrong: wire.Bind requires 2 args.
|
||||
wire.Build(wire.Bind((*Fooer)(nil)))
|
||||
wire.Build(wire.Bind(new(Fooer)))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func injectFooBar() FooBar {
|
||||
wire.Build(
|
||||
provideBar,
|
||||
provideFooBar,
|
||||
wire.Bind((*Fooer)(nil), (*Bar)(nil)),
|
||||
wire.Bind(new(Fooer), new(*Bar)),
|
||||
)
|
||||
return FooBar{}
|
||||
}
|
||||
|
||||
@@ -49,5 +49,5 @@ func injectDuplicateValues() Foo {
|
||||
|
||||
func injectDuplicateInterface() Bar {
|
||||
// fail: provideBar and wire.Bind both provide Bar.
|
||||
panic(wire.Build(provideBar, wire.Bind(new(Bar), strings.NewReader("hello"))))
|
||||
panic(wire.Build(provideBar, wire.Bind(new(Bar), new(*strings.Reader))))
|
||||
}
|
||||
|
||||
@@ -44,6 +44,6 @@ var (
|
||||
// From the user guide:
|
||||
// Any set that includes an interface binding must also have a provider in
|
||||
// the same set that provides the concrete type.
|
||||
setB = wire.NewSet(wire.Bind(new(fooer), new(foo)))
|
||||
setB = wire.NewSet(wire.Bind(new(fooer), new(*foo)))
|
||||
setC = wire.NewSet(setA, setB)
|
||||
)
|
||||
|
||||
@@ -22,14 +22,14 @@ import (
|
||||
|
||||
func injectBar() Bar {
|
||||
wire.Build(
|
||||
provideFoo, // needed as input for provideBar
|
||||
provideBar, // needed for Bar
|
||||
partiallyUsedSet, // 1/2 providers in the set are needed
|
||||
provideUnused, // not needed -> error
|
||||
wire.Value("unused"), // not needed -> error
|
||||
unusedSet, // nothing in set is needed -> error
|
||||
wire.Bind((*Fooer)(nil), (*Foo)(nil)), // binding to Fooer is not needed -> error
|
||||
wire.FieldsOf(new(S), "Cfg"), // S.Cfg not needed -> error
|
||||
provideFoo, // needed as input for provideBar
|
||||
provideBar, // needed for Bar
|
||||
partiallyUsedSet, // 1/2 providers in the set are needed
|
||||
provideUnused, // not needed -> error
|
||||
wire.Value("unused"), // not needed -> error
|
||||
unusedSet, // nothing in set is needed -> error
|
||||
wire.Bind(new(Fooer), new(*Foo)), // binding to Fooer is not needed -> error
|
||||
wire.FieldsOf(new(S), "Cfg"), // S.Cfg not needed -> error
|
||||
)
|
||||
return 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user