Enable overriding sort criteria of config aliases with --sort flags

pull/6/head
Mickaël Menu 4 years ago
parent c10449a425
commit a5b7639bcd
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -166,8 +166,11 @@ func SorterFromString(str string) (Sorter, error) {
// SortersFromStrings returns a list of Sorter from their string representation. // SortersFromStrings returns a list of Sorter from their string representation.
func SortersFromStrings(strs []string) ([]Sorter, error) { func SortersFromStrings(strs []string) ([]Sorter, error) {
sorters := make([]Sorter, 0) sorters := make([]Sorter, 0)
for _, str := range strs {
sorter, err := SorterFromString(str) // Iterates in reverse order to be able to override sort criteria set in a
// config alias with a `--sort` flag.
for i := len(strs) - 1; i >= 0; i-- {
sorter, err := SorterFromString(strs[i])
if err != nil { if err != nil {
return sorters, err return sorters, err
} }

@ -56,9 +56,12 @@ func TestSortersFromStrings(t *testing.T) {
{Field: SortCreated, Ascending: false}, {Field: SortCreated, Ascending: false},
}) })
test([]string{"c+", "title"}, []Sorter{ // It is parsed in reverse order to be able to override sort criteria set
{Field: SortCreated, Ascending: true}, // in aliases.
test([]string{"c+", "title", "random"}, []Sorter{
{Field: SortRandom, Ascending: true},
{Field: SortTitle, Ascending: true}, {Field: SortTitle, Ascending: true},
{Field: SortCreated, Ascending: true},
}) })
_, err := SortersFromStrings([]string{"c", "foobar"}) _, err := SortersFromStrings([]string{"c", "foobar"})

Loading…
Cancel
Save