chore(lint): add ireturn linter

- Return concrete structs
- Accept interfaces
- Define narrow interfaces locally where needed
This commit is contained in:
Quentin McGaw
2022-08-28 19:50:02 +00:00
parent 45c684232d
commit 1561babd76
25 changed files with 83 additions and 115 deletions

View File

@@ -1,18 +1,11 @@
package dns
import (
"context"
"net"
"github.com/miekg/dns"
)
type Fetcher interface {
IP(ctx context.Context) (publicIP net.IP, err error)
IP4(ctx context.Context) (publicIP net.IP, err error)
IP6(ctx context.Context) (publicIP net.IP, err error)
}
type fetcher struct {
ring ring
client Client
@@ -26,7 +19,7 @@ type ring struct {
providers []Provider
}
func New(options ...Option) (f Fetcher, err error) {
func New(options ...Option) (f *fetcher, err error) {
settings := newDefaultSettings()
for _, option := range options {
if err := option(&settings); err != nil {

View File

@@ -11,12 +11,9 @@ import (
func Test_New(t *testing.T) {
t.Parallel()
intf, err := New(SetTimeout(time.Hour))
impl, err := New(SetTimeout(time.Hour))
require.NoError(t, err)
impl, ok := intf.(*fetcher)
require.True(t, ok)
assert.NotNil(t, impl.ring.counter)
assert.NotEmpty(t, impl.ring.providers)
assert.NotNil(t, impl.client)

View File

@@ -1,20 +1,12 @@
package http
import (
"context"
"net"
"net/http"
"time"
"github.com/qdm12/ddns-updater/pkg/publicip/ipversion"
)
type Fetcher interface {
IP(ctx context.Context) (publicIP net.IP, err error)
IP4(ctx context.Context) (publicIP net.IP, err error)
IP6(ctx context.Context) (publicIP net.IP, err error)
}
type fetcher struct {
client *http.Client
timeout time.Duration
@@ -28,7 +20,7 @@ type urlsRing struct {
urls []string
}
func New(client *http.Client, options ...Option) (f Fetcher, err error) {
func New(client *http.Client, options ...Option) (f *fetcher, err error) {
settings := newDefaultSettings()
for _, option := range options {
if err := option(&settings); err != nil {

View File

@@ -75,18 +75,16 @@ func Test_New(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()
f, err := New(client, testCase.options...)
fetcher, err := New(client, testCase.options...)
if testCase.err != nil {
require.Error(t, err)
assert.Equal(t, testCase.err.Error(), err.Error())
assert.Nil(t, f)
assert.Nil(t, fetcher)
return
}
assert.NoError(t, err)
implementation, ok := f.(*fetcher)
require.True(t, ok)
assert.Equal(t, testCase.fetcher, implementation)
assert.Equal(t, testCase.fetcher, fetcher)
})
}
}

View File

@@ -9,7 +9,7 @@ import (
"github.com/qdm12/ddns-updater/pkg/publicip/http"
)
type Fetcher interface {
type ipFetcher interface {
IP(ctx context.Context) (ip net.IP, err error)
IP4(ctx context.Context) (ipv4 net.IP, err error)
IP6(ctx context.Context) (ipv6 net.IP, err error)
@@ -17,14 +17,14 @@ type Fetcher interface {
type fetcher struct {
settings settings
fetchers []Fetcher
fetchers []ipFetcher
// Cycling effect if both are enabled
counter *uint32 // 32 bit for 32 bit systems
}
var ErrNoFetchTypeSpecified = errors.New("at least one fetcher type must be specified")
func NewFetcher(dnsSettings DNSSettings, httpSettings HTTPSettings) (f Fetcher, err error) {
func NewFetcher(dnsSettings DNSSettings, httpSettings HTTPSettings) (f *fetcher, err error) {
settings := settings{
dns: dnsSettings,
http: httpSettings,

View File

@@ -4,7 +4,7 @@ import (
"sync/atomic"
)
func (f *fetcher) getSubFetcher() Fetcher {
func (f *fetcher) getSubFetcher() ipFetcher { //nolint:ireturn
fetcher := f.fetchers[0]
if len(f.fetchers) > 1 { // cycling effect
index := int(atomic.AddUint32(f.counter, 1)) % len(f.fetchers)