@@ -7,6 +7,7 @@ package generator
77import (
88 "cmp"
99 "fmt"
10+ "log"
1011 "maps"
1112 "path/filepath"
1213 "runtime/debug"
@@ -103,6 +104,7 @@ type Method struct {
103104 Method string
104105 FullName string
105106 RPCType RPCType
107+ Options protoreflect.ProtoMessage
106108}
107109
108110type message struct {
@@ -134,12 +136,14 @@ func (g *Generator) generate(gen *protogen.GeneratedFile, f *protogen.File) {
134136 }
135137 for _ , svc := range f .Services {
136138 for i , meth := range svc .Methods {
139+ log .Printf ("meth.Desc.Options(): %#v\n " , meth .Desc .Options ())
137140 fullname := string (meth .Desc .FullName ())
138141 idx := strings .LastIndex (fullname , "." )
139142 method := Method {
140143 idx : i ,
141144 Method : meth .GoName ,
142145 FullName : fullname [:idx ],
146+ Options : meth .Desc .Options (),
143147 }
144148
145149 // parse RPC type
@@ -174,13 +178,23 @@ func (g *Generator) generate(gen *protogen.GeneratedFile, f *protogen.File) {
174178 }
175179 p .P (`"""Generated connect code."""` )
176180 p .P ()
181+ p .P (`import abc` )
177182 p .P (`from enum import Enum` )
178183 p .P ()
179- p .P (`from connect.client import Client` )
180- p .P (`from connect.connect import StreamRequest, StreamResponse, UnaryRequest, UnaryResponse` )
181- p .P (`from connect.handler import ClientStreamHandler, Handler, ServerStreamHandler, UnaryHandler` )
182- p .P (`from connect.options import ClientOptions, ConnectOptions` )
184+ p .P (`from connect import (` )
185+ p .P (` Client,` )
186+ p .P (` ClientOptions,` )
187+ p .P (` ConnectOptions,` )
188+ p .P (` Handler,` )
189+ p .P (` HandlerContext,` )
190+ p .P (` IdempotencyLevel,` )
191+ p .P (` StreamRequest,` )
192+ p .P (` StreamResponse,` )
193+ p .P (` UnaryRequest,` )
194+ p .P (` UnaryResponse,` )
195+ p .P (`)` )
183196 p .P (`from connect.connection_pool import AsyncConnectionPool` )
197+ p .P (`from connect.handler import BidiStreamHandler, ClientStreamHandler, ServerStreamHandler, UnaryHandler` )
184198 p .P (`from google.protobuf.descriptor import MethodDescriptor, ServiceDescriptor` )
185199 p .P ()
186200
@@ -278,7 +292,7 @@ func (g *Generator) generate(gen *protogen.GeneratedFile, f *protogen.File) {
278292 reqRPCType = `StreamRequest`
279293 respRPCType = `StreamResponse`
280294 }
281- fmt .Fprintf (& sb , "%s[%s]) -> %s[%s]: ... " , reqRPCType , svc .input .method , respRPCType , svc .output .method )
295+ fmt .Fprintf (& sb , "%s[%s], context: HandlerContext ) -> %s[%s]:\n raise NotImplementedError() " , reqRPCType , svc .input .method , respRPCType , svc .output .method )
282296 p .P (sb .String ())
283297 if j <= len (p .services )- 2 {
284298 p .P ()
@@ -316,7 +330,13 @@ func (g *Generator) generate(gen *protogen.GeneratedFile, f *protogen.File) {
316330 p .P (call )
317331 p .P (` input=` , svc .input .method , `,` )
318332 p .P (` output=` , svc .output .method , `,` )
319- p .P (` options=options,` )
333+ if options := meth .Options ; options != nil {
334+ if options .(* descriptorpb.MethodOptions ).GetIdempotencyLevel () != descriptorpb .MethodOptions_IDEMPOTENCY_UNKNOWN {
335+ p .P (` options=ConnectOptions(idempotency_level=IdempotencyLevel.NO_SIDE_EFFECTS).merge(options),` )
336+ } else {
337+ p .P (` options=options,` )
338+ }
339+ }
320340 p .P (` ),` )
321341 }
322342 }
0 commit comments