wire: support using '-' tag to prevent filling struct fields (#163)
This commit is contained in:
2
internal/wire/testdata/Struct/foo/foo.go
vendored
2
internal/wire/testdata/Struct/foo/foo.go
vendored
@@ -16,6 +16,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/google/wire"
|
||||
)
|
||||
@@ -31,6 +32,7 @@ type Foo int
|
||||
type Bar int
|
||||
|
||||
type FooBar struct {
|
||||
mu sync.Mutex `wire:"-"`
|
||||
Foo Foo
|
||||
Bar Bar
|
||||
}
|
||||
|
||||
48
internal/wire/testdata/StructWithPreventTag/foo/foo.go
vendored
Normal file
48
internal/wire/testdata/StructWithPreventTag/foo/foo.go
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
// Copyright 2018 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"
|
||||
"sync"
|
||||
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
||||
func main() {
|
||||
pfb := injectPartFooBar()
|
||||
fmt.Println(pfb.Foo)
|
||||
}
|
||||
|
||||
type Foo int
|
||||
|
||||
type FooBar struct {
|
||||
mu sync.Mutex `wire:"-"`
|
||||
Foo Foo
|
||||
}
|
||||
|
||||
func provideFoo() Foo {
|
||||
return 42
|
||||
}
|
||||
|
||||
func provideMutex() sync.Mutex {
|
||||
return sync.Mutex{}
|
||||
}
|
||||
|
||||
var ProhibitSet = wire.NewSet(
|
||||
wire.Struct(new(FooBar), "mu", "Foo"),
|
||||
provideMutex,
|
||||
provideFoo,
|
||||
)
|
||||
26
internal/wire/testdata/StructWithPreventTag/foo/wire.go
vendored
Normal file
26
internal/wire/testdata/StructWithPreventTag/foo/wire.go
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
// Copyright 2018 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 injectPartFooBar() FooBar {
|
||||
wire.Build(ProhibitSet)
|
||||
return FooBar{}
|
||||
}
|
||||
1
internal/wire/testdata/StructWithPreventTag/pkg
vendored
Normal file
1
internal/wire/testdata/StructWithPreventTag/pkg
vendored
Normal file
@@ -0,0 +1 @@
|
||||
example.com/foo
|
||||
1
internal/wire/testdata/StructWithPreventTag/want/wire_errs.txt
vendored
Normal file
1
internal/wire/testdata/StructWithPreventTag/want/wire_errs.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
example.com/foo/foo.go:x:y: "mu" is prevented from injecting by wire
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(injectBar())
|
||||
fmt.Println(injectFooBar())
|
||||
}
|
||||
|
||||
type Foo int
|
||||
@@ -31,6 +31,12 @@ type UnusedInSet int
|
||||
type OneOfTwo int
|
||||
type TwoOfTwo int
|
||||
|
||||
type FooBar struct {
|
||||
MyFoo *Foo
|
||||
MyBar Bar
|
||||
MyUnused Unused
|
||||
}
|
||||
|
||||
var (
|
||||
unusedSet = wire.NewSet(provideUnusedInSet)
|
||||
partiallyUsedSet = wire.NewSet(provideOneOfTwo, provideTwoOfTwo)
|
||||
|
||||
@@ -20,16 +20,17 @@ import (
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
||||
func injectBar() Bar {
|
||||
func injectFooBar() FooBar {
|
||||
wire.Build(
|
||||
provideFoo, // needed as input for provideBar
|
||||
provideBar, // needed for Bar
|
||||
provideBar, // needed for FooBar
|
||||
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
|
||||
wire.Struct(new(FooBar), "MyFoo", "MyBar"), // needed for FooBar
|
||||
)
|
||||
return 0
|
||||
return FooBar{}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
example.com/foo/wire.go:x:y: inject injectBar: unused provider set "unusedSet"
|
||||
example.com/foo/wire.go:x:y: inject injectFooBar: unused provider set "unusedSet"
|
||||
|
||||
example.com/foo/wire.go:x:y: inject injectBar: unused provider "main.provideUnused"
|
||||
example.com/foo/wire.go:x:y: inject injectFooBar: unused provider "main.provideUnused"
|
||||
|
||||
example.com/foo/wire.go:x:y: inject injectBar: unused value of type string
|
||||
example.com/foo/wire.go:x:y: inject injectFooBar: unused value of type string
|
||||
|
||||
example.com/foo/wire.go:x:y: inject injectBar: unused interface binding to type example.com/foo.Fooer
|
||||
example.com/foo/wire.go:x:y: inject injectFooBar: unused interface binding to type example.com/foo.Fooer
|
||||
|
||||
example.com/foo/wire.go:x:y: inject injectBar: unused field "example.com/foo.S".Cfg
|
||||
example.com/foo/wire.go:x:y: inject injectFooBar: unused field "example.com/foo.S".Cfg
|
||||
Reference in New Issue
Block a user