-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cache: provide a generic bug sorting function
- Loading branch information
1 parent
e3c445f
commit 0728c00
Showing
5 changed files
with
89 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cache | ||
|
||
import "sort" | ||
|
||
type OrderBy int | ||
|
||
const ( | ||
_ OrderBy = iota | ||
OrderById | ||
OrderByCreation | ||
OrderByEdit | ||
) | ||
|
||
type OrderDirection int | ||
|
||
const ( | ||
_ OrderDirection = iota | ||
OrderAscending | ||
OrderDescending | ||
) | ||
|
||
func (c *RepoCache) AllBugsId(order OrderBy, direction OrderDirection) []string { | ||
if order == OrderById { | ||
return c.orderIds(direction) | ||
} | ||
|
||
excerpts := c.allExcerpt() | ||
|
||
var sorter sort.Interface | ||
|
||
switch order { | ||
case OrderByCreation: | ||
sorter = BugsByCreationTime(excerpts) | ||
case OrderByEdit: | ||
sorter = BugsByEditTime(excerpts) | ||
default: | ||
panic("missing sort type") | ||
} | ||
|
||
if direction == OrderDescending { | ||
sorter = sort.Reverse(sorter) | ||
} | ||
|
||
sort.Sort(sorter) | ||
|
||
result := make([]string, len(excerpts)) | ||
|
||
for i, val := range excerpts { | ||
result[i] = val.Id | ||
} | ||
|
||
return result | ||
} | ||
|
||
func (c *RepoCache) orderIds(direction OrderDirection) []string { | ||
result := make([]string, len(c.excerpts)) | ||
|
||
i := 0 | ||
for key := range c.excerpts { | ||
result[i] = key | ||
i++ | ||
} | ||
|
||
var sorter sort.Interface = sort.StringSlice(result) | ||
|
||
if direction == OrderDescending { | ||
sorter = sort.Reverse(sorter) | ||
} | ||
|
||
sort.Sort(sorter) | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters