-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_provider_test.go
141 lines (132 loc) · 3.21 KB
/
example_provider_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package nject_test
import (
"fmt"
"strconv"
"github.com/muir/nject"
)
// Provide does one job: it names an otherwise anonymous
// function so that it easier to identify if there is an
// error creating an injection chain.
func ExampleProvide() {
fmt.Println(nject.Run("failure1",
func(s string) int {
return 4
},
))
fmt.Println(nject.Run("failure2",
nject.Provide("create-int", func(s string) int {
return 4
}),
))
// Output: final-func: failure1(0) [func(string) int]: required but has no match for its input parameter string
// final-func: create-int [func(string) int]: required but has no match for its input parameter string
}
func ExampleProvide_literal() {
fmt.Println(nject.Run("literals",
nject.Provide("an int", 7),
"I am a literal string", // naked literal work too
nject.Provide("I-am-a-final-func", func(s string, i int) {
fmt.Println("final:", s, i)
}),
))
// Output: final: I am a literal string 7
// <nil>
}
func ExampleProvide_regular_injector() {
fmt.Println(nject.Run("regular",
func() int {
return 7
},
nject.Provide("convert-int-to-string",
func(i int) string {
return strconv.Itoa(i)
},
),
func(s string) {
fmt.Println(s)
},
))
// Output: 7
// <nil>
}
// This demonstrates multiple types of injectors including a
// wrapper and a fallible injector
func ExampleProvide_wrapper_and_fallible_injectors() {
shouldFail := true
seq := nject.Sequence("fallible",
nject.Provide("example-wrapper",
func(inner func() (string, error)) {
s, err := inner()
fmt.Println("string:", s, "error:", err)
}),
nject.Provide("example-injector",
func() bool {
return shouldFail
}),
nject.Provide("example-fallible-injector",
func(b bool) (string, nject.TerminalError) {
if b {
return "", fmt.Errorf("oops, failing")
}
return "example", nil
}),
nject.Provide("example-final-injector",
func(s string) string {
return "final: " + s
}),
)
fmt.Println(nject.Run("failure", seq))
shouldFail = false
fmt.Println(nject.Run("success", seq))
// Output: string: error: oops, failing
// oops, failing
// string: final: example error: <nil>
// <nil>
}
// This demonstrates the use of NonFinal. NonFinal is useful when
// manipulating lists of providers.
func ExampleNonFinal() {
seq := nject.Sequence("example",
func() string {
return "some string"
},
func(i int, s string) {
fmt.Println("final", i, s)
},
)
fmt.Println(nject.Run("almost incomplete",
seq,
nject.NonFinal(func() int {
return 20
}),
))
// Output: final 20 some string
// <nil>
}
func ExampleSaveTo() {
var s string
var i int
fmt.Println(nject.Run("example",
func() string { return "one" },
func() int { return 3 },
nject.MustSaveTo(&s, &i)), s, i)
// Output: <nil> one 3
}
func ExampleCurry() {
lotsOfUnchangingArgs := func(s string, i int, u uint) string {
return fmt.Sprintf("%s-%d-%d", s, i, u)
}
var shorthand func(i int) string
fmt.Println(nject.Run("example",
func() string { return "foo" },
func() uint { return 33 },
nject.MustCurry(lotsOfUnchangingArgs, &shorthand),
func() {
fmt.Println("actual injection goal")
},
))
fmt.Println(shorthand(10))
// Output: actual injection goal
// <nil>
// foo-10-33
}