Skip to content

Commit

Permalink
refactor: add lazy init example
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Sep 4, 2024
1 parent 55ff08d commit 8a84f10
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 6 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ jobs:
fail-fast: false
matrix:
version: ["1.23.x", "1.22.x"]
services:
redis:
image: redis
ports:
- 6379:6379

steps:
- name: Check out repository
uses: actions/checkout@v4
Expand Down
29 changes: 29 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ func Example_helloWorld() {
return nil
})
// Output:
// helloWorld init
// Hello, World!
// helloWorld shutdown
}

func Example_callComponent() {
Expand All @@ -29,7 +31,9 @@ func Example_callComponent() {
return nil
})
// Output:
// helloWorld init
// Hello, World!
// helloWorld shutdown
}

func Example_mockComponent() {
Expand All @@ -52,8 +56,10 @@ func Example_config() {
return nil
}, kod.WithConfigFile("./examples/helloworld/config.toml"))
// Output:
// helloWorld init
// globalConfig
// Hello, World!config
// helloWorld shutdown
}

func Example_log() {
Expand All @@ -73,6 +79,8 @@ func Example_log() {
}

// Output:
// helloWorld init
// helloWorld shutdown
// 3
// INFO Hello, World!
// WARN Hello, World!
Expand All @@ -92,9 +100,11 @@ func Example_interceptor() {
return nil
}, kod.WithInterceptors(interceptor))
// Output:
// helloWorld init
// Before call
// Hello, World!
// After call
// helloWorld shutdown
}

func Example_builtinInterceptor() {
Expand All @@ -103,15 +113,19 @@ func Example_builtinInterceptor() {
return nil
}, kod.WithInterceptors(krecovery.Interceptor(), ktrace.Interceptor(), kmetric.Interceptor()))
// Output:
// helloWorld init
// Hello, World!
// helloWorld shutdown
}

func Example_test() {
kod.RunTest(&testing.T{}, func(ctx context.Context, t *helloworld.App) {
t.HelloWorld.Get().SayHello(ctx)
})
// Output:
// helloWorld init
// Hello, World!
// helloWorld shutdown
}

func Example_testWithMockComponent() {
Expand All @@ -125,3 +139,18 @@ func Example_testWithMockComponent() {
// Output:
// Nothing printed from mock
}

func Example_lazyInit() {
kod.Run(context.Background(), func(ctx context.Context, t *helloworld.App) error {
t.HelloBob.Get().SayHello(ctx)
t.HelloWorld.Get().SayHello(ctx)
return nil
})
// Output:
// helloWorld init
// lazyHelloBob init
// Hello, Bob!
// Hello, World!
// lazyHelloBob shutdown
// helloWorld shutdown
}
30 changes: 30 additions & 0 deletions examples/helloworld/helloworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,43 @@ type App struct {
kod.WithGlobalConfig[Config]

HelloWorld kod.Ref[HelloWorld]
HelloBob kod.Ref[HelloBob]
}

type helloWorld struct {
kod.Implements[HelloWorld]
kod.WithConfig[Config]
}

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

func (h *helloWorld) SayHello(ctx context.Context) {
fmt.Println("Hello, World!" + h.Config().Name)
}

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

type lazyHelloBob struct {
kod.Implements[HelloBob]
kod.LazyInit
}

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

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

func (h *lazyHelloBob) Shutdown(ctx context.Context) error {
fmt.Println("lazyHelloBob shutdown")
return nil
}
55 changes: 54 additions & 1 deletion examples/helloworld/kod_gen.go

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

5 changes: 5 additions & 0 deletions examples/helloworld/kod_gen_interface.go

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

35 changes: 35 additions & 0 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 8a84f10

Please sign in to comment.