From 925a11ad0d3a3591ccd4c1f99e085718267fe6ad Mon Sep 17 00:00:00 2001 From: Robert van Gent Date: Fri, 16 Nov 2018 13:27:30 -0800 Subject: [PATCH] wire: add a test for using a function argument as a provider (google/go-cloud#724) --- internal/wire/parse.go | 2 +- .../wire/testdata/FuncArgProvider/foo/foo.go | 36 +++++++++++++++++++ .../wire/testdata/FuncArgProvider/foo/wire.go | 26 ++++++++++++++ internal/wire/testdata/FuncArgProvider/pkg | 1 + .../FuncArgProvider/want/wire_errs.txt | 1 + 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 internal/wire/testdata/FuncArgProvider/foo/foo.go create mode 100644 internal/wire/testdata/FuncArgProvider/foo/wire.go create mode 100644 internal/wire/testdata/FuncArgProvider/pkg create mode 100644 internal/wire/testdata/FuncArgProvider/want/wire_errs.txt diff --git a/internal/wire/parse.go b/internal/wire/parse.go index 558551e..8b57e28 100644 --- a/internal/wire/parse.go +++ b/internal/wire/parse.go @@ -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 diff --git a/internal/wire/testdata/FuncArgProvider/foo/foo.go b/internal/wire/testdata/FuncArgProvider/foo/foo.go new file mode 100644 index 0000000..e794e4c --- /dev/null +++ b/internal/wire/testdata/FuncArgProvider/foo/foo.go @@ -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)} +} diff --git a/internal/wire/testdata/FuncArgProvider/foo/wire.go b/internal/wire/testdata/FuncArgProvider/foo/wire.go new file mode 100644 index 0000000..b970f83 --- /dev/null +++ b/internal/wire/testdata/FuncArgProvider/foo/wire.go @@ -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)) +} diff --git a/internal/wire/testdata/FuncArgProvider/pkg b/internal/wire/testdata/FuncArgProvider/pkg new file mode 100644 index 0000000..f7a5c8c --- /dev/null +++ b/internal/wire/testdata/FuncArgProvider/pkg @@ -0,0 +1 @@ +example.com/foo diff --git a/internal/wire/testdata/FuncArgProvider/want/wire_errs.txt b/internal/wire/testdata/FuncArgProvider/want/wire_errs.txt new file mode 100644 index 0000000..7155e14 --- /dev/null +++ b/internal/wire/testdata/FuncArgProvider/want/wire_errs.txt @@ -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 \ No newline at end of file