Skip to content

Commit d4d09a5

Browse files
MKrupauskasjeffbean
authored andcommitted
refactor options to be structs + interface
1 parent e8570cf commit d4d09a5

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

dnshostprovider.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,29 @@ import (
1010

1111
const _defaultLookupTimeout = 3 * time.Second
1212

13+
type lookupHostFn func(context.Context, string) ([]string, error)
14+
1315
// DNSHostProviderOption is an option for the DNSHostProvider.
14-
type DNSHostProviderOption func (*DNSHostProvider)
16+
type DNSHostProviderOption interface {
17+
apply(*DNSHostProvider)
18+
}
19+
20+
type lookupTimeoutOption struct {
21+
timeout time.Duration
22+
}
1523

1624
// WithLookupTimeout returns a DNSHostProviderOption that sets the lookup timeout.
1725
func WithLookupTimeout(timeout time.Duration) DNSHostProviderOption {
18-
return func(provider *DNSHostProvider) {
19-
provider.lookupTimeout = timeout
26+
return lookupTimeoutOption{
27+
timeout: timeout,
2028
}
2129
}
2230

31+
func (o lookupTimeoutOption) apply(provider *DNSHostProvider) {
32+
provider.lookupTimeout = o.timeout
33+
}
34+
35+
2336
// DNSHostProvider is the default HostProvider. It currently matches
2437
// the Java StaticHostProvider, resolving hosts from DNS once during
2538
// the call to Init. It could be easily extended to re-query DNS
@@ -30,7 +43,7 @@ type DNSHostProvider struct {
3043
curr int
3144
last int
3245
lookupTimeout time.Duration
33-
lookupHost func(context.Context, string) ([]string, error) // Override of net.LookupHost, for testing.
46+
lookupHost lookupHostFn // Override of net.LookupHost, for testing.
3447
}
3548

3649
// NewDNSHostProvider creates a new DNSHostProvider with the given options.

dnshostprovider_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@ import (
88
"time"
99
)
1010

11-
func withLookupHost(lookupFn func(context.Context, string) ([]string, error)) DNSHostProviderOption {
12-
return func(provider *DNSHostProvider) {
13-
provider.lookupHost = lookupFn
11+
type lookupHostOption struct {
12+
lookupFn lookupHostFn
13+
}
14+
15+
func withLookupHost(lookupFn lookupHostFn) DNSHostProviderOption {
16+
return lookupHostOption{
17+
lookupFn: lookupFn,
1418
}
1519
}
1620

21+
func (o lookupHostOption) apply(provider *DNSHostProvider) {
22+
provider.lookupHost = o.lookupFn
23+
}
24+
1725
// TestDNSHostProviderCreate is just like TestCreate, but with an
1826
// overridden HostProvider that ignores the provided hostname.
1927
func TestIntegration_DNSHostProviderCreate(t *testing.T) {

0 commit comments

Comments
 (0)