From 6f4e54a8766ad9c32605f27faf918cf90f4403ae Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 21 Mar 2022 12:48:06 +0300 Subject: [PATCH 1/2] fix drpc method passing Signed-off-by: Vasiliy Tolstov --- drpc.go | 4 ++++ request.go | 20 ++++++-------------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/drpc.go b/drpc.go index fff63f4..32c02d4 100644 --- a/drpc.go +++ b/drpc.go @@ -30,6 +30,9 @@ type drpcClient struct { func (d *drpcClient) call(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error { var header map[string]string + if strings.HasPrefix(addr, "http") { + addr = addr[strings.Index(addr, ":")+3:] + } if md, ok := metadata.FromOutgoingContext(ctx); ok { header = make(map[string]string, len(md)) @@ -90,6 +93,7 @@ func (d *drpcClient) call(ctx context.Context, addr string, req client.Request, */ ch := make(chan error, 1) _ = dialCtx + // rc, err := net.DialContext(ctx, "tcp", addr) rc, err := net.Dial("tcp", addr) if err != nil { diff --git a/request.go b/request.go index fcb814b..dd60630 100644 --- a/request.go +++ b/request.go @@ -18,24 +18,16 @@ type drpcRequest struct { } // service Struct.Method /service.Struct/Method -func methodToDRPC(service, method string) string { - // no method or already grpc method +func methodToDRPC(_ string, method string) string { + // no method or already drpc method if len(method) == 0 || method[0] == '/' { return method } - // assume method is Foo.Bar - mParts := strings.Split(method, ".") - if len(mParts) != 2 { - return method - } - - if len(service) == 0 { - return fmt.Sprintf("/%s/%s", mParts[0], mParts[1]) - } - - // return /pkg.Foo/Bar - return fmt.Sprintf("/%s.%s/%s", strings.Title(service), mParts[0], mParts[1]) + idx := strings.LastIndex(method, ".") + drpcService := method[:idx] + drpcMethod := method[idx+1:] + return fmt.Sprintf("/%s/%s", drpcService, drpcMethod) } func newDRPCRequest(service, method string, request interface{}, contentType string, reqOpts ...client.RequestOption) client.Request { -- 2.45.2 From de02d636adf6f20341a776f110b329c106d1946c Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 21 Mar 2022 12:53:33 +0300 Subject: [PATCH 2/2] fix drpc method passing Signed-off-by: Vasiliy Tolstov --- request.go | 5 ++++- request_test.go | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/request.go b/request.go index dd60630..fddae5f 100644 --- a/request.go +++ b/request.go @@ -18,13 +18,16 @@ type drpcRequest struct { } // service Struct.Method /service.Struct/Method -func methodToDRPC(_ string, method string) string { +func methodToDRPC(service string, method string) string { // no method or already drpc method if len(method) == 0 || method[0] == '/' { return method } idx := strings.LastIndex(method, ".") + if len(method) < 3 || idx < 2 { + return fmt.Sprintf("/%s.%s", strings.Title(service), method) + } drpcService := method[:idx] drpcMethod := method[idx+1:] return fmt.Sprintf("/%s/%s", drpcService, drpcMethod) diff --git a/request_test.go b/request_test.go index 9369d05..c2d5870 100644 --- a/request_test.go +++ b/request_test.go @@ -12,7 +12,7 @@ func TestMethodToDRPC(t *testing.T) { }{ { "helloworld", - "Greeter.SayHello", + "Greeter/SayHello", "/Helloworld.Greeter/SayHello", }, { -- 2.45.2