From 44fdda751b2119a4118d8875c7ddb686e0198dc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E5=8F=AF?= Date: Tue, 22 Oct 2024 15:46:16 +0800 Subject: [PATCH] chore: improve comments for generated code --- cmd/kod/internal/generate_generator.go | 3 +- examples/helloworld/kod_gen.go | 11 ++++--- tests/case1/kod_gen.go | 45 +++++++++++++++++--------- tests/case2/kod_gen.go | 8 +++-- tests/case3/kod_gen.go | 11 ++++--- tests/case4/kod_gen.go | 11 ++++--- tests/case5/kod_gen.go | 4 +-- tests/graphcase/kod_gen.go | 17 ++++++---- 8 files changed, 71 insertions(+), 39 deletions(-) diff --git a/cmd/kod/internal/generate_generator.go b/cmd/kod/internal/generate_generator.go index 4d098f5..1e86621 100644 --- a/cmd/kod/internal/generate_generator.go +++ b/cmd/kod/internal/generate_generator.go @@ -774,7 +774,7 @@ func (g *generator) generateLocalStubs(p printFn) { p(`}`) p(``) - p(`// Check that %s implements the %s interface.`, stub, g.tset.genTypeString(comp.intf)) + p(`// Check that [%s] implements the [%s] interface.`, stub, g.tset.genTypeString(comp.intf)) p(`var _ %s = (*%s)(nil)`, g.tset.genTypeString(comp.intf), stub) p(``) @@ -788,6 +788,7 @@ func (g *generator) generateLocalStubs(p printFn) { } p(``) + p(`// %s wraps the method [%s.%s].`, m.Name(), comp.implName(), m.Name()) p(`func (s %s) %s(%s) (%s) {`, stub, m.Name(), g.args(mt), g.returns(mt)) // If the first argument is not context.Context, then we don't support interceptors. diff --git a/examples/helloworld/kod_gen.go b/examples/helloworld/kod_gen.go index 8be6ddc..faf427c 100644 --- a/examples/helloworld/kod_gen.go +++ b/examples/helloworld/kod_gen.go @@ -92,7 +92,7 @@ type main_local_stub struct { interceptor interceptor.Interceptor } -// Check that main_local_stub implements the kod.Main interface. +// Check that [main_local_stub] implements the [kod.Main] interface. var _ kod.Main = (*main_local_stub)(nil) // helloWorld_local_stub is a local stub implementation of [HelloWorld]. @@ -102,9 +102,10 @@ type helloWorld_local_stub struct { interceptor interceptor.Interceptor } -// Check that helloWorld_local_stub implements the HelloWorld interface. +// Check that [helloWorld_local_stub] implements the [HelloWorld] interface. var _ HelloWorld = (*helloWorld_local_stub)(nil) +// SayHello wraps the method [helloWorld.SayHello]. func (s helloWorld_local_stub) SayHello(ctx context.Context) { if s.interceptor == nil { @@ -133,9 +134,10 @@ type helloWorldLazy_local_stub struct { interceptor interceptor.Interceptor } -// Check that helloWorldLazy_local_stub implements the HelloWorldLazy interface. +// Check that [helloWorldLazy_local_stub] implements the [HelloWorldLazy] interface. var _ HelloWorldLazy = (*helloWorldLazy_local_stub)(nil) +// SayHello wraps the method [lazyHelloWorld.SayHello]. func (s helloWorldLazy_local_stub) SayHello(ctx context.Context) { if s.interceptor == nil { @@ -164,9 +166,10 @@ type helloWorldInterceptor_local_stub struct { interceptor interceptor.Interceptor } -// Check that helloWorldInterceptor_local_stub implements the HelloWorldInterceptor interface. +// Check that [helloWorldInterceptor_local_stub] implements the [HelloWorldInterceptor] interface. var _ HelloWorldInterceptor = (*helloWorldInterceptor_local_stub)(nil) +// SayHello wraps the method [helloWorldInterceptor.SayHello]. func (s helloWorldInterceptor_local_stub) SayHello(ctx context.Context) { if s.interceptor == nil { diff --git a/tests/case1/kod_gen.go b/tests/case1/kod_gen.go index 2bc1c9b..90ea2ee 100644 --- a/tests/case1/kod_gen.go +++ b/tests/case1/kod_gen.go @@ -271,7 +271,7 @@ type test1Controller_local_stub struct { interceptor interceptor.Interceptor } -// Check that test1Controller_local_stub implements the test1Controller interface. +// Check that [test1Controller_local_stub] implements the [test1Controller] interface. var _ test1Controller = (*test1Controller_local_stub)(nil) // testService_local_stub is a local stub implementation of [testService]. @@ -281,9 +281,10 @@ type testService_local_stub struct { interceptor interceptor.Interceptor } -// Check that testService_local_stub implements the testService interface. +// Check that [testService_local_stub] implements the [testService] interface. var _ testService = (*testService_local_stub)(nil) +// Foo wraps the method [serviceImpl.Foo]. func (s testService_local_stub) Foo(ctx context.Context) (err error) { if s.interceptor == nil { @@ -313,9 +314,10 @@ type testRepository_local_stub struct { interceptor interceptor.Interceptor } -// Check that testRepository_local_stub implements the testRepository interface. +// Check that [testRepository_local_stub] implements the [testRepository] interface. var _ testRepository = (*testRepository_local_stub)(nil) +// Foo wraps the method [modelImpl.Foo]. func (s testRepository_local_stub) Foo(ctx context.Context) (err error) { if s.interceptor == nil { @@ -345,9 +347,10 @@ type test1Component_local_stub struct { interceptor interceptor.Interceptor } -// Check that test1Component_local_stub implements the Test1Component interface. +// Check that [test1Component_local_stub] implements the [Test1Component] interface. var _ Test1Component = (*test1Component_local_stub)(nil) +// Foo wraps the method [test1Component.Foo]. func (s test1Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (r0 *FooRes, err error) { if s.interceptor == nil { @@ -378,9 +381,10 @@ type test2Component_local_stub struct { interceptor interceptor.Interceptor } -// Check that test2Component_local_stub implements the Test2Component interface. +// Check that [test2Component_local_stub] implements the [Test2Component] interface. var _ Test2Component = (*test2Component_local_stub)(nil) +// GetClient wraps the method [test2Component.GetClient]. func (s test2Component_local_stub) GetClient() (r0 *http.Client) { // Because the first argument is not context.Context, so interceptors are not supported. r0 = s.impl.GetClient() @@ -394,7 +398,7 @@ type main_local_stub struct { interceptor interceptor.Interceptor } -// Check that main_local_stub implements the kod.Main interface. +// Check that [main_local_stub] implements the [kod.Main] interface. var _ kod.Main = (*main_local_stub)(nil) // ctxInterface_local_stub is a local stub implementation of [ctxInterface]. @@ -404,9 +408,10 @@ type ctxInterface_local_stub struct { interceptor interceptor.Interceptor } -// Check that ctxInterface_local_stub implements the ctxInterface interface. +// Check that [ctxInterface_local_stub] implements the [ctxInterface] interface. var _ ctxInterface = (*ctxInterface_local_stub)(nil) +// Foo wraps the method [ctxImpl.Foo]. func (s ctxInterface_local_stub) Foo(ctx context.Context) { if s.interceptor == nil { @@ -435,15 +440,17 @@ type testEchoController_local_stub struct { interceptor interceptor.Interceptor } -// Check that testEchoController_local_stub implements the testEchoController interface. +// Check that [testEchoController_local_stub] implements the [testEchoController] interface. var _ testEchoController = (*testEchoController_local_stub)(nil) +// Error wraps the method [testEchoControllerImpl.Error]. func (s testEchoController_local_stub) Error(a0 echo.Context) (err error) { // Because the first argument is not context.Context, so interceptors are not supported. err = s.impl.Error(a0) return } +// Hello wraps the method [testEchoControllerImpl.Hello]. func (s testEchoController_local_stub) Hello(a0 echo.Context) (err error) { // Because the first argument is not context.Context, so interceptors are not supported. err = s.impl.Hello(a0) @@ -457,9 +464,10 @@ type testGinController_local_stub struct { interceptor interceptor.Interceptor } -// Check that testGinController_local_stub implements the testGinController interface. +// Check that [testGinController_local_stub] implements the [testGinController] interface. var _ testGinController = (*testGinController_local_stub)(nil) +// Hello wraps the method [testGinControllerImpl.Hello]. func (s testGinController_local_stub) Hello(a0 *gin.Context) { // Because the first argument is not context.Context, so interceptors are not supported. s.impl.Hello(a0) @@ -473,9 +481,10 @@ type hTTPController_local_stub struct { interceptor interceptor.Interceptor } -// Check that hTTPController_local_stub implements the HTTPController interface. +// Check that [hTTPController_local_stub] implements the [HTTPController] interface. var _ HTTPController = (*hTTPController_local_stub)(nil) +// Foo wraps the method [httpControllerImpl.Foo]. func (s hTTPController_local_stub) Foo(a0 http.ResponseWriter, a1 *http.Request) { // Because the first argument is not context.Context, so interceptors are not supported. s.impl.Foo(a0, a1) @@ -489,9 +498,10 @@ type interceptorRetry_local_stub struct { interceptor interceptor.Interceptor } -// Check that interceptorRetry_local_stub implements the InterceptorRetry interface. +// Check that [interceptorRetry_local_stub] implements the [InterceptorRetry] interface. var _ InterceptorRetry = (*interceptorRetry_local_stub)(nil) +// TestError wraps the method [interceptorRetry.TestError]. func (s interceptorRetry_local_stub) TestError(ctx context.Context) (err error) { if s.interceptor == nil { @@ -514,6 +524,7 @@ func (s interceptorRetry_local_stub) TestError(ctx context.Context) (err error) return } +// TestNormal wraps the method [interceptorRetry.TestNormal]. func (s interceptorRetry_local_stub) TestNormal(ctx context.Context) (err error) { if s.interceptor == nil { @@ -543,9 +554,10 @@ type lazyInitImpl_local_stub struct { interceptor interceptor.Interceptor } -// Check that lazyInitImpl_local_stub implements the LazyInitImpl interface. +// Check that [lazyInitImpl_local_stub] implements the [LazyInitImpl] interface. var _ LazyInitImpl = (*lazyInitImpl_local_stub)(nil) +// Try wraps the method [lazyInitImpl.Try]. func (s lazyInitImpl_local_stub) Try(ctx context.Context) { if s.interceptor == nil { @@ -574,9 +586,10 @@ type lazyInitComponent_local_stub struct { interceptor interceptor.Interceptor } -// Check that lazyInitComponent_local_stub implements the LazyInitComponent interface. +// Check that [lazyInitComponent_local_stub] implements the [LazyInitComponent] interface. var _ LazyInitComponent = (*lazyInitComponent_local_stub)(nil) +// Try wraps the method [lazyInitComponent.Try]. func (s lazyInitComponent_local_stub) Try(ctx context.Context) (err error) { if s.interceptor == nil { @@ -606,9 +619,10 @@ type panicCaseInterface_local_stub struct { interceptor interceptor.Interceptor } -// Check that panicCaseInterface_local_stub implements the panicCaseInterface interface. +// Check that [panicCaseInterface_local_stub] implements the [panicCaseInterface] interface. var _ panicCaseInterface = (*panicCaseInterface_local_stub)(nil) +// TestPanic wraps the method [panicCase.TestPanic]. func (s panicCaseInterface_local_stub) TestPanic(ctx context.Context) { if s.interceptor == nil { @@ -637,9 +651,10 @@ type panicNoRecvoeryCaseInterface_local_stub struct { interceptor interceptor.Interceptor } -// Check that panicNoRecvoeryCaseInterface_local_stub implements the panicNoRecvoeryCaseInterface interface. +// Check that [panicNoRecvoeryCaseInterface_local_stub] implements the [panicNoRecvoeryCaseInterface] interface. var _ panicNoRecvoeryCaseInterface = (*panicNoRecvoeryCaseInterface_local_stub)(nil) +// TestPanic wraps the method [panicNoRecvoeryCase.TestPanic]. func (s panicNoRecvoeryCaseInterface_local_stub) TestPanic(ctx context.Context) { if s.interceptor == nil { diff --git a/tests/case2/kod_gen.go b/tests/case2/kod_gen.go index 647b24b..5caa00c 100644 --- a/tests/case2/kod_gen.go +++ b/tests/case2/kod_gen.go @@ -74,9 +74,10 @@ type test1Component_local_stub struct { interceptor interceptor.Interceptor } -// Check that test1Component_local_stub implements the Test1Component interface. +// Check that [test1Component_local_stub] implements the [Test1Component] interface. var _ Test1Component = (*test1Component_local_stub)(nil) +// Foo wraps the method [test1Component.Foo]. func (s test1Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err error) { if s.interceptor == nil { @@ -106,9 +107,10 @@ type test2Component_local_stub struct { interceptor interceptor.Interceptor } -// Check that test2Component_local_stub implements the Test2Component interface. +// Check that [test2Component_local_stub] implements the [Test2Component] interface. var _ Test2Component = (*test2Component_local_stub)(nil) +// Foo wraps the method [test2Component.Foo]. func (s test2Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err error) { if s.interceptor == nil { @@ -138,6 +140,6 @@ type main_local_stub struct { interceptor interceptor.Interceptor } -// Check that main_local_stub implements the kod.Main interface. +// Check that [main_local_stub] implements the [kod.Main] interface. var _ kod.Main = (*main_local_stub)(nil) diff --git a/tests/case3/kod_gen.go b/tests/case3/kod_gen.go index 908a85e..7453017 100644 --- a/tests/case3/kod_gen.go +++ b/tests/case3/kod_gen.go @@ -91,9 +91,10 @@ type test1Component_local_stub struct { interceptor interceptor.Interceptor } -// Check that test1Component_local_stub implements the Test1Component interface. +// Check that [test1Component_local_stub] implements the [Test1Component] interface. var _ Test1Component = (*test1Component_local_stub)(nil) +// Foo wraps the method [test1Component.Foo]. func (s test1Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err error) { if s.interceptor == nil { @@ -123,9 +124,10 @@ type test2Component_local_stub struct { interceptor interceptor.Interceptor } -// Check that test2Component_local_stub implements the Test2Component interface. +// Check that [test2Component_local_stub] implements the [Test2Component] interface. var _ Test2Component = (*test2Component_local_stub)(nil) +// Foo wraps the method [test2Component.Foo]. func (s test2Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err error) { if s.interceptor == nil { @@ -155,9 +157,10 @@ type test3Component_local_stub struct { interceptor interceptor.Interceptor } -// Check that test3Component_local_stub implements the Test3Component interface. +// Check that [test3Component_local_stub] implements the [Test3Component] interface. var _ Test3Component = (*test3Component_local_stub)(nil) +// Foo wraps the method [test3Component.Foo]. func (s test3Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err error) { if s.interceptor == nil { @@ -187,6 +190,6 @@ type main_local_stub struct { interceptor interceptor.Interceptor } -// Check that main_local_stub implements the kod.Main interface. +// Check that [main_local_stub] implements the [kod.Main] interface. var _ kod.Main = (*main_local_stub)(nil) diff --git a/tests/case4/kod_gen.go b/tests/case4/kod_gen.go index 2b1c0f0..f81bc68 100644 --- a/tests/case4/kod_gen.go +++ b/tests/case4/kod_gen.go @@ -91,9 +91,10 @@ type test1Component_local_stub struct { interceptor interceptor.Interceptor } -// Check that test1Component_local_stub implements the Test1Component interface. +// Check that [test1Component_local_stub] implements the [Test1Component] interface. var _ Test1Component = (*test1Component_local_stub)(nil) +// Foo wraps the method [test1Component.Foo]. func (s test1Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err error) { if s.interceptor == nil { @@ -123,9 +124,10 @@ type test2Component_local_stub struct { interceptor interceptor.Interceptor } -// Check that test2Component_local_stub implements the Test2Component interface. +// Check that [test2Component_local_stub] implements the [Test2Component] interface. var _ Test2Component = (*test2Component_local_stub)(nil) +// Foo wraps the method [test2Component.Foo]. func (s test2Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err error) { if s.interceptor == nil { @@ -155,9 +157,10 @@ type test3Component_local_stub struct { interceptor interceptor.Interceptor } -// Check that test3Component_local_stub implements the Test3Component interface. +// Check that [test3Component_local_stub] implements the [Test3Component] interface. var _ Test3Component = (*test3Component_local_stub)(nil) +// Foo wraps the method [test3Component.Foo]. func (s test3Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err error) { if s.interceptor == nil { @@ -187,6 +190,6 @@ type main_local_stub struct { interceptor interceptor.Interceptor } -// Check that main_local_stub implements the kod.Main interface. +// Check that [main_local_stub] implements the [kod.Main] interface. var _ kod.Main = (*main_local_stub)(nil) diff --git a/tests/case5/kod_gen.go b/tests/case5/kod_gen.go index 8443f0a..7a8deae 100644 --- a/tests/case5/kod_gen.go +++ b/tests/case5/kod_gen.go @@ -55,7 +55,7 @@ type main_local_stub struct { interceptor interceptor.Interceptor } -// Check that main_local_stub implements the kod.Main interface. +// 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]. @@ -65,6 +65,6 @@ type testRefStruct1_local_stub struct { interceptor interceptor.Interceptor } -// Check that testRefStruct1_local_stub implements the TestRefStruct1 interface. +// Check that [testRefStruct1_local_stub] implements the [TestRefStruct1] interface. var _ TestRefStruct1 = (*testRefStruct1_local_stub)(nil) diff --git a/tests/graphcase/kod_gen.go b/tests/graphcase/kod_gen.go index 22dc50e..16725f2 100644 --- a/tests/graphcase/kod_gen.go +++ b/tests/graphcase/kod_gen.go @@ -126,9 +126,10 @@ type test1Controller_local_stub struct { interceptor interceptor.Interceptor } -// Check that test1Controller_local_stub implements the test1Controller interface. +// Check that [test1Controller_local_stub] implements the [test1Controller] interface. var _ test1Controller = (*test1Controller_local_stub)(nil) +// Foo wraps the method [test1ControllerImpl.Foo]. func (s test1Controller_local_stub) Foo(a0 *gin.Context) { // Because the first argument is not context.Context, so interceptors are not supported. s.impl.Foo(a0) @@ -142,9 +143,10 @@ type hTTPController_local_stub struct { interceptor interceptor.Interceptor } -// Check that hTTPController_local_stub implements the HTTPController interface. +// Check that [hTTPController_local_stub] implements the [HTTPController] interface. var _ HTTPController = (*hTTPController_local_stub)(nil) +// Foo wraps the method [httpControllerImpl.Foo]. func (s hTTPController_local_stub) Foo(a0 http.ResponseWriter, a1 http.Request) { // Because the first argument is not context.Context, so interceptors are not supported. s.impl.Foo(a0, a1) @@ -158,9 +160,10 @@ type testService_local_stub struct { interceptor interceptor.Interceptor } -// Check that testService_local_stub implements the testService interface. +// Check that [testService_local_stub] implements the [testService] interface. var _ testService = (*testService_local_stub)(nil) +// Foo wraps the method [serviceImpl.Foo]. func (s testService_local_stub) Foo(ctx context.Context) (err error) { if s.interceptor == nil { @@ -190,9 +193,10 @@ type testModel_local_stub struct { interceptor interceptor.Interceptor } -// Check that testModel_local_stub implements the testModel interface. +// Check that [testModel_local_stub] implements the [testModel] interface. var _ testModel = (*testModel_local_stub)(nil) +// Foo wraps the method [modelImpl.Foo]. func (s testModel_local_stub) Foo(ctx context.Context) (err error) { if s.interceptor == nil { @@ -222,9 +226,10 @@ type test1Component_local_stub struct { interceptor interceptor.Interceptor } -// Check that test1Component_local_stub implements the Test1Component interface. +// Check that [test1Component_local_stub] implements the [Test1Component] interface. var _ Test1Component = (*test1Component_local_stub)(nil) +// Foo wraps the method [test1Component.Foo]. func (s test1Component_local_stub) Foo(ctx context.Context, a1 *FooReq) (err error) { if s.interceptor == nil { @@ -254,6 +259,6 @@ type main_local_stub struct { interceptor interceptor.Interceptor } -// Check that main_local_stub implements the kod.Main interface. +// Check that [main_local_stub] implements the [kod.Main] interface. var _ kod.Main = (*main_local_stub)(nil)