Skip to content

Commit 8b7bcb9

Browse files
authored
Merge pull request moby#3761 from cpuguy83/type_safe_clientopt
Make ClientOpts type safe
2 parents 7e6bdcf + eae50a4 commit 8b7bcb9

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

client/client.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ type Client struct {
3434
sessionDialer func(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error)
3535
}
3636

37-
type ClientOpt interface{}
37+
type ClientOpt interface {
38+
isClientOpt()
39+
}
3840

3941
// New returns a new buildkit client. Address can be empty for the system-default address.
4042
func New(ctx context.Context, address string, opts ...ClientOpt) (*Client, error) {
@@ -82,8 +84,8 @@ func New(ctx context.Context, address string, opts ...ClientOpt) (*Client, error
8284
if sd, ok := o.(*withSessionDialer); ok {
8385
sessionDialer = sd.dialer
8486
}
85-
if opt, ok := o.(grpc.DialOption); ok {
86-
customDialOptions = append(customDialOptions, opt)
87+
if opt, ok := o.(*withGRPCDialOption); ok {
88+
customDialOptions = append(customDialOptions, opt.opt)
8789
}
8890
}
8991

@@ -182,6 +184,8 @@ func (c *Client) Close() error {
182184

183185
type withFailFast struct{}
184186

187+
func (*withFailFast) isClientOpt() {}
188+
185189
func WithFailFast() ClientOpt {
186190
return &withFailFast{}
187191
}
@@ -190,6 +194,8 @@ type withDialer struct {
190194
dialer func(context.Context, string) (net.Conn, error)
191195
}
192196

197+
func (*withDialer) isClientOpt() {}
198+
193199
func WithContextDialer(df func(context.Context, string) (net.Conn, error)) ClientOpt {
194200
return &withDialer{dialer: df}
195201
}
@@ -201,6 +207,8 @@ type withCredentials struct {
201207
Key string
202208
}
203209

210+
func (*withCredentials) isClientOpt() {}
211+
204212
// WithCredentials configures the TLS parameters of the client.
205213
// Arguments:
206214
// * serverName: specifies the name of the target server
@@ -247,6 +255,8 @@ type withTracer struct {
247255
tp trace.TracerProvider
248256
}
249257

258+
func (w *withTracer) isClientOpt() {}
259+
250260
type TracerDelegate interface {
251261
SetSpanExporter(context.Context, sdktrace.SpanExporter) error
252262
}
@@ -261,6 +271,8 @@ type withTracerDelegate struct {
261271
TracerDelegate
262272
}
263273

274+
func (w *withTracerDelegate) isClientOpt() {}
275+
264276
func WithSessionDialer(dialer func(context.Context, string, map[string][]string) (net.Conn, error)) ClientOpt {
265277
return &withSessionDialer{dialer}
266278
}
@@ -269,6 +281,8 @@ type withSessionDialer struct {
269281
dialer func(context.Context, string, map[string][]string) (net.Conn, error)
270282
}
271283

284+
func (w *withSessionDialer) isClientOpt() {}
285+
272286
func resolveDialer(address string) (func(context.Context, string) (net.Conn, error), error) {
273287
ch, err := connhelper.GetConnectionHelper(address)
274288
if err != nil {
@@ -289,3 +303,13 @@ func filterInterceptor(intercept grpc.UnaryClientInterceptor) grpc.UnaryClientIn
289303
return intercept(ctx, method, req, reply, cc, invoker, opts...)
290304
}
291305
}
306+
307+
type withGRPCDialOption struct {
308+
opt grpc.DialOption
309+
}
310+
311+
func (*withGRPCDialOption) isClientOpt() {}
312+
313+
func WithGRPCDialOption(opt grpc.DialOption) ClientOpt {
314+
return &withGRPCDialOption{opt}
315+
}

client/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9036,7 +9036,7 @@ func testClientCustomGRPCOpts(t *testing.T, sb integration.Sandbox) {
90369036
interceptedMethods = append(interceptedMethods, method)
90379037
return invoker(ctx, method, req, reply, cc, opts...)
90389038
}
9039-
c, err := New(sb.Context(), sb.Address(), grpc.WithChainUnaryInterceptor(intercept))
9039+
c, err := New(sb.Context(), sb.Address(), WithGRPCDialOption(grpc.WithChainUnaryInterceptor(intercept)))
90409040
require.NoError(t, err)
90419041
defer c.Close()
90429042

0 commit comments

Comments
 (0)