Skip to content

Commit 24e81ce

Browse files
committed
Modernize Go code with go fix
Apply the modernize analyzers integrated into go1.26's `go fix` across the codebase: strings.SplitSeq/CutSuffix/CutPrefix, slices.Contains, maps.Copy/Clone, fmt.Appendf, new(expr), for-range over integers, and any.
1 parent f0b18fe commit 24e81ce

30 files changed

Lines changed: 151 additions & 190 deletions

checkers/domain_contact.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (r *domainContactRule) ValidateOptions(opts happydns.CheckerOptions) error
7474

7575
if v, ok := opts["checkRoles"].(string); ok && v != "" {
7676
hasOne := false
77-
for _, p := range strings.Split(v, ",") {
77+
for p := range strings.SplitSeq(v, ",") {
7878
role := strings.TrimSpace(p)
7979
if role == "" {
8080
continue

checkers/domain_lock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (r *domainLockRule) ValidateOptions(opts happydns.CheckerOptions) error {
6565
if !ok {
6666
return fmt.Errorf("requiredStatuses must be a string")
6767
}
68-
for _, p := range strings.Split(s, ",") {
68+
for p := range strings.SplitSeq(s, ",") {
6969
if strings.TrimSpace(p) != "" {
7070
return nil
7171
}
@@ -89,7 +89,7 @@ func (r *domainLockRule) Evaluate(ctx context.Context, obs happydns.ObservationG
8989
}
9090

9191
var required []string
92-
for _, s := range strings.Split(requiredStr, ",") {
92+
for s := range strings.SplitSeq(requiredStr, ",") {
9393
s = strings.TrimSpace(s)
9494
if s != "" {
9595
required = append(required, s)

internal/adapters/libdns-providers_test.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package adapter
2424
import (
2525
"context"
2626
"net/netip"
27+
"slices"
2728
"testing"
2829
"time"
2930

@@ -303,27 +304,15 @@ func TestGetLibdnsProviderCapabilities(t *testing.T) {
303304
caps := GetLibdnsProviderCapabilities(config)
304305

305306
// Should include ListDomains since mock implements ZoneLister
306-
found := false
307-
for _, c := range caps {
308-
if c == "ListDomains" {
309-
found = true
310-
break
311-
}
312-
}
307+
found := slices.Contains(caps, "ListDomains")
313308
if !found {
314309
t.Error("expected ListDomains capability")
315310
}
316311

317312
// Should include common RR types
318313
expectedTypes := []string{"rr-1-A", "rr-28-AAAA", "rr-5-CNAME", "rr-15-MX", "rr-16-TXT"}
319314
for _, expected := range expectedTypes {
320-
found = false
321-
for _, c := range caps {
322-
if c == expected {
323-
found = true
324-
break
325-
}
326-
}
315+
found = slices.Contains(caps, expected)
327316
if !found {
328317
t.Errorf("expected capability %s", expected)
329318
}

internal/dnschecker/observation.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"encoding/json"
4343
"fmt"
4444
"log"
45+
"maps"
4546
"sync"
4647
"time"
4748

@@ -288,8 +289,6 @@ func (oc *ObservationContext) Data() map[happydns.ObservationKey]json.RawMessage
288289
defer oc.mu.Unlock()
289290

290291
data := make(map[happydns.ObservationKey]json.RawMessage, len(oc.cache))
291-
for k, v := range oc.cache {
292-
data[k] = v
293-
}
292+
maps.Copy(data, oc.cache)
294293
return data
295294
}

internal/dnschecker/observation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestObservationContext_DedupesSameKey(t *testing.T) {
126126
const N = 8
127127
var wg sync.WaitGroup
128128
wg.Add(N)
129-
for i := 0; i < N; i++ {
129+
for range N {
130130
go func() {
131131
defer wg.Done()
132132
var dst map[string]string

internal/helpers/dns.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ func DomainRelative(subdomain string, origin string) string {
9292
origin += "."
9393
}
9494

95-
if strings.HasSuffix(subdomain, origin) {
96-
subdomain = strings.TrimSuffix(strings.TrimSuffix(subdomain, origin), ".")
95+
if before, ok := strings.CutSuffix(subdomain, origin); ok {
96+
subdomain = strings.TrimSuffix(before, ".")
9797
}
9898

9999
if subdomain == "" {
@@ -143,8 +143,8 @@ func RRRelative(rr happydns.Record, origin string) happydns.Record {
143143
}
144144

145145
// Make header relative
146-
if strings.HasSuffix(rr.Header().Name, origin) {
147-
rr.Header().Name = strings.TrimSuffix(strings.TrimSuffix(rr.Header().Name, origin), ".")
146+
if before, ok := strings.CutSuffix(rr.Header().Name, origin); ok {
147+
rr.Header().Name = strings.TrimSuffix(before, ".")
148148
}
149149

150150
return RDataRelative(rr, origin)
@@ -159,8 +159,8 @@ func RRRelativeSubdomain(rr happydns.Record, origin, subdomain string) happydns.
159159
subdomain = DomainFQDN(subdomain, origin)
160160

161161
// Make header relative
162-
if strings.HasSuffix(rr.Header().Name, subdomain) {
163-
rr.Header().Name = strings.TrimSuffix(strings.TrimSuffix(rr.Header().Name, subdomain), ".")
162+
if before, ok := strings.CutSuffix(rr.Header().Name, subdomain); ok {
163+
rr.Header().Name = strings.TrimSuffix(before, ".")
164164
}
165165

166166
return RDataRelative(rr, origin)

internal/helpers/dnssec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ func TestIsDNSSECTypeNonDNSSECTypes(t *testing.T) {
425425
func TestIsDNSSECTypeConsistency(t *testing.T) {
426426
testType := dns.TypeNSEC
427427

428-
for i := 0; i < 100; i++ {
428+
for i := range 100 {
429429
result := IsDNSSECType(testType)
430430
if !result {
431431
t.Errorf("IsDNSSECType returned inconsistent result on iteration %d", i)

internal/mailer/mailer.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package mailer
2424
import (
2525
"bytes"
2626
"io"
27+
"maps"
2728
"net/mail"
2829
"text/template"
2930

@@ -104,10 +105,7 @@ func (r *Mailer) SendMail(to *mail.Address, subject, content string) (err error)
104105
if err != nil {
105106
return err
106107
}
107-
htmlData := map[string]string{}
108-
for k, v := range tplData {
109-
htmlData[k] = v
110-
}
108+
htmlData := maps.Clone(tplData)
111109
htmlData["Content"] = buf.String()
112110
m.AddAlternativeWriter("text/html", func(w io.Writer) error {
113111
return htmlTpl.Execute(w, htmlData)

internal/serviceanalyzer/registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func RegisterSubServices(t reflect.Type) {
9292
for i := 0; i < t.NumField(); i += 1 {
9393
RegisterSubServices(t.Field(i).Type)
9494
}
95-
} else if t.Kind() == reflect.Array || t.Kind() == reflect.Map || t.Kind() == reflect.Ptr || t.Kind() == reflect.Slice {
95+
} else if t.Kind() == reflect.Array || t.Kind() == reflect.Map || t.Kind() == reflect.Pointer || t.Kind() == reflect.Slice {
9696
RegisterSubServices(t.Elem())
9797
} else if t.PkgPath() == pathToSvcsModule {
9898
if _, ok := subServices[t.String()]; ok {

internal/storage/kvtpl/discovery_test.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ type fakeIter struct {
131131
idx int
132132
}
133133

134-
func (i *fakeIter) Release() {}
135-
func (i *fakeIter) Next() bool { i.idx++; return i.idx < len(i.keys) }
136-
func (i *fakeIter) Valid() bool { return i.idx >= 0 && i.idx < len(i.keys) }
137-
func (i *fakeIter) Key() string { return i.keys[i.idx] }
138-
func (i *fakeIter) Value() any { return i.data[i.keys[i.idx]] }
139-
func (i *fakeIter) Err() error { return nil }
134+
func (i *fakeIter) Release() {}
135+
func (i *fakeIter) Next() bool { i.idx++; return i.idx < len(i.keys) }
136+
func (i *fakeIter) Valid() bool { return i.idx >= 0 && i.idx < len(i.keys) }
137+
func (i *fakeIter) Key() string { return i.keys[i.idx] }
138+
func (i *fakeIter) Value() any { return i.data[i.keys[i.idx]] }
139+
func (i *fakeIter) Err() error { return nil }
140140

141141
func newDiscoveryTestStore() *KVStorage {
142142
return &KVStorage{db: newFakeKV()}
@@ -325,11 +325,9 @@ func TestPutDiscoveryObservationRefConcurrentSamePrimary(t *testing.T) {
325325
const N = 16
326326
var wg sync.WaitGroup
327327
errs := make(chan error, N)
328-
for i := 0; i < N; i++ {
328+
for i := range N {
329329
i := i
330-
wg.Add(1)
331-
go func() {
332-
defer wg.Done()
330+
wg.Go(func() {
333331
errs <- s.PutDiscoveryObservationRef(&happydns.DiscoveryObservationRef{
334332
ProducerID: "p",
335333
Target: target,
@@ -339,7 +337,7 @@ func TestPutDiscoveryObservationRefConcurrentSamePrimary(t *testing.T) {
339337
SnapshotID: happydns.Identifier{byte(i + 1)},
340338
CollectedAt: time.Now(),
341339
})
342-
}()
340+
})
343341
}
344342
wg.Wait()
345343
close(errs)

0 commit comments

Comments
 (0)