wire: add a test for using a function argument as a provider (google/go-cloud#724)
This commit is contained in:
committed by
Ross Light
parent
6ea381b3fe
commit
925a11ad0d
@@ -455,7 +455,7 @@ func (oc *objectCache) get(obj types.Object) (val interface{}, errs []error) {
|
|||||||
switch obj := obj.(type) {
|
switch obj := obj.(type) {
|
||||||
case *types.Var:
|
case *types.Var:
|
||||||
spec := oc.varDecl(obj)
|
spec := oc.varDecl(obj)
|
||||||
if len(spec.Values) == 0 {
|
if spec == nil || len(spec.Values) == 0 {
|
||||||
return nil, []error{fmt.Errorf("%v is not a provider or a provider set", obj)}
|
return nil, []error{fmt.Errorf("%v is not a provider or a provider set", obj)}
|
||||||
}
|
}
|
||||||
var i int
|
var i int
|
||||||
|
|||||||
36
internal/wire/testdata/FuncArgProvider/foo/foo.go
vendored
Normal file
36
internal/wire/testdata/FuncArgProvider/foo/foo.go
vendored
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
// Copyright 2018 The Go Cloud 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() {
|
||||||
|
bar := injectBar(func() *Foo { return &Foo{42} })
|
||||||
|
fmt.Println(bar.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Foo struct {
|
||||||
|
Val int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Bar struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBar(f *Foo) *Bar {
|
||||||
|
return &Bar{Name: fmt.Sprintf("foo value %d", f.Val)}
|
||||||
|
}
|
||||||
26
internal/wire/testdata/FuncArgProvider/foo/wire.go
vendored
Normal file
26
internal/wire/testdata/FuncArgProvider/foo/wire.go
vendored
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
// Copyright 2018 The Go Cloud 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/go-cloud/wire"
|
||||||
|
)
|
||||||
|
|
||||||
|
func injectBar(fn func() *Foo) *Bar {
|
||||||
|
// fails because it doesn't identify fn as a Provider; see #723.
|
||||||
|
panic(wire.Build(fn, NewBar))
|
||||||
|
}
|
||||||
1
internal/wire/testdata/FuncArgProvider/pkg
vendored
Normal file
1
internal/wire/testdata/FuncArgProvider/pkg
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
example.com/foo
|
||||||
1
internal/wire/testdata/FuncArgProvider/want/wire_errs.txt
vendored
Normal file
1
internal/wire/testdata/FuncArgProvider/want/wire_errs.txt
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
example.com/foo/wire.go:x:y: var fn func() *example.com/foo.Foo is not a provider or a provider set
|
||||||
Reference in New Issue
Block a user