Use switch when creating TTRPC/GRPC client

Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
Maksym Pavlenko 2023-02-06 14:51:14 -08:00
parent a82e37a5a2
commit 77fc0948c4
2 changed files with 14 additions and 18 deletions

View File

@ -38,16 +38,14 @@ import (
// In 1.7 we support TaskService v2 (for backward compatibility with existing shims) and GRPC TaskService v3.
// In 2.0 we'll switch to TaskService v3 only for both TTRPC and GRPC, which will remove overhead of mapping v2 structs to v3 structs.
func NewTaskClient(client interface{}) (v2.TaskService, error) {
if ttrpcClient, ok := client.(*ttrpc.Client); ok {
return v2.NewTaskClient(ttrpcClient), nil
switch c := client.(type) {
case *ttrpc.Client:
return v2.NewTaskClient(c), nil
case grpc.ClientConnInterface:
return &grpcBridge{v3.NewTaskClient(c)}, nil
default:
return nil, fmt.Errorf("unsupported shim client type %T", c)
}
if grpcClient, ok := client.(grpc.ClientConnInterface); ok {
client := v3.NewTaskClient(grpcClient)
return &grpcBridge{client}, nil
}
return nil, fmt.Errorf("unsupported shim client type %T", client)
}
// grpcBridge implements `v2.TaskService` interface for GRPC shim server.

View File

@ -28,16 +28,14 @@ import (
// NewClient returns a new sandbox client that handles both GRPC and TTRPC clients.
func NewClient(client interface{}) (api.TTRPCSandboxService, error) {
if ttrpcClient, ok := client.(*ttrpc.Client); ok {
return api.NewTTRPCSandboxClient(ttrpcClient), nil
switch c := client.(type) {
case *ttrpc.Client:
return api.NewTTRPCSandboxClient(c), nil
case grpc.ClientConnInterface:
return &grpcBridge{api.NewSandboxClient(c)}, nil
default:
return nil, fmt.Errorf("unsupported client type %T", client)
}
if grpcClient, ok := client.(grpc.ClientConnInterface); ok {
client := api.NewSandboxClient(grpcClient)
return &grpcBridge{client}, nil
}
return nil, fmt.Errorf("unsupported client type %T", client)
}
type grpcBridge struct {