Skip to content

refactor: replace custom sort types with slices.SortFunc#13

Open
ryans-posthog wants to merge 2 commits intomasterfrom
refactor/use-slices-sortfunc-for-browse
Open

refactor: replace custom sort types with slices.SortFunc#13
ryans-posthog wants to merge 2 commits intomasterfrom
refactor/use-slices-sortfunc-for-browse

Conversation

@ryans-posthog
Copy link
Copy Markdown
Owner

Summary

  • Replace four custom sort types (byName, byNameDirFirst, bySize, byTime) and their Len/Swap/Less boilerplate with slices.SortFunc calls
  • Remove the "sort" import since slices was already in use
  • Net reduction of ~38 lines while preserving identical sorting behavior

Test plan

  • Verify go build ./modules/caddyhttp/fileserver/... compiles cleanly
  • Verify go test ./modules/caddyhttp/fileserver/... passes
  • Verify go test ./caddytest/... integration tests pass
  • Manual: browse a file listing and confirm sort by name, size, time work in both asc/desc order

Replace four custom sort types (byName, byNameDirFirst, bySize, byTime)
and their Len/Swap/Less boilerplate with slices.SortFunc calls. Remove
the "sort" import since slices was already in use.

Generated-By: PostHog Code
Task-Id: ebf74917-a9d2-4455-b94a-776bbdb1036b
Copy link
Copy Markdown
Owner Author

@ryans-posthog ryans-posthog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address the feedback, this fix appears to add more complexity to the visual understanding of the code.

Comment on lines +225 to +258
case sortByName:
slices.SortFunc(l.Items, func(a, b fileInfo) int {
return cmp.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))
})
case sortByNameDirFirst:
slices.SortFunc(l.Items, func(a, b fileInfo) int {
if a.IsDir != b.IsDir {
if a.IsDir {
return -1
}
return 1
}
return cmp.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))
})
case sortBySize:
slices.SortFunc(l.Items, func(a, b fileInfo) int {
const directoryOffset = -1 << 31
aSize, bSize := a.Size, b.Size
if a.IsDir {
aSize = directoryOffset
}
if b.IsDir {
bSize = directoryOffset
}
if a.IsDir && b.IsDir {
return cmp.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))
}
return cmp.Compare(aSize, bSize)
})
case sortByTime:
slices.SortFunc(l.Items, func(a, b fileInfo) int {
return a.ModTime.Compare(b.ModTime)
})
}
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new functions are much more unreadable than the previous ones. How many lines were saved if you remove the boilerplate lines of function definitions?

Move sort comparison logic into named package-level functions
(compareByName, compareByNameDirFirst, compareBySize, compareByTime)
so applySortAndLimit reads cleanly as a simple switch statement.

Generated-By: PostHog Code
Task-Id: ebf74917-a9d2-4455-b94a-776bbdb1036b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant