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 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. // 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) { func NewTaskClient(client interface{}) (v2.TaskService, error) {
if ttrpcClient, ok := client.(*ttrpc.Client); ok { switch c := client.(type) {
return v2.NewTaskClient(ttrpcClient), nil 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. // grpcBridge implements `v2.TaskService` interface for GRPC shim server.

View File

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