From 70ee5a73990e52ac9a7aca75773c4be346f0f794 Mon Sep 17 00:00:00 2001 From: Ben Garrett Date: Wed, 31 Jan 2024 20:27:42 +1100 Subject: [PATCH] Refactor SortContent and DeleteDupe functions --- handler/app/app.go | 4 ++-- handler/app/app_test.go | 6 +++++- internal/cache/cache.go | 2 +- internal/helper/string.go | 2 +- internal/helper/string_test.go | 8 ++++---- model/fix/fix.go | 10 ++++------ model/scener.go | 2 +- 7 files changed, 18 insertions(+), 16 deletions(-) diff --git a/handler/app/app.go b/handler/app/app.go index a6bc1c8e..e8bf4278 100644 --- a/handler/app/app.go +++ b/handler/app/app.go @@ -747,7 +747,7 @@ func ReadmeSug(filename, group string, content ...string) string { if len(finds) == 1 { return finds[0] } - finds = SortContent(finds) + finds = SortContent(finds...) // match either the filename or the group name with a priority extension // e.g. .nfo, .txt, .unp, .doc @@ -839,7 +839,7 @@ func SafeHTML(s string) template.HTML { // SortContent sorts the content list by the number of slashes in each string. // It prioritizes strings with fewer slashes (i.e., closer to the root). // If the number of slashes is the same, it sorts alphabetically. -func SortContent(content []string) []string { +func SortContent(content ...string) []string { sort.Slice(content, func(i, j int) bool { // Fix any Windows path separators content[i] = strings.ReplaceAll(content[i], "\\", "/") diff --git a/handler/app/app_test.go b/handler/app/app_test.go index 36f9a9af..44045112 100644 --- a/handler/app/app_test.go +++ b/handler/app/app_test.go @@ -113,6 +113,10 @@ func TestSortContent(t *testing.T) { content []string expected []string }{ + { + content: nil, + expected: nil, + }, { content: []string{ "dir1/file1", @@ -150,7 +154,7 @@ func TestSortContent(t *testing.T) { copy(originalContent, tt.content) // Sort the content using the SortContent function - sortedContent := app.SortContent(tt.content) + sortedContent := app.SortContent(tt.content...) for i, v := range sortedContent { assert.Equal(t, tt.expected[i], v) diff --git a/internal/cache/cache.go b/internal/cache/cache.go index cb59b85d..3fd2b1b5 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -13,7 +13,7 @@ import ( type Cache int const ( - Pouet Cache = iota // pouet data cache + Pouet Cache = iota // data cache for the Pouet website, API requests Test // test cache ) diff --git a/internal/helper/string.go b/internal/helper/string.go index 1f567a4f..7c98f048 100644 --- a/internal/helper/string.go +++ b/internal/helper/string.go @@ -68,7 +68,7 @@ func Capitalize(s string) string { // DeleteDupe removes duplicate strings from a slice. // The returned slice is sorted and compacted. -func DeleteDupe(s []string) []string { +func DeleteDupe(s ...string) []string { slices.Sort(s) s = slices.Compact(s) x := make([]string, 0, len(s)) diff --git a/internal/helper/string_test.go b/internal/helper/string_test.go index 7f271eab..98baac33 100644 --- a/internal/helper/string_test.go +++ b/internal/helper/string_test.go @@ -50,13 +50,13 @@ func TestCapitalize(t *testing.T) { } func TestDeleteDupe(t *testing.T) { - s := helper.DeleteDupe([]string{}) + s := helper.DeleteDupe(nil...) assert.EqualValues(t, []string{}, s) - s = helper.DeleteDupe([]string{"a"}) + s = helper.DeleteDupe([]string{"a"}...) assert.EqualValues(t, []string{"a"}, s) - s = helper.DeleteDupe([]string{"a", "b", "abcde"}) + s = helper.DeleteDupe([]string{"a", "b", "abcde"}...) assert.EqualValues(t, []string{"a", "abcde", "b"}, s) // sorted - s = helper.DeleteDupe([]string{"a", "b", "a"}) + s = helper.DeleteDupe([]string{"a", "b", "a"}...) assert.EqualValues(t, []string{"a", "b"}, s) } diff --git a/model/fix/fix.go b/model/fix/fix.go index a18a7e55..9d6975ce 100644 --- a/model/fix/fix.go +++ b/model/fix/fix.go @@ -16,22 +16,20 @@ import ( "github.com/volatiletech/sqlboiler/v4/queries/qm" ) -var ( - ErrDB = errors.New("database connection is nil") -) +var ErrDB = errors.New("database connection is nil") -// Repair the database data. +// Repair a column or type of data within the database. type Repair int const ( None Repair = iota - 1 // None does nothing. All // All repairs all the repairable data. - Releaser // Releaser repairs the releaser data. + Releaser // Releaser focuses on the releaser data using the group_brand_by and group_brand_for columns. ) // In the future we may want to add a Debug or TestRun func. -// Run the repair. +// Run the database repair based on the repair option. func (r Repair) Run(ctx context.Context, w io.Writer, db *sql.DB) error { if w == nil { w = io.Discard diff --git a/model/scener.go b/model/scener.go index 0d71fcac..0b54fddb 100644 --- a/model/scener.go +++ b/model/scener.go @@ -123,5 +123,5 @@ func (s Sceners) Sort() []string { for _, scener := range s { sceners = append(sceners, strings.Split(string(scener.Name), ",")...) } - return helper.DeleteDupe(sceners) + return helper.DeleteDupe(sceners...) }