2023-01-09 15:37:44 -05:00
|
|
|
package repodb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DetailedRepoMeta is a auxiliary structure used for sorting RepoMeta arrays by information
|
|
|
|
// that's not directly available in the RepoMetadata structure (ex. that needs to be calculated
|
|
|
|
// by iterating the manifests, etc.)
|
|
|
|
type DetailedRepoMeta struct {
|
|
|
|
RepoMeta RepoMetadata
|
2023-03-22 12:31:53 -05:00
|
|
|
Rank int
|
2023-01-09 15:37:44 -05:00
|
|
|
Downloads int
|
|
|
|
UpdateTime time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func SortFunctions() map[SortCriteria]func(pageBuffer []DetailedRepoMeta) func(i, j int) bool {
|
|
|
|
return map[SortCriteria]func(pageBuffer []DetailedRepoMeta) func(i, j int) bool{
|
|
|
|
AlphabeticAsc: SortByAlphabeticAsc,
|
|
|
|
AlphabeticDsc: SortByAlphabeticDsc,
|
|
|
|
Relevance: SortByRelevance,
|
|
|
|
UpdateTime: SortByUpdateTime,
|
|
|
|
Downloads: SortByDownloads,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SortByAlphabeticAsc(pageBuffer []DetailedRepoMeta) func(i, j int) bool {
|
|
|
|
return func(i, j int) bool {
|
|
|
|
return pageBuffer[i].RepoMeta.Name < pageBuffer[j].RepoMeta.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SortByAlphabeticDsc(pageBuffer []DetailedRepoMeta) func(i, j int) bool {
|
|
|
|
return func(i, j int) bool {
|
|
|
|
return pageBuffer[i].RepoMeta.Name > pageBuffer[j].RepoMeta.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SortByRelevance(pageBuffer []DetailedRepoMeta) func(i, j int) bool {
|
|
|
|
return func(i, j int) bool {
|
2023-03-22 12:31:53 -05:00
|
|
|
return pageBuffer[i].Rank < pageBuffer[j].Rank
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SortByUpdateTime sorting descending by time.
|
|
|
|
func SortByUpdateTime(pageBuffer []DetailedRepoMeta) func(i, j int) bool {
|
|
|
|
return func(i, j int) bool {
|
|
|
|
return pageBuffer[i].UpdateTime.After(pageBuffer[j].UpdateTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SortByDownloads returns a comparison function for descendant sorting by downloads.
|
|
|
|
func SortByDownloads(pageBuffer []DetailedRepoMeta) func(i, j int) bool {
|
|
|
|
return func(i, j int) bool {
|
|
|
|
return pageBuffer[i].Downloads > pageBuffer[j].Downloads
|
|
|
|
}
|
|
|
|
}
|