Skip to content

Commit

Permalink
chore: add example for component interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Sep 6, 2024
1 parent 12c78d2 commit b2d7fee
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 51 deletions.
22 changes: 18 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Example_componentRefAndCall() {
// This example demonstrates how to use [kod.LazyInit] to defer component initialization until it is needed.
func Example_componentLazyInit() {
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
app.HelloLazy.Get().SayHello(ctx)
app.HelloWorldLazy.Get().SayHello(ctx)
app.HelloWorld.Get().SayHello(ctx)
return nil
})
Expand Down Expand Up @@ -169,8 +169,8 @@ func Example_openTelemetryMetric() {
// helloWorld shutdown
}

// This example demonstrates how to use [kod.WithInterceptors] to provide a custom interceptor to the application.
func Example_interceptor() {
// This example demonstrates how to use [kod.WithInterceptors] to provide global interceptors to the application.
func Example_interceptorGlobal() {
interceptor := interceptor.Interceptor(func(ctx context.Context, info interceptor.CallInfo, req, res []interface{}, next interceptor.HandleFunc) error {
fmt.Println("Before call")
err := next(ctx, info, req, res)
Expand All @@ -190,7 +190,21 @@ func Example_interceptor() {
// helloWorld shutdown
}

// This example demonstrates how to use built-in interceptors
// This example demonstrates how to use [kod.WithInterceptors] to provide component-specific interceptors to the application.
func Example_interceptorComponent() {
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
app.HelloWorldInterceptor.Get().SayHello(ctx)
return nil
})
// Output:
// helloWorld init
// Before call
// Hello, Interceptor!
// After call
// helloWorld shutdown
}

// This example demonstrates how to use built-in interceptors with [kod.WithInterceptors].
// Such as [krecovery.Interceptor], [ktrace.Interceptor], and [kmetric.Interceptor] ...
func Example_interceptorBuiltin() {
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
Expand Down
35 changes: 28 additions & 7 deletions examples/helloworld/helloworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"fmt"

"github.com/go-kod/kod"
"github.com/go-kod/kod/interceptor"
)

type App struct {
kod.Implements[kod.Main]
kod.WithGlobalConfig[GlobalConfig]

HelloWorld kod.Ref[HelloWorld]
HelloLazy kod.Ref[HelloLazy]
HelloWorld kod.Ref[HelloWorld]
HelloWorldLazy kod.Ref[HelloWorldLazy]
HelloWorldInterceptor kod.Ref[HelloWorldInterceptor]
}

type GlobalConfig struct {
Expand Down Expand Up @@ -44,21 +46,40 @@ func (h *helloWorld) Shutdown(ctx context.Context) error {
return nil
}

type lazyHelloBob struct {
kod.Implements[HelloLazy]
type lazyHelloWorld struct {
kod.Implements[HelloWorldLazy]
kod.LazyInit
}

func (h *lazyHelloBob) Init(ctx context.Context) error {
func (h *lazyHelloWorld) Init(ctx context.Context) error {
fmt.Println("lazyHelloBob init")
return nil
}

func (h *lazyHelloBob) SayHello(ctx context.Context) {
func (h *lazyHelloWorld) SayHello(ctx context.Context) {
fmt.Println("Hello, Bob!")
}

func (h *lazyHelloBob) Shutdown(ctx context.Context) error {
func (h *lazyHelloWorld) Shutdown(ctx context.Context) error {
fmt.Println("lazyHelloBob shutdown")
return nil
}

type helloWorldInterceptor struct {
kod.Implements[HelloWorldInterceptor]
}

func (h *helloWorldInterceptor) SayHello(ctx context.Context) {
fmt.Println("Hello, Interceptor!")
}

func (h *helloWorldInterceptor) Interceptors() []interceptor.Interceptor {
return []interceptor.Interceptor{
func(ctx context.Context, info interceptor.CallInfo, req, reply []any, invoker interceptor.HandleFunc) error {
fmt.Println("Before call")
err := invoker(ctx, info, req, reply)
fmt.Println("After call")
return err
},
}
}
101 changes: 77 additions & 24 deletions examples/helloworld/kod_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions examples/helloworld/kod_gen_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 49 additions & 14 deletions examples/helloworld/kod_gen_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b2d7fee

Please sign in to comment.