Copilot commented on code in PR #3587:
URL: https://github.com/apache/dubbo-go/pull/3587#discussion_r3693749413
##########
tools/dubbogo-cli/generator/sample/api_tripe.go:
##########
@@ -19,257 +19,233 @@ package sample
const (
apiTripleFile = `
-// Code generated by protoc-gen-go-triple. DO NOT EDIT.
-// versions:
-// - protoc-gen-go-triple v1.0.8
-// - protoc v3.14.0
-// source: api/samples_api.proto
-
+// Code generated by protoc-gen-triple. DO NOT EDIT.
+//
+// Source: api/samples_api.proto
package api
import (
- context "context"
- fmt "fmt"
+ client "dubbo.apache.org/dubbo-go/v3/client"
+ common "dubbo.apache.org/dubbo-go/v3/common"
+ constant "dubbo.apache.org/dubbo-go/v3/common/constant"
+ triple_protocol
"dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+ server "dubbo.apache.org/dubbo-go/v3/server"
)
import (
- grpc_go "github.com/dubbogo/grpc-go"
- codes "github.com/dubbogo/grpc-go/codes"
- metadata "github.com/dubbogo/grpc-go/metadata"
- status "github.com/dubbogo/grpc-go/status"
-
- common "github.com/dubbogo/triple/pkg/common"
- constant "github.com/dubbogo/triple/pkg/common/constant"
- triple "github.com/dubbogo/triple/pkg/triple"
+ "context"
+ "reflect"
+ "net/http"
)
import (
- constant1 "dubbo.apache.org/dubbo-go/v3/common/constant"
- protocol "dubbo.apache.org/dubbo-go/v3/protocol"
- dubbo3 "dubbo.apache.org/dubbo-go/v3/protocol/dubbo3"
- invocation "dubbo.apache.org/dubbo-go/v3/protocol/invocation"
+ "dubbo.apache.org/dubbo-go/v3"
)
-// This is a compile-time assertion to ensure that this generated file
-// is compatible with the grpc package it is being compiled against.
-const _ = grpc_go.SupportPackageIsVersion7
+// This is a compile-time assertion to ensure that this generated file and the
Triple package
+// are compatible. If you get a compiler error that this constant is not
defined, this code was
+// generated with a version of Triple newer than the one compiled into your
binary. You can fix the
+// problem by either regenerating this code with an older version of Triple or
updating the Triple
+// version compiled into your binary.
+const _ = triple_protocol.IsAtLeastVersion0_1_0
+
+const (
+ // GreeterName is the fully-qualified name of the Greeter service.
+ GreeterName = "api.Greeter"
+)
-// GreeterClient is the client API for Greeter service.
+// These constants are the fully-qualified names of the RPCs defined in this
package. They're
+// exposed at runtime as procedure and as the final two segments of the HTTP
route.
//
-// For semantics around ctx use and closing/ending streaming RPCs, please
refer to
https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
-type GreeterClient interface {
- // Sends a greeting
- SayHello(ctx context.Context, in *HelloRequest, opts
...grpc_go.CallOption) (*User, common.ErrorWithAttachment)
- // Sends a greeting via stream
- SayHelloStream(ctx context.Context, opts ...grpc_go.CallOption)
(Greeter_SayHelloStreamClient, error)
-}
+// Note that these are different from the fully-qualified method names used by
+// google.golang.org/protobuf/reflect/protoreflect. To convert from these
constants to
+// reflection-formatted method names, remove the leading slash and convert the
remaining slash to a
+// period.
+const (
+ // GreeterSayHelloProcedure is the fully-qualified name of the
Greeter's SayHello RPC.
+ GreeterSayHelloProcedure = "/api.Greeter/SayHello"
+ // GreeterSayHelloStreamProcedure is the fully-qualified name of the
Greeter's SayHelloStream RPC.
+ GreeterSayHelloStreamProcedure = "/api.Greeter/SayHelloStream"
+)
-type greeterClient struct {
- cc *triple.TripleConn
-}
+var (
+ _ Greeter = (*GreeterImpl)(nil)
+
+ _ Greeter_SayHelloStreamClient = (*GreeterSayHelloStreamClient)(nil)
-type GreeterClientImpl struct {
- SayHello func(ctx context.Context, in *HelloRequest) (*User,
error)
- SayHelloStream func(ctx context.Context) (Greeter_SayHelloStreamClient,
error)
+ _ Greeter_SayHelloStreamServer = (*GreeterSayHelloStreamServer)(nil)
+)
+
+// Greeter is a client for the api.Greeter service.
+type Greeter interface {
+ SayHello(ctx context.Context, req *HelloRequest, opts
...client.CallOption) (*User, error)
+ SayHelloStream(ctx context.Context, opts ...client.CallOption)
(Greeter_SayHelloStreamClient, error)
}
-func (c *GreeterClientImpl) GetDubboStub(cc *triple.TripleConn) GreeterClient {
- return NewGreeterClient(cc)
+// NewGreeter constructs a client for the api.Greeter service.
+func NewGreeter(cli *client.Client, opts ...client.ReferenceOption) (Greeter,
error) {
+ conn, err := cli.DialWithInfo("api.Greeter", &Greeter_ClientInfo,
opts...)
+ if err != nil {
+ return nil, err
+ }
+ return &GreeterImpl{
+ conn: conn,
+ }, nil
}
-func (c *GreeterClientImpl) XXX_InterfaceName() string {
- return "api.Greeter"
+func SetConsumerGreeter(srv common.RPCService) {
+ dubbo.SetConsumerServiceWithInfo(srv, &Greeter_ClientInfo)
}
-func NewGreeterClient(cc *triple.TripleConn) GreeterClient {
- return &greeterClient{cc}
+// GreeterImpl implements Greeter.
+type GreeterImpl struct {
+ conn *client.Connection
}
-func (c *greeterClient) SayHello(ctx context.Context, in *HelloRequest, opts
...grpc_go.CallOption) (*User, common.ErrorWithAttachment) {
- out := new(User)
- interfaceKey := ctx.Value(constant.InterfaceKey).(string)
- return out, c.cc.Invoke(ctx, "/"+interfaceKey+"/SayHello", in, out)
+func (c *GreeterImpl) SayHello(ctx context.Context, req *HelloRequest, opts
...client.CallOption) (*User, error) {
+ resp := new(User)
+ if err := c.conn.CallUnary(ctx, []interface{}{req}, resp, "SayHello",
opts...); err != nil {
+ return nil, err
+ }
+ return resp, nil
}
-func (c *greeterClient) SayHelloStream(ctx context.Context, opts
...grpc_go.CallOption) (Greeter_SayHelloStreamClient, error) {
- interfaceKey := ctx.Value(constant.InterfaceKey).(string)
- stream, err := c.cc.NewStream(ctx, "/"+interfaceKey+"/SayHelloStream",
opts...)
+func (c *GreeterImpl) SayHelloStream(ctx context.Context, opts
...client.CallOption) (Greeter_SayHelloStreamClient, error) {
+ stream, err := c.conn.CallBidiStream(ctx, "SayHelloStream", opts...)
if err != nil {
return nil, err
}
- x := &greeterSayHelloStreamClient{stream}
- return x, nil
+ rawStream := stream.(*triple_protocol.BidiStreamForClient)
+ return &GreeterSayHelloStreamClient{rawStream}, nil
}
type Greeter_SayHelloStreamClient interface {
+ Spec() triple_protocol.Spec
+ Peer() triple_protocol.Peer
Send(*HelloRequest) error
+ RequestHeader() http.Header
+ CloseRequest() error
Recv() (*User, error)
- grpc_go.ClientStream
+ ResponseHeader() http.Header
+ ResponseTrailer() http.Header
+ CloseResponse() error
}
-type greeterSayHelloStreamClient struct {
- grpc_go.ClientStream
+type GreeterSayHelloStreamClient struct {
+ *triple_protocol.BidiStreamForClient
}
-func (x *greeterSayHelloStreamClient) Send(m *HelloRequest) error {
- return x.ClientStream.SendMsg(m)
+func (cli *GreeterSayHelloStreamClient) Send(msg *HelloRequest) error {
+ return cli.BidiStreamForClient.Send(msg)
}
-func (x *greeterSayHelloStreamClient) Recv() (*User, error) {
- m := new(User)
- if err := x.ClientStream.RecvMsg(m); err != nil {
+func (cli *GreeterSayHelloStreamClient) Recv() (*User, error) {
+ msg := new(User)
+ if err := cli.BidiStreamForClient.Receive(msg); err != nil {
return nil, err
}
- return m, nil
-}
-
-// GreeterServer is the server API for Greeter service.
-// All implementations must embed UnimplementedGreeterServer
-// for forward compatibility
-type GreeterServer interface {
- // Sends a greeting
- SayHello(context.Context, *HelloRequest) (*User, error)
- // Sends a greeting via stream
- SayHelloStream(Greeter_SayHelloStreamServer) error
- mustEmbedUnimplementedGreeterServer()
-}
-
-// UnimplementedGreeterServer must be embedded to have forward compatible
implementations.
-type UnimplementedGreeterServer struct {
- proxyImpl protocol.Invoker
-}
-
-func (UnimplementedGreeterServer) SayHello(context.Context, *HelloRequest)
(*User, error) {
- return nil, status.Errorf(codes.Unimplemented, "method SayHello not
implemented")
+ return msg, nil
}
-func (UnimplementedGreeterServer) SayHelloStream(Greeter_SayHelloStreamServer)
error {
- return status.Errorf(codes.Unimplemented, "method SayHelloStream not
implemented")
-}
-func (s *UnimplementedGreeterServer) XXX_SetProxyImpl(impl protocol.Invoker) {
- s.proxyImpl = impl
-}
-
-func (s *UnimplementedGreeterServer) XXX_GetProxyImpl() protocol.Invoker {
- return s.proxyImpl
-}
-
-func (s *UnimplementedGreeterServer) XXX_ServiceDesc() *grpc_go.ServiceDesc {
- return &Greeter_ServiceDesc
-}
-func (s *UnimplementedGreeterServer) XXX_InterfaceName() string {
- return "api.Greeter"
-}
-
-func (UnimplementedGreeterServer) mustEmbedUnimplementedGreeterServer() {}
-// UnsafeGreeterServer may be embedded to opt out of forward compatibility for
this service.
-// Use of this interface is not recommended, as added methods to GreeterServer
will
-// result in compilation errors.
-type UnsafeGreeterServer interface {
- mustEmbedUnimplementedGreeterServer()
+var Greeter_ClientInfo = client.ClientInfo{
+ InterfaceName: "api.Greeter",
+ MethodNames: []string{"SayHello", "SayHelloStream"},
+ ConnectionInjectFunc: func(dubboCliRaw interface{}, conn
*client.Connection) {
+ dubboCli := dubboCliRaw.(*GreeterImpl)
+ dubboCli.conn = conn
+ },
}
-func RegisterGreeterServer(s grpc_go.ServiceRegistrar, srv GreeterServer) {
- s.RegisterService(&Greeter_ServiceDesc, srv)
+// GreeterHandler is an implementation of the api.Greeter service.
+type GreeterHandler interface {
+ SayHello(context.Context, *HelloRequest) (*User, error)
+ SayHelloStream(context.Context, Greeter_SayHelloStreamServer) error
}
-func _Greeter_SayHello_Handler(srv any, ctx context.Context, dec func(any)
error, interceptor grpc_go.UnaryServerInterceptor) (any, error) {
- in := new(HelloRequest)
- if err := dec(in); err != nil {
- return nil, err
- }
- base := srv.(dubbo3.Dubbo3GrpcService)
- args := []any{}
- args = append(args, in)
- md, _ := metadata.FromIncomingContext(ctx)
- invAttachment := make(map[string]any, len(md))
- for k, v := range md {
- invAttachment[k] = v
- }
- invo := invocation.NewRPCInvocation("SayHello", args, invAttachment)
- if interceptor == nil {
- result := base.XXX_GetProxyImpl().Invoke(ctx, invo)
- return result, result.Error()
- }
- info := &grpc_go.UnaryServerInfo{
- Server: srv,
- FullMethod: ctx.Value("XXX_TRIPLE_GO_INTERFACE_NAME").(string),
- }
- handler := func(ctx context.Context, req any) (any, error) {
- result := base.XXX_GetProxyImpl().Invoke(ctx, invo)
- return result, result.Error()
- }
- return interceptor(ctx, in, info, handler)
+func RegisterGreeterHandler(srv *server.Server, hdlr GreeterHandler, opts
...server.ServiceOption) error {
+ return srv.Register(hdlr, &Greeter_ServiceInfo, opts...)
}
-func _Greeter_SayHelloStream_Handler(srv any, stream grpc_go.ServerStream)
error {
- _, ok := srv.(dubbo3.Dubbo3GrpcService)
- ctx := stream.Context()
- md, _ := metadata.FromIncomingContext(ctx)
- invAttachment := make(map[string]any, len(md))
- for k, v := range md {
- invAttachment[k] = v
- }
- stream.(grpc_go.CtxSetterStream).SetContext(context.WithValue(ctx,
constant1.AttachmentKey, invAttachment))
- invo := invocation.NewRPCInvocation("SayHelloStream", nil, nil)
- if !ok {
- fmt.Println(invo)
- return nil
- }
- return
srv.(GreeterServer).SayHelloStream(&greeterSayHelloStreamServer{stream})
+func SetProviderGreeter(srv common.RPCService) {
+ dubbo.SetProviderServiceWithInfo(srv, &Greeter_ServiceInfo)
}
type Greeter_SayHelloStreamServer interface {
Send(*User) error
Recv() (*HelloRequest, error)
- grpc_go.ServerStream
+ Spec() triple_protocol.Spec
+ Peer() triple_protocol.Peer
+ RequestHeader() http.Header
+ ResponseHeader() http.Header
+ ResponseTrailer() http.Header
+ Conn() triple_protocol.StreamingHandlerConn
}
-type greeterSayHelloStreamServer struct {
- grpc_go.ServerStream
+type GreeterSayHelloStreamServer struct {
+ *triple_protocol.BidiStream
}
-func (x *greeterSayHelloStreamServer) Send(m *User) error {
- return x.ServerStream.SendMsg(m)
+func (srv *GreeterSayHelloStreamServer) Send(msg *User) error {
+ return srv.BidiStream.Send(msg)
}
-func (x *greeterSayHelloStreamServer) Recv() (*HelloRequest, error) {
- m := new(HelloRequest)
- if err := x.ServerStream.RecvMsg(m); err != nil {
+func (srv GreeterSayHelloStreamServer) Recv() (*HelloRequest, error) {
+ msg := new(HelloRequest)
+ if err := srv.BidiStream.Receive(msg); err != nil {
return nil, err
}
- return m, nil
+ return msg, nil
}
-// Greeter_ServiceDesc is the grpc_go.ServiceDesc for Greeter service.
-// It's only intended for direct use with grpc_go.RegisterService,
-// and not to be introspected or modified (even as a copy)
-var Greeter_ServiceDesc = grpc_go.ServiceDesc{
- ServiceName: "api.Greeter",
- HandlerType: (*GreeterServer)(nil),
- Methods: []grpc_go.MethodDesc{
+var Greeter_ServiceInfo = server.ServiceInfo{
+ InterfaceName: "api.Greeter",
+ ServiceType: (*GreeterHandler)(nil),
+ Methods: []server.MethodInfo{
{
- MethodName: "SayHello",
- Handler: _Greeter_SayHello_Handler,
+ Name: "SayHello",
+ Type: constant.CallUnary,
+ ReqInitFunc: func() interface{} {
+ return new(HelloRequest)
+ },
+ MethodFunc: func(ctx context.Context, args
[]interface{}, handler interface{}) (interface{}, error) {
+ req := args[0].(*HelloRequest)
+ res, err :=
handler.(GreeterHandler).SayHello(ctx, req)
+ if err != nil {
+ return nil, err
+ }
+ return triple_protocol.NewResponse(res), nil
+ },
+ Meta: map[string]interface{}{
+ "response.type": reflect.TypeOf(new(User)),
+ },
},
- },
- Streams: []grpc_go.StreamDesc{
{
- StreamName: "SayHelloStream",
- Handler: _Greeter_SayHelloStream_Handler,
- ServerStreams: true,
- ClientStreams: true,
+ Name: "SayHelloStream",
+ Type: constant.CallBidiStream,
+ StreamInitFunc: func(baseStream interface{})
interface{} {
+ return
&GreeterSayHelloStreamServer{baseStream.(*triple_protocol.BidiStream)}
+ },
+ MethodFunc: func(ctx context.Context, args
[]interface{}, handler interface{}) (interface{}, error) {
+ stream := args[0].(Greeter_SayHelloStreamServer)
+ if err :=
handler.(GreeterHandler).SayHelloStream(ctx, stream); err != nil {
+ return nil, err
+ }
+ return nil, nil
+ },
+ Meta: map[string]interface{}{
+ "request.type":
reflect.TypeOf(new(HelloRequest)),
+ "response.type": reflect.TypeOf(new(User)),
+ },
},
},
- Metadata: "api/samples_api.proto",
}
`
)
func init() {
fileMap["apiTripleFile"] = &fileGenerator{
path: "./api",
- file: "samples_api_triple.pb.go",
+ file: "samples_api.triple.go",
context: license + "\n" + apiTripleFile,
}
}
Review Comment:
The generator source file is named `api_tripe.go` (typo: "tripe" vs
"triple"), which makes it harder to discover/grep and inconsistent with the
generated `*.triple.go` naming used elsewhere in this PR. Consider renaming
this file to `api_triple.go` for clarity (no functional change, but improves
maintainability).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]