-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add display sorting across most aggregators to support text, numeric, contextual, date-parsing, and by aggregated value. This commit breaks backwards compatibility by replacing --sort-key and --reverse in many aggregators with simply --sort=name
- Loading branch information
Showing
25 changed files
with
787 additions
and
174 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
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,95 @@ | ||
package helpers | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"rare/pkg/aggregation/sorting" | ||
"rare/pkg/logger" | ||
"rare/pkg/stringSplitter" | ||
"strings" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var DefaultSortFlag = &cli.StringFlag{ | ||
Name: "sort", | ||
Usage: "Sorting method for display (value, text, numeric, contextual, date)", | ||
Value: "numeric", | ||
} | ||
|
||
// Create a sort flag with a different default value | ||
func DefaultSortFlagWithDefault(dflt string) *cli.StringFlag { | ||
if _, err := lookupSorter(dflt); err != nil { | ||
panic(err) | ||
} | ||
|
||
flag := *DefaultSortFlag | ||
flag.Value = dflt | ||
return &flag | ||
} | ||
|
||
func BuildSorterOrFail(fullName string) sorting.NameValueSorter { | ||
sorter, err := BuildSorter(fullName) | ||
if err != nil { | ||
logger.Fatal(err) | ||
} | ||
return sorter | ||
} | ||
|
||
func BuildSorter(fullName string) (sorting.NameValueSorter, error) { | ||
name, reverse, err := parseSort(fullName) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing sort: %v", err) | ||
} | ||
|
||
sorter, err := lookupSorter(name) | ||
if err != nil { | ||
return nil, fmt.Errorf("unknown sort: %s", name) | ||
} | ||
if reverse { | ||
sorter = sorting.Reverse(sorter) | ||
} | ||
return sorter, nil | ||
} | ||
|
||
func parseSort(name string) (realname string, reverse bool, err error) { | ||
splitter := stringSplitter.Splitter{ | ||
S: name, | ||
Delim: ":", | ||
} | ||
|
||
realname = strings.ToLower(splitter.Next()) | ||
reverse = (realname == "value") // Value defaults descending | ||
|
||
if modifier, hasModifier := splitter.NextOk(); hasModifier { | ||
switch strings.ToLower(modifier) { | ||
case "rev", "reverse": | ||
reverse = !reverse | ||
case "desc": | ||
reverse = true | ||
case "asc": | ||
reverse = false | ||
default: | ||
return "", false, errors.New("invalid sort modifier") | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func lookupSorter(name string) (sorting.NameValueSorter, error) { | ||
name = strings.ToLower(name) | ||
switch name { | ||
case "text", "": | ||
return sorting.ValueNilSorter(sorting.ByName), nil | ||
case "numeric": | ||
return sorting.ValueNilSorter(sorting.ByNameSmart), nil | ||
case "contextual", "context": | ||
return sorting.ValueNilSorter(sorting.ByContextual()), nil | ||
case "date": | ||
return sorting.ValueNilSorter(sorting.ByDateWithContextual()), nil | ||
case "value": | ||
return sorting.ValueSorterEx(sorting.ByName), nil | ||
} | ||
return nil, errors.New("unknown sort") | ||
} |
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,93 @@ | ||
package helpers | ||
|
||
import ( | ||
"rare/pkg/aggregation/sorting" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBuildSorter(t *testing.T) { | ||
assert.NotNil(t, BuildSorterOrFail("text")) | ||
assert.NotNil(t, BuildSorterOrFail("numeric")) | ||
assert.NotNil(t, BuildSorterOrFail("contextual")) | ||
assert.NotNil(t, BuildSorterOrFail("value")) | ||
assert.NotNil(t, BuildSorterOrFail("value:reverse")) | ||
} | ||
|
||
func TestOrderResults(t *testing.T) { | ||
assertSortEquals(t, "text", 1, 4, 2, 0, 3) | ||
assertSortEquals(t, "text:asc", 1, 4, 2, 0, 3) | ||
assertSortEquals(t, "text:reverse", 3, 0, 2, 4, 1) | ||
assertSortEquals(t, "text:desc", 3, 0, 2, 4, 1) | ||
|
||
assertSortEquals(t, "numeric", 1, 4, 2, 0, 3) | ||
assertSortEquals(t, "numeric:asc", 1, 4, 2, 0, 3) | ||
assertSortEquals(t, "numeric:reverse", 3, 0, 2, 4, 1) | ||
assertSortEquals(t, "numeric:desc", 3, 0, 2, 4, 1) | ||
|
||
assertSortEquals(t, "value", 3, 2, 1, 0, 4) | ||
assertSortEquals(t, "value:desc", 3, 2, 1, 0, 4) | ||
assertSortEquals(t, "value:reverse", 4, 0, 1, 2, 3) | ||
assertSortEquals(t, "value:asc", 4, 0, 1, 2, 3) | ||
} | ||
|
||
func TestInvalidSortNames(t *testing.T) { | ||
sorter, err := BuildSorter("bla") | ||
assert.Nil(t, sorter) | ||
assert.Error(t, err) | ||
|
||
sorter, err = BuildSorter("numeric:bla") | ||
assert.Nil(t, sorter) | ||
assert.Error(t, err) | ||
} | ||
|
||
// Given a hardcoded set of values, and a sort name assert the order is as expected | ||
func assertSortEquals(t *testing.T, sortName string, order ...int) { | ||
sorter, err := BuildSorter(sortName) | ||
assert.NoError(t, err) | ||
|
||
type orderedPair struct { | ||
sorting.NameValuePair | ||
id int | ||
} | ||
|
||
vals := []orderedPair{ | ||
{sorting.NameValuePair{Name: "qef", Value: 5}, 0}, | ||
{sorting.NameValuePair{Name: "abc", Value: 12}, 1}, | ||
{sorting.NameValuePair{Name: "egf", Value: 52}, 2}, | ||
{sorting.NameValuePair{Name: "zac", Value: 52}, 3}, | ||
{sorting.NameValuePair{Name: "bbb", Value: 3}, 4}, | ||
} | ||
|
||
if len(order) != len(vals) { | ||
panic("bad test") | ||
} | ||
|
||
sorting.SortBy(vals, sorter, func(obj orderedPair) sorting.NameValuePair { | ||
return obj.NameValuePair | ||
}) | ||
|
||
for i := 0; i < len(vals); i++ { | ||
assert.Equal(t, order[i], vals[i].id) | ||
} | ||
|
||
} | ||
|
||
func TestDefaultSortResolves(t *testing.T) { | ||
sortName, _, err := parseSort(DefaultSortFlag.Value) | ||
assert.NoError(t, err) | ||
|
||
sorter, sorterErr := lookupSorter(sortName) | ||
assert.NoError(t, sorterErr) | ||
assert.NotNil(t, sorter) | ||
} | ||
|
||
func TestBuildSortFlag(t *testing.T) { | ||
flag := DefaultSortFlagWithDefault("contextual") | ||
assert.Equal(t, "contextual", flag.Value) | ||
|
||
assert.Panics(t, func() { | ||
DefaultSortFlagWithDefault("fake") | ||
}) | ||
} |
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
Oops, something went wrong.