Merge pull request #31 from unistack-org/fix

fix drpc method passing
This commit is contained in:
Василий Толстов 2022-03-21 12:55:40 +03:00 committed by GitHub
commit 5758c7dfd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 14 deletions

View File

@ -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 { func (d *drpcClient) call(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
var header map[string]string var header map[string]string
if strings.HasPrefix(addr, "http") {
addr = addr[strings.Index(addr, ":")+3:]
}
if md, ok := metadata.FromOutgoingContext(ctx); ok { if md, ok := metadata.FromOutgoingContext(ctx); ok {
header = make(map[string]string, len(md)) 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) ch := make(chan error, 1)
_ = dialCtx _ = dialCtx
// rc, err := net.DialContext(ctx, "tcp", addr) // rc, err := net.DialContext(ctx, "tcp", addr)
rc, err := net.Dial("tcp", addr) rc, err := net.Dial("tcp", addr)
if err != nil { if err != nil {

View File

@ -18,24 +18,19 @@ type drpcRequest struct {
} }
// service Struct.Method /service.Struct/Method // service Struct.Method /service.Struct/Method
func methodToDRPC(service, method string) string { func methodToDRPC(service string, method string) string {
// no method or already grpc method // no method or already drpc method
if len(method) == 0 || method[0] == '/' { if len(method) == 0 || method[0] == '/' {
return method return method
} }
// assume method is Foo.Bar idx := strings.LastIndex(method, ".")
mParts := strings.Split(method, ".") if len(method) < 3 || idx < 2 {
if len(mParts) != 2 { return fmt.Sprintf("/%s.%s", strings.Title(service), method)
return method
} }
drpcService := method[:idx]
if len(service) == 0 { drpcMethod := method[idx+1:]
return fmt.Sprintf("/%s/%s", mParts[0], mParts[1]) return fmt.Sprintf("/%s/%s", drpcService, drpcMethod)
}
// return /pkg.Foo/Bar
return fmt.Sprintf("/%s.%s/%s", strings.Title(service), mParts[0], mParts[1])
} }
func newDRPCRequest(service, method string, request interface{}, contentType string, reqOpts ...client.RequestOption) client.Request { func newDRPCRequest(service, method string, request interface{}, contentType string, reqOpts ...client.RequestOption) client.Request {

View File

@ -12,7 +12,7 @@ func TestMethodToDRPC(t *testing.T) {
}{ }{
{ {
"helloworld", "helloworld",
"Greeter.SayHello", "Greeter/SayHello",
"/Helloworld.Greeter/SayHello", "/Helloworld.Greeter/SayHello",
}, },
{ {