Skip to content

Commit 4a1d545

Browse files
committed
feat: add MustRun and fix comments
1 parent ba8379d commit 4a1d545

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

example_test.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
// This example demonstrates how to use [kod.Run] and [kod.Implements] to run a simple application.
18-
func Example_componentMain() {
18+
func Example_componentRun() {
1919
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
2020
fmt.Println("Hello, World!")
2121
return nil
@@ -26,6 +26,18 @@ func Example_componentMain() {
2626
// helloWorld shutdown
2727
}
2828

29+
// This example demonstrates how to use [kod.MustRun] and [kod.Implements] to run a simple application.
30+
func Example_componentRunMust() {
31+
kod.MustRun(context.Background(), func(ctx context.Context, app *helloworld.App) error {
32+
fmt.Println("Hello, World!")
33+
return nil
34+
})
35+
// Output:
36+
// helloWorld init
37+
// Hello, World!
38+
// helloWorld shutdown
39+
}
40+
2941
// This example demonstrates how to use [kod.Ref] to reference a component and call a method on it.
3042
func Example_componentRefAndCall() {
3143
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
@@ -69,7 +81,7 @@ func Example_componentMock() {
6981
}
7082

7183
// This example demonstrates how to use [kod.WithConfig] to provide a configuration to the application.
72-
func Example_config() {
84+
func Example_configInComponent() {
7385
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
7486
app.HelloWorld.Get().SayHello(ctx)
7587
return nil
@@ -192,7 +204,7 @@ func Example_interceptorBuiltin() {
192204
}
193205

194206
// This example demonstrates how to use [kod.RunTest] to run a test function.
195-
func Example_test() {
207+
func Example_testRun() {
196208
kod.RunTest(&testing.T{}, func(ctx context.Context, app *helloworld.App) {
197209
app.HelloWorld.Get().SayHello(ctx)
198210
})

internal/kslog/otel_slog.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ func (h otelHandler) slogAttrToOtelAttr(attr slog.Attr, groupKeys ...string) att
163163
func LogWithContext(ctx context.Context, logger *slog.Logger) *slog.Logger {
164164
s := trace.SpanContextFromContext(ctx)
165165
if s.HasTraceID() {
166-
logger = logger.With("trace_id", s.TraceID().String())
166+
logger = logger.With(slog.String("trace_id", s.TraceID().String()))
167167
}
168168
if s.HasSpanID() {
169-
logger = logger.With("span_id", s.SpanID().String())
169+
logger = logger.With(slog.String("span_id", s.SpanID().String()))
170170
}
171171

172172
return logger

kod.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const (
4141
)
4242

4343
// Implements[T any] provides a common structure for components,
44-
// with logging capabilities and a reference to the component's interface.
44+
// with logging/tracing/metrics capabilities and a reference to the component's interface.
4545
type Implements[T any] struct {
4646
name string
4747
log *slog.Logger
@@ -54,12 +54,12 @@ func (i *Implements[T]) L(ctx context.Context) *slog.Logger {
5454
return kslog.LogWithContext(ctx, i.log)
5555
}
5656

57-
// T return the associated tracer.
57+
// Tracer return the associated tracer.
5858
func (i *Implements[T]) Tracer(opts ...trace.TracerOption) trace.Tracer {
5959
return otel.Tracer(i.name, opts...)
6060
}
6161

62-
// M return the associated meter.
62+
// Meter return the associated meter.
6363
func (i *Implements[T]) Meter(opts ...metric.MeterOption) metric.Meter {
6464
return otel.GetMeterProvider().Meter(i.name, opts...)
6565
}
@@ -68,7 +68,7 @@ func (i *Implements[T]) Meter(opts ...metric.MeterOption) metric.Meter {
6868
// nolint
6969
func (i *Implements[T]) setLogger(name string, log *slog.Logger) {
7070
i.name = name
71-
i.log = log.With("component", name)
71+
i.log = log.With(slog.String("component", name))
7272
}
7373

7474
// implements is a marker method to assert implementation of an interface.
@@ -256,6 +256,12 @@ func WithLogger(logger *slog.Logger) func(*options) {
256256
}
257257
}
258258

259+
// MustRun is a helper function to run the application with the provided main component and options.
260+
// It panics if an error occurs during the execution.
261+
func MustRun[T any, P PointerToMain[T]](ctx context.Context, run func(context.Context, *T) error, opts ...func(*options)) {
262+
lo.Must0(Run[T, P](ctx, run, opts...))
263+
}
264+
259265
// Run initializes and runs the application with the provided main component and options.
260266
func Run[T any, _ PointerToMain[T]](ctx context.Context, run func(context.Context, *T) error, opts ...func(*options)) error {
261267
// Create a new Kod instance.

0 commit comments

Comments
 (0)