Skip to content

refactor: replace sort.Interface with slices.SortFunc in file browser - #8

Closed
ryans-posthog wants to merge 1 commit into
masterfrom
modernize-fileserver-sort
Closed

ryans-posthog wants to merge 1 commit into
masterfrom
modernize-fileserver-sort

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 27 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

…browse

Replace the four custom sort types (byName, byNameDirFirst, bySize,
byTime) and their Len/Swap/Less boilerplate with slices.SortFunc,
which is more idiomatic in modern Go. This also removes the "sort"
import since slices was already in use.

Generated-By: PostHog Code
Task-Id: 92686f54-3905-423a-8b7f-e9f9f93755ed

@ryans-posthog ryans-posthog left a comment

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.

Fix your lint issues and make this significantly more readable.

Comment on lines +240 to +260
cmp = func(a, b fileInfo) int {
const directoryOffset = -1 << 31 // = -math.MinInt32
aSize, bSize := a.Size, b.Size
if a.IsDir {
aSize = directoryOffset
}
if b.IsDir {
bSize = directoryOffset
}
if a.IsDir && b.IsDir {
return strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))
}
switch {
case aSize < bSize:
return -1
case aSize > bSize:
return 1
default:
return 0
}
}

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?

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