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 {
//nolint:modernize
idx := strings.IndexByte(host, '.')
if idx != -1 {
target := host[:idx]
before, _, ok := strings.Cut(host, ".")
if ok {
target := before
if r, ok := routes.Get(target); ok {
return r
}

View File

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

View File

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

View File

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

View File

@@ -5,6 +5,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"slices"
"strings"
"testing"
@@ -623,13 +624,7 @@ func TestAllFields(t *testing.T) {
require.Len(t, AllFields, len(expectedFields), "Expected %d fields", len(expectedFields))
for _, expected := range expectedFields {
found := false
for _, actual := range AllFields {
if actual == expected {
found = true
break
}
}
found := slices.Contains(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 {
idx := strings.IndexByte(s, '#')
if idx == -1 {
before, _, ok := strings.Cut(s, "#")
if !ok {
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 {
incomplete = append(incomplete, more[:nMore]...)
// Check if pattern is now complete
//nolint:modernize
if idx := bytes.IndexByte(incomplete, '}'); idx >= 0 {
if found := bytes.Contains(incomplete, []byte{'}'}); found {
// Pattern complete, append the rest back to chunk
chunk = append(chunk, incomplete...)
break

View File

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

View File

@@ -63,11 +63,11 @@ func TestValidateWithCustomValidator_PointerMethodWithPointerPassed(t *testing.T
input *CustomValidatingInt
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
{"invalid custom validating int pointer - zero", ptr(CustomValidatingInt(0)), true},
{"invalid custom validating int pointer - negative", ptr(CustomValidatingInt(-5)), true},
{"invalid custom validating int pointer - too large", ptr(CustomValidatingInt(200)), true},
{"invalid custom validating int pointer - zero", new(CustomValidatingInt(0)), true},
{"invalid custom validating int pointer - negative", new(CustomValidatingInt(-5)), true},
{"invalid custom validating int pointer - too large", new(CustomValidatingInt(200)), true},
}
for _, tt := range tests {
@@ -86,10 +86,10 @@ func TestValidateWithCustomValidator_ValueMethodButPointerPassed(t *testing.T) {
input *CustomValidatingFloat
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},
{"invalid custom validating float pointer - negative", ptr(CustomValidatingFloat(-5.5)), true},
{"invalid custom validating float pointer - too large", ptr(CustomValidatingFloat(2000.5)), true},
{"invalid custom validating float pointer - negative", new(CustomValidatingFloat(-5.5)), true},
{"invalid custom validating float pointer - too large", new(CustomValidatingFloat(2000.5)), true},
}
for _, tt := range tests {

View File

@@ -27,9 +27,9 @@ func TestValidateWithCustomValidator_StringPointer(t *testing.T) {
input *string
wantErr bool
}{
{"valid string pointer", ptr("hello"), false},
{"valid string pointer", new("hello"), false},
{"nil string pointer", nil, false},
{"empty string pointer", ptr(""), false},
{"empty string pointer", new(""), false},
}
for _, tt := range tests {
@@ -69,11 +69,11 @@ func TestValidateWithCustomValidator_CustomValidatingPointerStringPointer(t *tes
input *CustomValidatingPointerString
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
{"invalid custom validating pointer string - empty", customStringPointerPtr(CustomValidatingPointerString("")), true},
{"invalid custom validating pointer string - too short", customStringPointerPtr(CustomValidatingPointerString("a")), true},
{"valid custom validating pointer string - minimum length", customStringPointerPtr(CustomValidatingPointerString("ab")), false},
{"invalid custom validating pointer string - empty", new(CustomValidatingPointerString("")), true},
{"invalid custom validating pointer string - too short", new(CustomValidatingPointerString("a")), true},
{"valid custom validating pointer string - minimum length", new(CustomValidatingPointerString("ab")), false},
}
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
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},
{"invalid custom validating string pointer - empty", ptr(CustomValidatingString("")), true},
{"invalid custom validating string pointer - too short", ptr(CustomValidatingString("a")), true},
{"invalid custom validating string pointer - empty", new(CustomValidatingString("")), true},
{"invalid custom validating string pointer - too short", new(CustomValidatingString("a")), true},
}
for _, tt := range tests {