wire: Build now returns an error if it has any unused arguments (google/go-cloud#268)

Fixes google/go-cloud#164
This commit is contained in:
Robert van Gent
2018-08-02 09:31:50 -07:00
committed by Ross Light
parent b348a78000
commit 85deb53791
6 changed files with 204 additions and 9 deletions

View File

@@ -0,0 +1,71 @@
// Copyright 2018 Google LLC
//
// 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"
"github.com/google/go-cloud/wire"
)
func main() {
fmt.Println(injectBar())
}
type Foo int
type Bar int
type Unused int
type UnusedInSet int
type OneOfTwo int
type TwoOfTwo int
var (
unusedSet = wire.NewSet(provideUnusedInSet)
partiallyUsedSet = wire.NewSet(provideOneOfTwo, provideTwoOfTwo)
)
type Fooer interface {
Foo() string
}
func (f *Foo) Foo() string {
return fmt.Sprintf("Hello World %d", f)
}
func provideFoo() *Foo {
f := new(Foo)
*f = 1
return f
}
func provideBar(foo *Foo, one OneOfTwo) Bar {
return Bar(int(*foo) + int(one))
}
func provideUnused() Unused {
return 1
}
func provideUnusedInSet() UnusedInSet {
return 1
}
func provideOneOfTwo() OneOfTwo {
return 1
}
func provideTwoOfTwo() TwoOfTwo {
return 1
}

View File

@@ -0,0 +1,34 @@
// Copyright 2018 Google LLC
//
// 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() 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
)
return 0
}

View File

@@ -0,0 +1,5 @@
ERROR
unused provider set
unused provider "provideUnused"
unused value of type string
unused interface binding to type foo.Fooer

View File

@@ -0,0 +1 @@
foo