wire: add a test for using a function argument as a provider (google/go-cloud#724)

This commit is contained in:
Robert van Gent
2018-11-16 13:27:30 -08:00
committed by Ross Light
parent 6ea381b3fe
commit 925a11ad0d
5 changed files with 65 additions and 1 deletions

View File

@@ -455,7 +455,7 @@ func (oc *objectCache) get(obj types.Object) (val interface{}, errs []error) {
switch obj := obj.(type) {
case *types.Var:
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)}
}
var i int

View 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)}
}

View 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))
}

View File

@@ -0,0 +1 @@
example.com/foo

View 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