Skip to content

Commit

Permalink
feat: add MustRun and fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Sep 6, 2024
1 parent ba8379d commit 4a1d545
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
18 changes: 15 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

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

// This example demonstrates how to use [kod.MustRun] and [kod.Implements] to run a simple application.
func Example_componentRunMust() {
kod.MustRun(context.Background(), func(ctx context.Context, app *helloworld.App) error {
fmt.Println("Hello, World!")
return nil
})
// Output:
// helloWorld init
// Hello, World!
// helloWorld shutdown
}

// This example demonstrates how to use [kod.Ref] to reference a component and call a method on it.
func Example_componentRefAndCall() {
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
Expand Down Expand Up @@ -69,7 +81,7 @@ func Example_componentMock() {
}

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

// This example demonstrates how to use [kod.RunTest] to run a test function.
func Example_test() {
func Example_testRun() {
kod.RunTest(&testing.T{}, func(ctx context.Context, app *helloworld.App) {
app.HelloWorld.Get().SayHello(ctx)
})
Expand Down
4 changes: 2 additions & 2 deletions internal/kslog/otel_slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ func (h otelHandler) slogAttrToOtelAttr(attr slog.Attr, groupKeys ...string) att
func LogWithContext(ctx context.Context, logger *slog.Logger) *slog.Logger {
s := trace.SpanContextFromContext(ctx)
if s.HasTraceID() {
logger = logger.With("trace_id", s.TraceID().String())
logger = logger.With(slog.String("trace_id", s.TraceID().String()))
}
if s.HasSpanID() {
logger = logger.With("span_id", s.SpanID().String())
logger = logger.With(slog.String("span_id", s.SpanID().String()))
}

return logger
Expand Down
14 changes: 10 additions & 4 deletions kod.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
)

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

// T return the associated tracer.
// Tracer return the associated tracer.
func (i *Implements[T]) Tracer(opts ...trace.TracerOption) trace.Tracer {
return otel.Tracer(i.name, opts...)
}

// M return the associated meter.
// Meter return the associated meter.
func (i *Implements[T]) Meter(opts ...metric.MeterOption) metric.Meter {
return otel.GetMeterProvider().Meter(i.name, opts...)
}
Expand All @@ -68,7 +68,7 @@ func (i *Implements[T]) Meter(opts ...metric.MeterOption) metric.Meter {
// nolint
func (i *Implements[T]) setLogger(name string, log *slog.Logger) {
i.name = name
i.log = log.With("component", name)
i.log = log.With(slog.String("component", name))
}

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

// MustRun is a helper function to run the application with the provided main component and options.
// It panics if an error occurs during the execution.
func MustRun[T any, P PointerToMain[T]](ctx context.Context, run func(context.Context, *T) error, opts ...func(*options)) {
lo.Must0(Run[T, P](ctx, run, opts...))
}

// Run initializes and runs the application with the provided main component and options.
func Run[T any, _ PointerToMain[T]](ctx context.Context, run func(context.Context, *T) error, opts ...func(*options)) error {
// Create a new Kod instance.
Expand Down

0 comments on commit 4a1d545

Please sign in to comment.