refactor: modernize code with go fix

This commit is contained in:
yusing
2026-02-21 13:03:21 +08:00
parent 64ffe44a2d
commit 3a7d1f8b18
12 changed files with 32 additions and 61 deletions

Submodule goutils updated: 0dbc371839...8849086bc0

View File

@@ -162,10 +162,9 @@ func (ep *Entrypoint) SetAccessLogger(parent task.Parent, cfg *accesslog.Request
} }
func findRouteAnyDomain(routes HTTPRoutes, host string) types.HTTPRoute { func findRouteAnyDomain(routes HTTPRoutes, host string) types.HTTPRoute {
//nolint:modernize before, _, ok := strings.Cut(host, ".")
idx := strings.IndexByte(host, '.') if ok {
if idx != -1 { target := before
target := host[:idx]
if r, ok := routes.Get(target); ok { if r, ok := routes.Get(target); ok {
return r return r
} }

View File

@@ -9,10 +9,6 @@ import (
expect "github.com/yusing/goutils/testing" expect "github.com/yusing/goutils/testing"
) )
func strPtr(s string) *string {
return &s
}
func TestOverrideItem(t *testing.T) { func TestOverrideItem(t *testing.T) {
a := &Item{ a := &Item{
Alias: "foo", Alias: "foo",
@@ -20,7 +16,7 @@ func TestOverrideItem(t *testing.T) {
Show: false, Show: false,
Name: "Foo", Name: "Foo",
Icon: &icons.URL{ Icon: &icons.URL{
FullURL: strPtr("/favicon.ico"), FullURL: new("/favicon.ico"),
Source: icons.SourceRelative, Source: icons.SourceRelative,
}, },
Category: "App", Category: "App",
@@ -31,7 +27,7 @@ func TestOverrideItem(t *testing.T) {
Name: "Bar", Name: "Bar",
Category: "Test", Category: "Test",
Icon: &icons.URL{ Icon: &icons.URL{
FullURL: strPtr("@walkxcode/example.png"), FullURL: new("@walkxcode/example.png"),
Source: icons.SourceWalkXCode, Source: icons.SourceWalkXCode,
}, },
} }

View File

@@ -7,10 +7,6 @@ import (
expect "github.com/yusing/goutils/testing" expect "github.com/yusing/goutils/testing"
) )
func strPtr(s string) *string {
return &s
}
func TestIconURL(t *testing.T) { func TestIconURL(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -22,7 +18,7 @@ func TestIconURL(t *testing.T) {
name: "absolute", name: "absolute",
input: "http://example.com/icon.png", input: "http://example.com/icon.png",
wantValue: &URL{ wantValue: &URL{
FullURL: strPtr("http://example.com/icon.png"), FullURL: new("http://example.com/icon.png"),
Source: SourceAbsolute, Source: SourceAbsolute,
}, },
}, },
@@ -30,7 +26,7 @@ func TestIconURL(t *testing.T) {
name: "relative", name: "relative",
input: "@target/icon.png", input: "@target/icon.png",
wantValue: &URL{ wantValue: &URL{
FullURL: strPtr("/icon.png"), FullURL: new("/icon.png"),
Source: SourceRelative, Source: SourceRelative,
}, },
}, },
@@ -38,7 +34,7 @@ func TestIconURL(t *testing.T) {
name: "relative2", name: "relative2",
input: "/icon.png", input: "/icon.png",
wantValue: &URL{ wantValue: &URL{
FullURL: strPtr("/icon.png"), FullURL: new("/icon.png"),
Source: SourceRelative, Source: SourceRelative,
}, },
}, },

View File

@@ -33,11 +33,7 @@ func (p *Poller[T, AggregateT]) ServeHTTP(c *gin.Context) {
query := c.Request.URL.Query() query := c.Request.URL.Query()
if httpheaders.IsWebsocket(c.Request.Header) { if httpheaders.IsWebsocket(c.Request.Header) {
interval := metricsutils.QueryDuration(query, "interval", 0) interval := max(metricsutils.QueryDuration(query, "interval", 0), PollInterval)
if interval < PollInterval {
interval = PollInterval
}
websocket.PeriodicWrite(c, interval, func() (any, error) { websocket.PeriodicWrite(c, interval, func() (any, error) {
return p.GetRespData(period, query) return p.GetRespData(period, query)
}) })

View File

@@ -5,6 +5,7 @@ import (
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"slices"
"strings" "strings"
"testing" "testing"
@@ -623,13 +624,7 @@ func TestAllFields(t *testing.T) {
require.Len(t, AllFields, len(expectedFields), "Expected %d fields", len(expectedFields)) require.Len(t, AllFields, len(expectedFields), "Expected %d fields", len(expectedFields))
for _, expected := range expectedFields { for _, expected := range expectedFields {
found := false found := slices.Contains(AllFields, expected)
for _, actual := range AllFields {
if actual == expected {
found = true
break
}
}
assert.True(t, found, "Expected field %s not found in AllFields", expected) assert.True(t, found, "Expected field %s not found in AllFields", expected)
} }
} }

View File

@@ -94,9 +94,9 @@ var staticRespVarSubsMap = map[string]respVarGetter{
} }
func stripFragment(s string) string { func stripFragment(s string) string {
idx := strings.IndexByte(s, '#') before, _, ok := strings.Cut(s, "#")
if idx == -1 { if !ok {
return s return s
} }
return s[:idx] return before
} }

View File

@@ -68,8 +68,7 @@ func (r *SubstituteEnvReader) Read(p []byte) (n int, err error) {
if nMore > 0 { if nMore > 0 {
incomplete = append(incomplete, more[:nMore]...) incomplete = append(incomplete, more[:nMore]...)
// Check if pattern is now complete // Check if pattern is now complete
//nolint:modernize if found := bytes.Contains(incomplete, []byte{'}'}); found {
if idx := bytes.IndexByte(incomplete, '}'); idx >= 0 {
// Pattern complete, append the rest back to chunk // Pattern complete, append the rest back to chunk
chunk = append(chunk, incomplete...) chunk = append(chunk, incomplete...)
break break

View File

@@ -6,11 +6,6 @@ import (
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
) )
// Common helper functions
func ptr[T any](s T) *T {
return &s
}
// Common test function for MustRegisterValidation // Common test function for MustRegisterValidation
func TestMustRegisterValidation(t *testing.T) { func TestMustRegisterValidation(t *testing.T) {
// Test registering a custom validation // Test registering a custom validation

View File

@@ -63,11 +63,11 @@ func TestValidateWithCustomValidator_PointerMethodWithPointerPassed(t *testing.T
input *CustomValidatingInt input *CustomValidatingInt
wantErr bool wantErr bool
}{ }{
{"valid custom validating int pointer", ptr(CustomValidatingInt(50)), false}, {"valid custom validating int pointer", new(CustomValidatingInt(50)), false},
{"nil custom validating int pointer", nil, true}, // Should fail because Validate() checks for nil {"nil custom validating int pointer", nil, true}, // Should fail because Validate() checks for nil
{"invalid custom validating int pointer - zero", ptr(CustomValidatingInt(0)), true}, {"invalid custom validating int pointer - zero", new(CustomValidatingInt(0)), true},
{"invalid custom validating int pointer - negative", ptr(CustomValidatingInt(-5)), true}, {"invalid custom validating int pointer - negative", new(CustomValidatingInt(-5)), true},
{"invalid custom validating int pointer - too large", ptr(CustomValidatingInt(200)), true}, {"invalid custom validating int pointer - too large", new(CustomValidatingInt(200)), true},
} }
for _, tt := range tests { for _, tt := range tests {
@@ -86,10 +86,10 @@ func TestValidateWithCustomValidator_ValueMethodButPointerPassed(t *testing.T) {
input *CustomValidatingFloat input *CustomValidatingFloat
wantErr bool wantErr bool
}{ }{
{"valid custom validating float pointer", ptr(CustomValidatingFloat(50.5)), false}, {"valid custom validating float pointer", new(CustomValidatingFloat(50.5)), false},
{"nil custom validating float pointer", nil, false}, {"nil custom validating float pointer", nil, false},
{"invalid custom validating float pointer - negative", ptr(CustomValidatingFloat(-5.5)), true}, {"invalid custom validating float pointer - negative", new(CustomValidatingFloat(-5.5)), true},
{"invalid custom validating float pointer - too large", ptr(CustomValidatingFloat(2000.5)), true}, {"invalid custom validating float pointer - too large", new(CustomValidatingFloat(2000.5)), true},
} }
for _, tt := range tests { for _, tt := range tests {

View File

@@ -27,9 +27,9 @@ func TestValidateWithCustomValidator_StringPointer(t *testing.T) {
input *string input *string
wantErr bool wantErr bool
}{ }{
{"valid string pointer", ptr("hello"), false}, {"valid string pointer", new("hello"), false},
{"nil string pointer", nil, false}, {"nil string pointer", nil, false},
{"empty string pointer", ptr(""), false}, {"empty string pointer", new(""), false},
} }
for _, tt := range tests { for _, tt := range tests {
@@ -69,11 +69,11 @@ func TestValidateWithCustomValidator_CustomValidatingPointerStringPointer(t *tes
input *CustomValidatingPointerString input *CustomValidatingPointerString
wantErr bool wantErr bool
}{ }{
{"valid custom validating pointer string", customStringPointerPtr(CustomValidatingPointerString("hello")), false}, {"valid custom validating pointer string", new(CustomValidatingPointerString("hello")), false},
{"nil custom validating pointer string", nil, true}, // Should fail because Validate() checks for nil {"nil custom validating pointer string", nil, true}, // Should fail because Validate() checks for nil
{"invalid custom validating pointer string - empty", customStringPointerPtr(CustomValidatingPointerString("")), true}, {"invalid custom validating pointer string - empty", new(CustomValidatingPointerString("")), true},
{"invalid custom validating pointer string - too short", customStringPointerPtr(CustomValidatingPointerString("a")), true}, {"invalid custom validating pointer string - too short", new(CustomValidatingPointerString("a")), true},
{"valid custom validating pointer string - minimum length", customStringPointerPtr(CustomValidatingPointerString("ab")), false}, {"valid custom validating pointer string - minimum length", new(CustomValidatingPointerString("ab")), false},
} }
for _, tt := range tests { for _, tt := range tests {
@@ -85,8 +85,3 @@ func TestValidateWithCustomValidator_CustomValidatingPointerStringPointer(t *tes
}) })
} }
} }
// Helper function to create CustomValidatingPointerString pointer
func customStringPointerPtr(s CustomValidatingPointerString) *CustomValidatingPointerString {
return &s
}

View File

@@ -67,10 +67,10 @@ func TestValidateWithCustomValidator_CustomValidatingStringPointer(t *testing.T)
input *CustomValidatingString input *CustomValidatingString
wantErr bool wantErr bool
}{ }{
{"valid custom validating string pointer", ptr(CustomValidatingString("hello")), false}, {"valid custom validating string pointer", new(CustomValidatingString("hello")), false},
{"nil custom validating string pointer", nil, true}, {"nil custom validating string pointer", nil, true},
{"invalid custom validating string pointer - empty", ptr(CustomValidatingString("")), true}, {"invalid custom validating string pointer - empty", new(CustomValidatingString("")), true},
{"invalid custom validating string pointer - too short", ptr(CustomValidatingString("a")), true}, {"invalid custom validating string pointer - too short", new(CustomValidatingString("a")), true},
} }
for _, tt := range tests { for _, tt := range tests {