1
0
mirror of https://github.com/rancher/steve.git synced 2025-09-01 15:37:31 +00:00

Add sorting to partition store

Extend the partition store to parse the "sort" query parameter as a
sorting condition. Dot notation is used to denote the object field.
Preceding the key with "-" denotes descending (reverse) order.

Example sorting by name:

GET /v1/secrets?sort=metadata.name

Reverse sorting by name:

GET /v1/secrets?sort=-metadata.name

All values are converted to strings and sorted lexicographically.
This commit is contained in:
Colleen Murphy
2022-10-27 14:11:17 -07:00
parent f8eaa11d83
commit adecbd9122
4 changed files with 447 additions and 0 deletions

View File

@@ -278,6 +278,85 @@ func TestList(t *testing.T) {
},
},
},
{
name: "with sorting",
apiOps: []*types.APIRequest{
newRequest("sort=metadata.name"),
newRequest("sort=-metadata.name"),
},
partitions: []Partition{
mockPartition{
name: "all",
},
},
objects: map[string]*unstructured.UnstructuredList{
"all": {
Items: []unstructured.Unstructured{
newApple("fuji").Unstructured,
newApple("granny-smith").Unstructured,
newApple("bramley").Unstructured,
newApple("crispin").Unstructured,
},
},
},
want: []types.APIObjectList{
{
Objects: []types.APIObject{
newApple("bramley").toObj(),
newApple("crispin").toObj(),
newApple("fuji").toObj(),
newApple("granny-smith").toObj(),
},
},
{
Objects: []types.APIObject{
newApple("granny-smith").toObj(),
newApple("fuji").toObj(),
newApple("crispin").toObj(),
newApple("bramley").toObj(),
},
},
},
},
{
name: "multi-partition sort=metadata.name",
apiOps: []*types.APIRequest{
newRequest("sort=metadata.name"),
},
partitions: []Partition{
mockPartition{
name: "green",
},
mockPartition{
name: "yellow",
},
},
objects: map[string]*unstructured.UnstructuredList{
"pink": {
Items: []unstructured.Unstructured{
newApple("fuji").Unstructured,
},
},
"green": {
Items: []unstructured.Unstructured{
newApple("granny-smith").Unstructured,
},
},
"yellow": {
Items: []unstructured.Unstructured{
newApple("crispin").Unstructured,
},
},
},
want: []types.APIObjectList{
{
Objects: []types.APIObject{
newApple("crispin").toObj(),
newApple("granny-smith").toObj(),
},
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {