Skip to content

Commit 4b6c2a3

Browse files
committed
refactor: logger and example
1 parent df1a6c8 commit 4b6c2a3

16 files changed

+227
-214
lines changed

cmd/kod/internal/generate.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ var generate = &cobra.Command{
3939
func doGenerate(cmd *cobra.Command, dir string, args []string) {
4040
startTime := time.Now()
4141

42-
fmt.Printf("Changes found in [%s]\n", dir)
43-
4442
if s2i, _ := cmd.Flags().GetBool("struct2interface"); s2i {
4543
if err := Struct2Interface(cmd, dir); err != nil {
4644
fmt.Println(err)
@@ -49,6 +47,8 @@ func doGenerate(cmd *cobra.Command, dir string, args []string) {
4947
fmt.Printf("[struct2interface] %s \n", time.Since(startTime).String())
5048
}
5149

50+
startTime = time.Now()
51+
5252
if err := Generate(dir, args, Options{}); err != nil {
5353
fmt.Println(err)
5454
return

cmd/kod/internal/struct2interface.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,16 @@ func Struct2Interface(c *cobra.Command, dir string) error {
322322
return nil
323323
}
324324

325-
// if dir == "." means only generate interface for the current directory
326-
if dir == "." && filepath.Dir(path) != dir {
325+
if !strings.HasSuffix(filepath.Base(path), ".go") {
327326
return nil
328327
}
329328

330329
if strings.HasPrefix(filepath.Base(path), "kod_gen") {
331330
return nil
332331
}
333-
if !strings.HasSuffix(filepath.Base(path), ".go") {
332+
333+
// if dir == "." means only generate interface for the current directory
334+
if dir == "." && filepath.Dir(path) != dir {
334335
return nil
335336
}
336337

example_test.go

+49-25
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import (
1111
"github.com/go-kod/kod/interceptor/kmetric"
1212
"github.com/go-kod/kod/interceptor/krecovery"
1313
"github.com/go-kod/kod/interceptor/ktrace"
14+
"github.com/go-kod/kod/internal/kslog"
1415
"go.uber.org/mock/gomock"
1516
)
1617

17-
func Example_helloWorld() {
18+
func Example_mainComponent() {
1819
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
1920
fmt.Println("Hello, World!")
2021
return nil
@@ -25,7 +26,7 @@ func Example_helloWorld() {
2526
// helloWorld shutdown
2627
}
2728

28-
func Example_callComponent() {
29+
func Example_componentRefAndCall() {
2930
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
3031
app.HelloWorld.Get().SayHello(ctx)
3132
return nil
@@ -36,7 +37,22 @@ func Example_callComponent() {
3637
// helloWorld shutdown
3738
}
3839

39-
func Example_mockComponent() {
40+
func Example_componentLazyInit() {
41+
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
42+
app.HelloLazy.Get().SayHello(ctx)
43+
app.HelloWorld.Get().SayHello(ctx)
44+
return nil
45+
})
46+
// Output:
47+
// helloWorld init
48+
// lazyHelloBob init
49+
// Hello, Bob!
50+
// Hello, World!
51+
// lazyHelloBob shutdown
52+
// helloWorld shutdown
53+
}
54+
55+
func Example_componentMock() {
4056
mock := helloworld.NewMockHelloWorld(gomock.NewController(nil))
4157
mock.EXPECT().SayHello(gomock.Any()).Return()
4258

@@ -63,28 +79,25 @@ func Example_config() {
6379
}
6480

6581
func Example_log() {
66-
wrapper, observer := kod.NewLogObserver()
82+
logger, observer := kslog.NewTestLogger()
6783

68-
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
84+
kod.RunTest(&testing.T{}, func(ctx context.Context, app *helloworld.App) {
6985
app.L(ctx).Debug("Hello, World!")
7086
app.L(ctx).Info("Hello, World!")
7187
app.L(ctx).Warn("Hello, World!")
7288
app.L(ctx).Error("Hello, World!")
73-
return nil
74-
}, kod.WithLogWrapper(wrapper))
75-
76-
fmt.Println(observer.Len())
77-
for _, entry := range observer.All() {
78-
fmt.Println(entry.Level, entry.Message)
79-
}
89+
app.HelloWorld.Get().SayHello(ctx)
90+
}, kod.WithLogger(logger))
8091

92+
fmt.Println(observer)
8193
// Output:
8294
// helloWorld init
95+
// Hello, World!
8396
// helloWorld shutdown
84-
// 3
85-
// INFO Hello, World!
86-
// WARN Hello, World!
87-
// ERROR Hello, World!
97+
// {"level":"INFO","msg":"Hello, World!","component":"github.com/go-kod/kod/Main"}
98+
// {"level":"WARN","msg":"Hello, World!","component":"github.com/go-kod/kod/Main"}
99+
// {"level":"ERROR","msg":"Hello, World!","component":"github.com/go-kod/kod/Main"}
100+
// {"level":"INFO","msg":"Hello, World!","component":"github.com/go-kod/kod/examples/helloworld/HelloWorld"}
88101
}
89102

90103
func Example_interceptor() {
@@ -107,7 +120,7 @@ func Example_interceptor() {
107120
// helloWorld shutdown
108121
}
109122

110-
func Example_builtinInterceptor() {
123+
func Example_interceptorBuiltin() {
111124
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
112125
app.HelloWorld.Get().SayHello(ctx)
113126
return nil
@@ -140,17 +153,28 @@ func Example_testWithMockComponent() {
140153
// Nothing printed from mock
141154
}
142155

143-
func Example_lazyInit() {
144-
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
145-
app.HelloBob.Get().SayHello(ctx)
156+
func Example_testWithConfig() {
157+
kod.RunTest(&testing.T{}, func(ctx context.Context, app *helloworld.App) {
158+
fmt.Println(app.Config().Name)
146159
app.HelloWorld.Get().SayHello(ctx)
147-
return nil
160+
}, kod.WithConfigFile("./examples/helloworld/config.toml"))
161+
// Output:
162+
// helloWorld init
163+
// globalConfig
164+
// Hello, World!config
165+
// helloWorld shutdown
166+
}
167+
168+
// Example_testWithLogObserver demonstrates how to test log output.
169+
func Example_testWithLogObserver() {
170+
kod.RunTest(&testing.T{}, func(ctx context.Context, app *helloworld.App) {
171+
app.L(ctx).Debug("Hello, World!")
172+
app.L(ctx).Info("Hello, World!")
173+
app.L(ctx).Warn("Hello, World!")
174+
app.L(ctx).Error("Hello, World!")
148175
})
176+
149177
// Output:
150178
// helloWorld init
151-
// lazyHelloBob init
152-
// Hello, Bob!
153-
// Hello, World!
154-
// lazyHelloBob shutdown
155179
// helloWorld shutdown
156180
}

examples/helloworld/helloworld.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type App struct {
1212
kod.WithGlobalConfig[GlobalConfig]
1313

1414
HelloWorld kod.Ref[HelloWorld]
15-
HelloBob kod.Ref[HelloBob]
15+
HelloLazy kod.Ref[HelloLazy]
1616
}
1717

1818
type GlobalConfig struct {
@@ -34,6 +34,8 @@ func (h *helloWorld) Init(ctx context.Context) error {
3434
}
3535

3636
func (h *helloWorld) SayHello(ctx context.Context) {
37+
h.L(ctx).Info("Hello, World!")
38+
3739
fmt.Println("Hello, World!" + h.Config().Name)
3840
}
3941

@@ -43,7 +45,7 @@ func (h *helloWorld) Shutdown(ctx context.Context) error {
4345
}
4446

4547
type lazyHelloBob struct {
46-
kod.Implements[HelloBob]
48+
kod.Implements[HelloLazy]
4749
kod.LazyInit
4850
}
4951

examples/helloworld/kod_gen.go

+12-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/helloworld/kod_gen_interface.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/helloworld/kod_gen_mock.go

+14-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/kslog/log_level.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@ func (h *levelHandler) Handle(ctx context.Context, r slog.Record) error {
2828

2929
// WithAttrs returns a new slog.handler with the provided attributes.
3030
func (h *levelHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
31-
h.next.WithAttrs(attrs)
32-
return h
31+
return &levelHandler{level: h.level, next: h.next.WithAttrs(attrs)}
3332
}
3433

3534
// WithGroup returns a slog.handler with a group, provided the group's name.
3635
func (h *levelHandler) WithGroup(name string) slog.Handler {
37-
h.next.WithGroup(name)
38-
return h
36+
return &levelHandler{level: h.level, next: h.next.WithGroup(name)}
3937
}
4038

4139
// Enabled returns true if the provided level is enabled.

0 commit comments

Comments
 (0)