goose: add goose.Value directive

Subsumes previous usage of goose.Optional.

Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Tuo Shan <shantuo@google.com>
This commit is contained in:
Ross Light
2018-05-04 12:44:53 -04:00
parent 235a7d8f80
commit 10676a814b
37 changed files with 706 additions and 60 deletions

View File

@@ -304,6 +304,33 @@ func injectFooBar() FooBar {
And similarly if the injector needed a `*FooBar`.
### Binding Values
Occasionally, it is useful to bind a basic value (usually `nil`) to a type.
Instead of having injectors depend on a throwaway provider function, you can
add a value expression to a provider set.
```go
type Foo int
func injectFoo() Foo {
panic(goose.Use(goose.Value(Foo(42))))
}
```
The generated injector would look like this:
```go
func injectFoo() Foo {
foo := Foo(42)
return foo
}
```
It's important to note that the expression will be copied, so references to
variables will be evaluated during the call to the injector. goose will emit
an error if the expression calls any functions.
### Cleanup functions
If a provider creates a value that needs to be cleaned up (e.g. closing a file),