Skip to content

Commit

Permalink
feat: Make using NewSortOrder optional
Browse files Browse the repository at this point in the history
Allow passing sort.Ascending directly to the read options.
Also allow to chain calls:
sort.Ascending("field1").Descending("field2")
  • Loading branch information
efirs committed Apr 24, 2023
1 parent 1fc4b09 commit 8c966fd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
4 changes: 2 additions & 2 deletions search/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type RequestBuilder interface {
WithFilter(filter.Filter) RequestBuilder
WithFacetFields(fields ...string) RequestBuilder
WithFacet(*FacetQuery) RequestBuilder
WithSorting(sortByFields ...sort.Sort) RequestBuilder
WithSorting(sortByFields ...sort.Order) RequestBuilder
WithSortOrder(sortOrder sort.Order) RequestBuilder
WithIncludeFields(fields ...string) RequestBuilder
WithExcludeFields(fields ...string) RequestBuilder
Expand Down Expand Up @@ -107,7 +107,7 @@ func (r *Request) WithFacet(facet *FacetQuery) RequestBuilder {
return r
}

func (r *Request) WithSorting(sortByFields ...sort.Sort) RequestBuilder {
func (r *Request) WithSorting(sortByFields ...sort.Order) RequestBuilder {
r.Sort = sort.NewSortOrder(sortByFields...)

return r
Expand Down
27 changes: 20 additions & 7 deletions sort/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,32 @@ type Sort interface {
}

// Ascending builds an increasing order for given field name.
func Ascending(fieldName string) Sort {
return &fieldSort{fieldName: fieldName, operator: asc}
func Ascending(fieldName string) Order {
return Order{&fieldSort{fieldName: fieldName, operator: asc}}
}

// Descending builds a decreasing order for given field name.
func Descending(fieldName string) Sort {
return &fieldSort{fieldName: fieldName, operator: desc}
func Descending(fieldName string) Order {
return Order{&fieldSort{fieldName: fieldName, operator: desc}}
}

// Ascending builds an increasing order for given field name.
func (o Order) Ascending(fieldName string) Order {
return append(o, &fieldSort{fieldName: fieldName, operator: asc})
}

// Descending builds a decreasing order for given field name.
func (o Order) Descending(fieldName string) Order {
return append(o, &fieldSort{fieldName: fieldName, operator: desc})
}

// NewSortOrder creates an array of multiple fields that will be used to sort results.
func NewSortOrder(sort ...Sort) Order {
o := make(Order, len(sort))
copy(o, sort)
func NewSortOrder(sort ...Order) Order {
o := make(Order, 0, len(sort))
for _, v := range sort {
o = append(o, v...)
}

return o
}

Expand Down
15 changes: 11 additions & 4 deletions sort/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import (

func TestAscending(t *testing.T) {
s := Ascending("field_1")
assert.Equal(t, "field_1", s.FieldName())
assert.Equal(t, map[string]string{"field_1": "$asc"}, s.ToSortOrder())
assert.Equal(t, "field_1", s[0].FieldName())
assert.Equal(t, map[string]string{"field_1": "$asc"}, s[0].ToSortOrder())
}

func TestDescending(t *testing.T) {
s := Descending("field_1")
assert.Equal(t, "field_1", s.FieldName())
assert.Equal(t, map[string]string{"field_1": "$desc"}, s.ToSortOrder())
assert.Equal(t, "field_1", s[0].FieldName())
assert.Equal(t, map[string]string{"field_1": "$desc"}, s[0].ToSortOrder())
}

func TestNewSortOrder(t *testing.T) {
Expand All @@ -46,6 +46,13 @@ func TestNewSortOrder(t *testing.T) {
assert.Equal(t, map[string]string{"field_1": "$desc"}, o[0].ToSortOrder())
assert.Equal(t, map[string]string{"parent.field_2": "$asc"}, o[1].ToSortOrder())
})

t.Run("multiple sort orders", func(t *testing.T) {
o := Descending("field_1").Ascending("parent.field_2")
assert.Len(t, o, 2)
assert.Equal(t, map[string]string{"field_1": "$desc"}, o[0].ToSortOrder())
assert.Equal(t, map[string]string{"parent.field_2": "$asc"}, o[1].ToSortOrder())
})
}

func TestExpr_Built(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions tigris/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ func TestCollection(t *testing.T) {
Limit: 111,
Skip: 222,
Offset: []byte("333"),
Sort: []byte("[{\"Key1\":\"$asc\"}]"),
Sort: []byte("[{\"Key1\":\"$asc\"},{\"Key2\":\"$desc\"}]"),
},
).Return(mit, nil)
_, err := c.ReadWithOptions(ctx, filter.All,
Expand All @@ -758,7 +758,7 @@ func TestCollection(t *testing.T) {
Limit: 111,
Skip: 222,
Offset: []byte("333"),
Sort: sort.NewSortOrder(sort.Ascending("Key1")),
Sort: sort.Ascending("Key1").Descending("Key2"),
},
)
require.NoError(t, err)
Expand Down

0 comments on commit 8c966fd

Please sign in to comment.