mirror of
https://github.com/qdm12/ddns-updater.git
synced 2026-04-05 08:54:09 -04:00
chore(lint): add ireturn linter
- Return concrete structs - Accept interfaces - Define narrow interfaces locally where needed
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user