-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
49 lines (43 loc) · 1.9 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package nject_test
import (
"errors"
"fmt"
"strings"
"github.com/muir/nject"
)
// Example shows what gets included and what does not for several injection chains.
// These examples are meant to show the subtlety of what gets included and why.
func Example() {
// This demonstrates displaying the elements of a chain using an error
// returned by the final element.
fmt.Println(nject.Run("empty-chain",
nject.Provide("Names", func(d *nject.Debugging) error {
return errors.New(strings.Join(d.NamesIncluded, ", "))
})))
// This demonstrates that wrappers will be included if they are closest
// provider of a return type that is required. Names is included in
// the upwards chain even though ReflectError could provide the error that
// Run() wants.
fmt.Println(nject.Run("overwrite",
nject.Required(nject.Provide("InjectErrorDownward",
func() error { return errors.New("overwrite me") })),
nject.Provide("Names",
func(inner func() error, d *nject.Debugging) error {
inner()
return errors.New(strings.Join(d.NamesIncluded, ", "))
}),
nject.Provide("ReflectError",
func(err error) error { return err })))
// This demonstrates that the closest provider will be chosen over one farther away.
// Otherwise InInjector would be included instead of BoolInjector and IntReinjector.
fmt.Println(nject.Run("multiple-choices",
nject.Provide("IntInjector", func() int { return 1 }),
nject.Provide("BoolInjector", func() bool { return true }),
nject.Provide("IntReinjector", func(bool) int { return 2 }),
nject.Provide("IntConsumer", func(i int, d *nject.Debugging) error {
return errors.New(strings.Join(d.NamesIncluded, ", "))
})))
// Output: Debugging, empty-chain invoke func, Run()error, Names
// Debugging, overwrite invoke func, Run()error, InjectErrorDownward, Names, ReflectError
// Debugging, multiple-choices invoke func, Run()error, BoolInjector, IntReinjector, IntConsumer
}