@@ -11,10 +11,10 @@ import (
11
11
"github.com/go-kod/kod/interceptor/kmetric"
12
12
"github.com/go-kod/kod/interceptor/krecovery"
13
13
"github.com/go-kod/kod/interceptor/ktrace"
14
- "github.com/go-kod/kod/internal/kslog"
15
14
"go.uber.org/mock/gomock"
16
15
)
17
16
17
+ // This example demonstrates how to use [kod.Run] and [kod.Implements] to run a simple application.
18
18
func Example_mainComponent () {
19
19
kod .Run (context .Background (), func (ctx context.Context , app * helloworld.App ) error {
20
20
fmt .Println ("Hello, World!" )
@@ -26,6 +26,7 @@ func Example_mainComponent() {
26
26
// helloWorld shutdown
27
27
}
28
28
29
+ // This example demonstrates how to use [kod.Ref] to reference a component and call a method on it.
29
30
func Example_componentRefAndCall () {
30
31
kod .Run (context .Background (), func (ctx context.Context , app * helloworld.App ) error {
31
32
app .HelloWorld .Get ().SayHello (ctx )
@@ -37,6 +38,7 @@ func Example_componentRefAndCall() {
37
38
// helloWorld shutdown
38
39
}
39
40
41
+ // This example demonstrates how to use [kod.LazyInit] to defer component initialization until it is needed.
40
42
func Example_componentLazyInit () {
41
43
kod .Run (context .Background (), func (ctx context.Context , app * helloworld.App ) error {
42
44
app .HelloLazy .Get ().SayHello (ctx )
@@ -52,6 +54,7 @@ func Example_componentLazyInit() {
52
54
// helloWorld shutdown
53
55
}
54
56
57
+ // This example demonstrates how to use [kod.WithFakes] and [kod.Fake] to provide a mock implementation of a component.
55
58
func Example_componentMock () {
56
59
mock := helloworld .NewMockHelloWorld (gomock .NewController (nil ))
57
60
mock .EXPECT ().SayHello (gomock .Any ()).Return ()
@@ -65,21 +68,33 @@ func Example_componentMock() {
65
68
// Nothing printed from mock
66
69
}
67
70
71
+ // This example demonstrates how to use [kod.WithConfig] to provide a configuration to the application.
68
72
func Example_config () {
69
73
kod .Run (context .Background (), func (ctx context.Context , app * helloworld.App ) error {
70
- fmt .Println (app .Config ().Name )
71
74
app .HelloWorld .Get ().SayHello (ctx )
72
75
return nil
73
76
}, kod .WithConfigFile ("./examples/helloworld/config.toml" ))
74
77
// Output:
75
78
// helloWorld init
76
- // globalConfig
77
79
// Hello, World!config
78
80
// helloWorld shutdown
79
81
}
80
82
83
+ // This example demonstrates how to use [kod.WithGlobalConfig] to provide a global configuration to the application.
84
+ func Example_configGlobal () {
85
+ kod .Run (context .Background (), func (ctx context.Context , app * helloworld.App ) error {
86
+ fmt .Println (app .Config ().Name )
87
+ return nil
88
+ }, kod .WithConfigFile ("./examples/helloworld/config.toml" ))
89
+ // Output:
90
+ // helloWorld init
91
+ // globalConfig
92
+ // helloWorld shutdown
93
+ }
94
+
95
+ // This example demonstrates how to use [kod.WithLogger] to provide a custom logger to the application.
81
96
func Example_log () {
82
- logger , observer := kslog .NewTestLogger ()
97
+ logger , observer := kod .NewTestLogger ()
83
98
84
99
kod .RunTest (& testing.T {}, func (ctx context.Context , app * helloworld.App ) {
85
100
app .L (ctx ).Debug ("Hello, World!" )
@@ -100,6 +115,7 @@ func Example_log() {
100
115
// {"level":"INFO","msg":"Hello, World!","component":"github.com/go-kod/kod/examples/helloworld/HelloWorld"}
101
116
}
102
117
118
+ // This example demonstrates how to use [kod.WithInterceptors] to provide a custom interceptor to the application.
103
119
func Example_interceptor () {
104
120
interceptor := interceptor .Interceptor (func (ctx context.Context , info interceptor.CallInfo , req , res []interface {}, next interceptor.HandleFunc ) error {
105
121
fmt .Println ("Before call" )
@@ -120,6 +136,8 @@ func Example_interceptor() {
120
136
// helloWorld shutdown
121
137
}
122
138
139
+ // This example demonstrates how to use built-in interceptors
140
+ // Such as [krecovery.Interceptor], [ktrace.Interceptor], and [kmetric.Interceptor] ...
123
141
func Example_interceptorBuiltin () {
124
142
kod .Run (context .Background (), func (ctx context.Context , app * helloworld.App ) error {
125
143
app .HelloWorld .Get ().SayHello (ctx )
@@ -131,6 +149,7 @@ func Example_interceptorBuiltin() {
131
149
// helloWorld shutdown
132
150
}
133
151
152
+ // This example demonstrates how to use [kod.RunTest] to run a test function.
134
153
func Example_test () {
135
154
kod .RunTest (& testing.T {}, func (ctx context.Context , app * helloworld.App ) {
136
155
app .HelloWorld .Get ().SayHello (ctx )
@@ -141,6 +160,7 @@ func Example_test() {
141
160
// helloWorld shutdown
142
161
}
143
162
163
+ // This example demonstrates how to use [kod.RunTest], [kod.Fake] and [kod.WithFakes] to run a test function with a mock component.
144
164
func Example_testWithMockComponent () {
145
165
mock := helloworld .NewMockHelloWorld (gomock .NewController (nil ))
146
166
mock .EXPECT ().SayHello (gomock .Any ()).Return ()
@@ -153,6 +173,7 @@ func Example_testWithMockComponent() {
153
173
// Nothing printed from mock
154
174
}
155
175
176
+ // This example demonstrates how to use [kod.RunTest] and [kod.WithConfigFile] to run a test function with a configuration.
156
177
func Example_testWithConfig () {
157
178
kod .RunTest (& testing.T {}, func (ctx context.Context , app * helloworld.App ) {
158
179
fmt .Println (app .Config ().Name )
@@ -165,16 +186,26 @@ func Example_testWithConfig() {
165
186
// helloWorld shutdown
166
187
}
167
188
168
- // Example_testWithLogObserver demonstrates how to test log output .
189
+ // This example demonstrates how to use [kod.RunTest], [kod.NewTestLogger] and [kod.WithLogger] to run a test function with a custom logger .
169
190
func Example_testWithLogObserver () {
170
- kod .RunTest (& testing.T {}, func (ctx context.Context , app * helloworld.App ) {
191
+ logger , observer := kod .NewTestLogger ()
192
+
193
+ t := & testing.T {}
194
+ kod .RunTest (t , func (ctx context.Context , app * helloworld.App ) {
171
195
app .L (ctx ).Debug ("Hello, World!" )
172
196
app .L (ctx ).Info ("Hello, World!" )
173
197
app .L (ctx ).Warn ("Hello, World!" )
174
198
app .L (ctx ).Error ("Hello, World!" )
175
- })
199
+ }, kod .WithLogger (logger ))
200
+
201
+ fmt .Println (observer .Len ())
202
+ fmt .Println (observer .ErrorCount ())
203
+ fmt .Println (observer .Clean ().Len ())
176
204
177
205
// Output:
178
206
// helloWorld init
179
207
// helloWorld shutdown
208
+ // 3
209
+ // 1
210
+ // 0
180
211
}
0 commit comments