From 3a7d1f8b1832a04b9052fe41958c8aed21c16636 Mon Sep 17 00:00:00 2001 From: yusing Date: Sat, 21 Feb 2026 13:03:21 +0800 Subject: [PATCH] refactor: modernize code with go fix --- goutils | 2 +- internal/entrypoint/entrypoint.go | 7 +++---- internal/homepage/homepage_test.go | 8 ++------ internal/homepage/icons/url_test.go | 10 +++------- internal/metrics/period/handler.go | 6 +----- internal/route/rules/do_set_test.go | 9 ++------- internal/route/rules/vars_static.go | 6 +++--- internal/serialization/reader.go | 3 +-- .../serialization/validation_common_test.go | 5 ----- .../serialization/validation_mismatch_test.go | 14 +++++++------- .../serialization/validation_string_ptr_test.go | 17 ++++++----------- .../serialization/validation_string_test.go | 6 +++--- 12 files changed, 32 insertions(+), 61 deletions(-) diff --git a/goutils b/goutils index 0dbc3718..8849086b 160000 --- a/goutils +++ b/goutils @@ -1 +1 @@ -Subproject commit 0dbc371839f3c682a752c107ebeddcfce243e7d7 +Subproject commit 8849086bc03f380ef782e1f1903ba8cdcde5ab75 diff --git a/internal/entrypoint/entrypoint.go b/internal/entrypoint/entrypoint.go index d54c1442..40bd7c07 100644 --- a/internal/entrypoint/entrypoint.go +++ b/internal/entrypoint/entrypoint.go @@ -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 } diff --git a/internal/homepage/homepage_test.go b/internal/homepage/homepage_test.go index 0cad0fe9..fc762885 100644 --- a/internal/homepage/homepage_test.go +++ b/internal/homepage/homepage_test.go @@ -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, }, } diff --git a/internal/homepage/icons/url_test.go b/internal/homepage/icons/url_test.go index 5bfbd3a5..1ae21245 100644 --- a/internal/homepage/icons/url_test.go +++ b/internal/homepage/icons/url_test.go @@ -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, }, }, diff --git a/internal/metrics/period/handler.go b/internal/metrics/period/handler.go index c20ec5ee..0fe3bebd 100644 --- a/internal/metrics/period/handler.go +++ b/internal/metrics/period/handler.go @@ -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) }) diff --git a/internal/route/rules/do_set_test.go b/internal/route/rules/do_set_test.go index 21b20108..3d861e41 100644 --- a/internal/route/rules/do_set_test.go +++ b/internal/route/rules/do_set_test.go @@ -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) } } diff --git a/internal/route/rules/vars_static.go b/internal/route/rules/vars_static.go index cf27fd24..0c4b82ac 100644 --- a/internal/route/rules/vars_static.go +++ b/internal/route/rules/vars_static.go @@ -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 } diff --git a/internal/serialization/reader.go b/internal/serialization/reader.go index 7a4c4167..6cdfd025 100644 --- a/internal/serialization/reader.go +++ b/internal/serialization/reader.go @@ -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 diff --git a/internal/serialization/validation_common_test.go b/internal/serialization/validation_common_test.go index 739a3f4c..f04503cb 100644 --- a/internal/serialization/validation_common_test.go +++ b/internal/serialization/validation_common_test.go @@ -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 diff --git a/internal/serialization/validation_mismatch_test.go b/internal/serialization/validation_mismatch_test.go index a46f65a8..2aa19661 100644 --- a/internal/serialization/validation_mismatch_test.go +++ b/internal/serialization/validation_mismatch_test.go @@ -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 { diff --git a/internal/serialization/validation_string_ptr_test.go b/internal/serialization/validation_string_ptr_test.go index 4d6a58f2..9f201e12 100644 --- a/internal/serialization/validation_string_ptr_test.go +++ b/internal/serialization/validation_string_ptr_test.go @@ -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 -} diff --git a/internal/serialization/validation_string_test.go b/internal/serialization/validation_string_test.go index 89e3773e..85085d82 100644 --- a/internal/serialization/validation_string_test.go +++ b/internal/serialization/validation_string_test.go @@ -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 {