diff --git a/cmd/kod/internal/generate_generator.go b/cmd/kod/internal/generate_generator.go index aff2d4c..4e18003 100644 --- a/cmd/kod/internal/generate_generator.go +++ b/cmd/kod/internal/generate_generator.go @@ -740,13 +740,14 @@ func (g *generator) generateLocalStubs(p printFn) { p(``) p(``) p(`// Local stub implementations.`) + p(``) g.tset.importPackage("context", "context") var b strings.Builder for _, comp := range g.components { stub := notExported(comp.intfName()) + "_local_stub" - p(``) + p(`// %s is a local stub implementation of [%s].`, stub, g.tset.genTypeString(comp.intf)) p(`type %s struct{`, stub) p(` impl %s`, g.componentRef(comp)) p(` name string`) diff --git a/cmd/kod/internal/struct2interface.go b/cmd/kod/internal/struct2interface.go index 64dab9f..37bc13e 100644 --- a/cmd/kod/internal/struct2interface.go +++ b/cmd/kod/internal/struct2interface.go @@ -296,9 +296,12 @@ func makeFile(file string) (*makeInterfaceFile, error) { } } + for _, structName := range structInfo.structs { + typeDoc[structName] = fmt.Sprintf("%s is a component interface implemented by [%s].\n%s", + structInfo.struct2Interfaces[structName], structName, structInfo.typeDoc[structName]) + } + for structName, methods := range structInfo.methods { - typeDoc[structName] = fmt.Sprintf("%s is a component that implements %s.\n%s", - structName, structInfo.struct2Interfaces[structName], structInfo.typeDoc[structName]) for _, m := range methods { allMethods[structName] = append(allMethods[structName], m.Lines()...) } diff --git a/examples/helloworld/kod_gen.go b/examples/helloworld/kod_gen.go index d49f629..91a6fc8 100644 --- a/examples/helloworld/kod_gen.go +++ b/examples/helloworld/kod_gen.go @@ -103,6 +103,7 @@ var _ kod.InstanceOf[kod.Main] = (*App)(nil) // Local stub implementations. +// helloWorld_local_stub is a local stub implementation of [HelloWorld]. type helloWorld_local_stub struct { impl HelloWorld name string @@ -134,6 +135,7 @@ func (s helloWorld_local_stub) SayHello(ctx context.Context) { _ = s.interceptor(ctx, info, []any{}, []any{}, call) } +// helloWorldInterceptor_local_stub is a local stub implementation of [HelloWorldInterceptor]. type helloWorldInterceptor_local_stub struct { impl HelloWorldInterceptor name string @@ -165,6 +167,7 @@ func (s helloWorldInterceptor_local_stub) SayHello(ctx context.Context) { _ = s.interceptor(ctx, info, []any{}, []any{}, call) } +// helloWorldLazy_local_stub is a local stub implementation of [HelloWorldLazy]. type helloWorldLazy_local_stub struct { impl HelloWorldLazy name string @@ -196,6 +199,7 @@ func (s helloWorldLazy_local_stub) SayHello(ctx context.Context) { _ = s.interceptor(ctx, info, []any{}, []any{}, call) } +// main_local_stub is a local stub implementation of [kod.Main]. type main_local_stub struct { impl kod.Main name string diff --git a/examples/helloworld/kod_gen_interface.go b/examples/helloworld/kod_gen_interface.go index e8fcb57..6648e28 100644 --- a/examples/helloworld/kod_gen_interface.go +++ b/examples/helloworld/kod_gen_interface.go @@ -6,17 +6,17 @@ import ( "context" ) -// helloWorld is a component that implements HelloWorld. +// HelloWorld is a component interface implemented by [helloWorld]. type HelloWorld interface { SayHello(ctx context.Context) } -// lazyHelloWorld is a component that implements HelloWorldLazy. +// HelloWorldLazy is a component interface implemented by [lazyHelloWorld]. type HelloWorldLazy interface { SayHello(ctx context.Context) } -// helloWorldInterceptor is a component that implements HelloWorldInterceptor. +// HelloWorldInterceptor is a component interface implemented by [helloWorldInterceptor]. type HelloWorldInterceptor interface { SayHello(ctx context.Context) } diff --git a/tests/case1/kod_gen.go b/tests/case1/kod_gen.go index 4bf04c0..ca60813 100644 --- a/tests/case1/kod_gen.go +++ b/tests/case1/kod_gen.go @@ -335,6 +335,7 @@ var _ kod.InstanceOf[testService] = (*serviceImpl)(nil) // Local stub implementations. +// hTTPController_local_stub is a local stub implementation of [HTTPController]. type hTTPController_local_stub struct { impl HTTPController name string @@ -350,6 +351,7 @@ func (s hTTPController_local_stub) Foo(a0 http.ResponseWriter, a1 *http.Request) return } +// interceptorRetry_local_stub is a local stub implementation of [InterceptorRetry]. type interceptorRetry_local_stub struct { impl InterceptorRetry name string @@ -405,6 +407,7 @@ func (s interceptorRetry_local_stub) TestNormal(ctx context.Context) (err error) return } +// lazyInitComponent_local_stub is a local stub implementation of [LazyInitComponent]. type lazyInitComponent_local_stub struct { impl LazyInitComponent name string @@ -437,6 +440,7 @@ func (s lazyInitComponent_local_stub) Try(ctx context.Context) (err error) { return } +// lazyInitImpl_local_stub is a local stub implementation of [LazyInitImpl]. type lazyInitImpl_local_stub struct { impl LazyInitImpl name string @@ -468,6 +472,7 @@ func (s lazyInitImpl_local_stub) Try(ctx context.Context) { _ = s.interceptor(ctx, info, []any{}, []any{}, call) } +// main_local_stub is a local stub implementation of [kod.Main]. type main_local_stub struct { impl kod.Main name string @@ -477,6 +482,7 @@ type main_local_stub struct { // Check that main_local_stub implements the kod.Main interface. var _ kod.Main = (*main_local_stub)(nil) +// test1Component_local_stub is a local stub implementation of [Test1Component]. type test1Component_local_stub struct { impl Test1Component name string @@ -510,6 +516,7 @@ func (s test1Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (r0 *Foo return } +// test2Component_local_stub is a local stub implementation of [Test2Component]. type test2Component_local_stub struct { impl Test2Component name string @@ -525,6 +532,7 @@ func (s test2Component_local_stub) GetClient() (r0 *http.Client) { return } +// ctxInterface_local_stub is a local stub implementation of [ctxInterface]. type ctxInterface_local_stub struct { impl ctxInterface name string @@ -556,6 +564,7 @@ func (s ctxInterface_local_stub) Foo(ctx context.Context) { _ = s.interceptor(ctx, info, []any{}, []any{}, call) } +// panicCaseInterface_local_stub is a local stub implementation of [panicCaseInterface]. type panicCaseInterface_local_stub struct { impl panicCaseInterface name string @@ -587,6 +596,7 @@ func (s panicCaseInterface_local_stub) TestPanic(ctx context.Context) { _ = s.interceptor(ctx, info, []any{}, []any{}, call) } +// panicNoRecvoeryCaseInterface_local_stub is a local stub implementation of [panicNoRecvoeryCaseInterface]. type panicNoRecvoeryCaseInterface_local_stub struct { impl panicNoRecvoeryCaseInterface name string @@ -618,6 +628,7 @@ func (s panicNoRecvoeryCaseInterface_local_stub) TestPanic(ctx context.Context) _ = s.interceptor(ctx, info, []any{}, []any{}, call) } +// test1Controller_local_stub is a local stub implementation of [test1Controller]. type test1Controller_local_stub struct { impl test1Controller name string @@ -627,6 +638,7 @@ type test1Controller_local_stub struct { // Check that test1Controller_local_stub implements the test1Controller interface. var _ test1Controller = (*test1Controller_local_stub)(nil) +// testEchoController_local_stub is a local stub implementation of [testEchoController]. type testEchoController_local_stub struct { impl testEchoController name string @@ -648,6 +660,7 @@ func (s testEchoController_local_stub) Hello(a0 echo.Context) (err error) { return } +// testGinController_local_stub is a local stub implementation of [testGinController]. type testGinController_local_stub struct { impl testGinController name string @@ -663,6 +676,7 @@ func (s testGinController_local_stub) Hello(a0 *gin.Context) { return } +// testRepository_local_stub is a local stub implementation of [testRepository]. type testRepository_local_stub struct { impl testRepository name string @@ -695,6 +709,7 @@ func (s testRepository_local_stub) Foo(ctx context.Context) (err error) { return } +// testService_local_stub is a local stub implementation of [testService]. type testService_local_stub struct { impl testService name string diff --git a/tests/case1/kod_gen_interface.go b/tests/case1/kod_gen_interface.go index e9e4587..aa89faf 100644 --- a/tests/case1/kod_gen_interface.go +++ b/tests/case1/kod_gen_interface.go @@ -10,36 +10,37 @@ import ( "github.com/labstack/echo/v4" ) +// test1Controller is a component interface implemented by [test1ControllerImpl]. type test1Controller interface { } -// serviceImpl is a component that implements testService. +// testService is a component interface implemented by [serviceImpl]. type testService interface { Foo(ctx context.Context) error } -// modelImpl is a component that implements testRepository. +// testRepository is a component interface implemented by [modelImpl]. type testRepository interface { Foo(ctx context.Context) error } -// test1Component is a component that implements Test1Component. +// Test1Component is a component interface implemented by [test1Component]. type Test1Component interface { Foo(ctx context.Context, req *FooReq) (*FooRes, error) } -// test2Component is a component that implements Test2Component. +// Test2Component is a component interface implemented by [test2Component]. type Test2Component interface { GetClient() *http.Client } -// ctxImpl is a component that implements ctxInterface. +// ctxInterface is a component interface implemented by [ctxImpl]. type ctxInterface interface { // Foo is a http handler Foo(ctx context.Context) } -// testEchoControllerImpl is a component that implements testEchoController. +// testEchoController is a component interface implemented by [testEchoControllerImpl]. type testEchoController interface { // Hello is a method of testEchoControllerImpl Hello(c echo.Context) error @@ -47,40 +48,40 @@ type testEchoController interface { Error(c echo.Context) error } -// testGinControllerImpl is a component that implements testGinController. +// testGinController is a component interface implemented by [testGinControllerImpl]. type testGinController interface { // Hello is a method of testGinControllerImpl Hello(c *gin.Context) } -// httpControllerImpl is a component that implements HTTPController. +// HTTPController is a component interface implemented by [httpControllerImpl]. type HTTPController interface { // Foo is a http handler Foo(w http.ResponseWriter, r *http.Request) } -// interceptorRetry is a component that implements InterceptorRetry. +// InterceptorRetry is a component interface implemented by [interceptorRetry]. type InterceptorRetry interface { TestError(ctx context.Context) error TestNormal(ctx context.Context) error } -// lazyInitImpl is a component that implements LazyInitImpl. +// LazyInitImpl is a component interface implemented by [lazyInitImpl]. type LazyInitImpl interface { Try(ctx context.Context) } -// lazyInitComponent is a component that implements LazyInitComponent. +// LazyInitComponent is a component interface implemented by [lazyInitComponent]. type LazyInitComponent interface { Try(ctx context.Context) error } -// panicCase is a component that implements panicCaseInterface. +// panicCaseInterface is a component interface implemented by [panicCase]. type panicCaseInterface interface { TestPanic(ctx context.Context) } -// panicNoRecvoeryCase is a component that implements panicNoRecvoeryCaseInterface. +// panicNoRecvoeryCaseInterface is a component interface implemented by [panicNoRecvoeryCase]. type panicNoRecvoeryCaseInterface interface { TestPanic(ctx context.Context) } diff --git a/tests/case2/kod_gen.go b/tests/case2/kod_gen.go index 055e6de..11a2906 100644 --- a/tests/case2/kod_gen.go +++ b/tests/case2/kod_gen.go @@ -80,6 +80,7 @@ var _ kod.InstanceOf[Test2Component] = (*test2Component)(nil) // Local stub implementations. +// main_local_stub is a local stub implementation of [kod.Main]. type main_local_stub struct { impl kod.Main name string @@ -89,6 +90,7 @@ type main_local_stub struct { // Check that main_local_stub implements the kod.Main interface. var _ kod.Main = (*main_local_stub)(nil) +// test1Component_local_stub is a local stub implementation of [Test1Component]. type test1Component_local_stub struct { impl Test1Component name string @@ -121,6 +123,7 @@ func (s test1Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err err return } +// test2Component_local_stub is a local stub implementation of [Test2Component]. type test2Component_local_stub struct { impl Test2Component name string diff --git a/tests/case2/kod_gen_interface.go b/tests/case2/kod_gen_interface.go index 91266a1..0c3b4b5 100644 --- a/tests/case2/kod_gen_interface.go +++ b/tests/case2/kod_gen_interface.go @@ -6,12 +6,12 @@ import ( "context" ) -// test1Component is a component that implements Test1Component. +// Test1Component is a component interface implemented by [test1Component]. type Test1Component interface { Foo(ctx context.Context, req *FooReq) error } -// test2Component is a component that implements Test2Component. +// Test2Component is a component interface implemented by [test2Component]. type Test2Component interface { Foo(ctx context.Context, req *FooReq) error } diff --git a/tests/case3/kod_gen.go b/tests/case3/kod_gen.go index 039a233..4bc4d89 100644 --- a/tests/case3/kod_gen.go +++ b/tests/case3/kod_gen.go @@ -102,6 +102,7 @@ var _ kod.InstanceOf[Test3Component] = (*test3Component)(nil) // Local stub implementations. +// main_local_stub is a local stub implementation of [kod.Main]. type main_local_stub struct { impl kod.Main name string @@ -111,6 +112,7 @@ type main_local_stub struct { // Check that main_local_stub implements the kod.Main interface. var _ kod.Main = (*main_local_stub)(nil) +// test1Component_local_stub is a local stub implementation of [Test1Component]. type test1Component_local_stub struct { impl Test1Component name string @@ -143,6 +145,7 @@ func (s test1Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err err return } +// test2Component_local_stub is a local stub implementation of [Test2Component]. type test2Component_local_stub struct { impl Test2Component name string @@ -175,6 +178,7 @@ func (s test2Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err err return } +// test3Component_local_stub is a local stub implementation of [Test3Component]. type test3Component_local_stub struct { impl Test3Component name string diff --git a/tests/case3/kod_gen_interface.go b/tests/case3/kod_gen_interface.go index 1351906..19ebb3d 100644 --- a/tests/case3/kod_gen_interface.go +++ b/tests/case3/kod_gen_interface.go @@ -6,17 +6,17 @@ import ( "context" ) -// test1Component is a component that implements Test1Component. +// Test1Component is a component interface implemented by [test1Component]. type Test1Component interface { Foo(ctx context.Context, req *FooReq) error } -// test2Component is a component that implements Test2Component. +// Test2Component is a component interface implemented by [test2Component]. type Test2Component interface { Foo(ctx context.Context, req *FooReq) error } -// test3Component is a component that implements Test3Component. +// Test3Component is a component interface implemented by [test3Component]. type Test3Component interface { Foo(ctx context.Context, req *FooReq) error } diff --git a/tests/case4/kod_gen.go b/tests/case4/kod_gen.go index 902576d..38a2aff 100644 --- a/tests/case4/kod_gen.go +++ b/tests/case4/kod_gen.go @@ -102,6 +102,7 @@ var _ kod.InstanceOf[Test3Component] = (*test3Component)(nil) // Local stub implementations. +// main_local_stub is a local stub implementation of [kod.Main]. type main_local_stub struct { impl kod.Main name string @@ -111,6 +112,7 @@ type main_local_stub struct { // Check that main_local_stub implements the kod.Main interface. var _ kod.Main = (*main_local_stub)(nil) +// test1Component_local_stub is a local stub implementation of [Test1Component]. type test1Component_local_stub struct { impl Test1Component name string @@ -143,6 +145,7 @@ func (s test1Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err err return } +// test2Component_local_stub is a local stub implementation of [Test2Component]. type test2Component_local_stub struct { impl Test2Component name string @@ -175,6 +178,7 @@ func (s test2Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err err return } +// test3Component_local_stub is a local stub implementation of [Test3Component]. type test3Component_local_stub struct { impl Test3Component name string diff --git a/tests/case4/kod_gen_interface.go b/tests/case4/kod_gen_interface.go index 25735c0..2e5d7b0 100644 --- a/tests/case4/kod_gen_interface.go +++ b/tests/case4/kod_gen_interface.go @@ -6,17 +6,17 @@ import ( "context" ) -// test1Component is a component that implements Test1Component. +// Test1Component is a component interface implemented by [test1Component]. type Test1Component interface { Foo(ctx context.Context, req *FooReq) error } -// test2Component is a component that implements Test2Component. +// Test2Component is a component interface implemented by [test2Component]. type Test2Component interface { Foo(ctx context.Context, req *FooReq) error } -// test3Component is a component that implements Test3Component. +// Test3Component is a component interface implemented by [test3Component]. type Test3Component interface { Foo(ctx context.Context, req *FooReq) error } diff --git a/tests/case5/kod_gen.go b/tests/case5/kod_gen.go index e12dcb0..88c73e0 100644 --- a/tests/case5/kod_gen.go +++ b/tests/case5/kod_gen.go @@ -59,6 +59,7 @@ var _ kod.InstanceOf[TestRefStruct1] = (*testRefStruct1)(nil) // Local stub implementations. +// main_local_stub is a local stub implementation of [kod.Main]. type main_local_stub struct { impl kod.Main name string @@ -68,6 +69,7 @@ type main_local_stub struct { // Check that main_local_stub implements the kod.Main interface. var _ kod.Main = (*main_local_stub)(nil) +// testRefStruct1_local_stub is a local stub implementation of [TestRefStruct1]. type testRefStruct1_local_stub struct { impl TestRefStruct1 name string diff --git a/tests/case5/kod_gen_interface.go b/tests/case5/kod_gen_interface.go index 69ef3a2..5cedf32 100644 --- a/tests/case5/kod_gen_interface.go +++ b/tests/case5/kod_gen_interface.go @@ -2,5 +2,6 @@ package case5 +// TestRefStruct1 is a component interface implemented by [testRefStruct1]. type TestRefStruct1 interface { } diff --git a/tests/graphcase/kod_gen.go b/tests/graphcase/kod_gen.go index b013ac6..e3b00af 100644 --- a/tests/graphcase/kod_gen.go +++ b/tests/graphcase/kod_gen.go @@ -147,6 +147,7 @@ var _ kod.InstanceOf[testService] = (*serviceImpl)(nil) // Local stub implementations. +// hTTPController_local_stub is a local stub implementation of [HTTPController]. type hTTPController_local_stub struct { impl HTTPController name string @@ -162,6 +163,7 @@ func (s hTTPController_local_stub) Foo(a0 http.ResponseWriter, a1 http.Request) return } +// main_local_stub is a local stub implementation of [kod.Main]. type main_local_stub struct { impl kod.Main name string @@ -171,6 +173,7 @@ type main_local_stub struct { // Check that main_local_stub implements the kod.Main interface. var _ kod.Main = (*main_local_stub)(nil) +// test1Component_local_stub is a local stub implementation of [Test1Component]. type test1Component_local_stub struct { impl Test1Component name string @@ -203,6 +206,7 @@ func (s test1Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err err return } +// test1Controller_local_stub is a local stub implementation of [test1Controller]. type test1Controller_local_stub struct { impl test1Controller name string @@ -218,6 +222,7 @@ func (s test1Controller_local_stub) Foo(a0 *gin.Context) { return } +// testModel_local_stub is a local stub implementation of [testModel]. type testModel_local_stub struct { impl testModel name string @@ -250,6 +255,7 @@ func (s testModel_local_stub) Foo(ctx context.Context) (err error) { return } +// testService_local_stub is a local stub implementation of [testService]. type testService_local_stub struct { impl testService name string diff --git a/tests/graphcase/kod_gen_interface.go b/tests/graphcase/kod_gen_interface.go index 8434324..87f0fbb 100644 --- a/tests/graphcase/kod_gen_interface.go +++ b/tests/graphcase/kod_gen_interface.go @@ -9,27 +9,27 @@ import ( "github.com/gin-gonic/gin" ) -// test1ControllerImpl is a component that implements test1Controller. +// test1Controller is a component interface implemented by [test1ControllerImpl]. type test1Controller interface { Foo(cccccc *gin.Context) } -// httpControllerImpl is a component that implements HTTPController. +// HTTPController is a component interface implemented by [httpControllerImpl]. type HTTPController interface { Foo(w http.ResponseWriter, r http.Request) } -// serviceImpl is a component that implements testService. +// testService is a component interface implemented by [serviceImpl]. type testService interface { Foo(ctx context.Context) error } -// modelImpl is a component that implements testModel. +// testModel is a component interface implemented by [modelImpl]. type testModel interface { Foo(ctx context.Context) error } -// test1Component is a component that implements Test1Component. +// Test1Component is a component interface implemented by [test1Component]. type Test1Component interface { Foo(ctx context.Context, req *FooReq) error }