-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.go
50 lines (44 loc) · 1.19 KB
/
sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package search
import "fmt"
// Sort
//
// Structures the sorting logic and API format in a programmatic
// interface so that sorting can be performed by developers
// unfamiliar with the meilisearch API.
type Sort struct {
Attribute string
Desc bool
}
// String
//
// Formats the Sort into a string sort
// supported by the meilisearch search engine
func (s *Sort) String() string {
// select order of sort default to ascending
order := "asc"
if s.Desc {
order = "desc"
}
return fmt.Sprintf("%s:%s", s.Attribute, order)
}
// SortGroup
//
// Merges multiple sorts together allowing for more complex sorting
// operations. Sort ordering is prioritized in order of their position
// in the Sorts slice. Lower indices correlate to a higher priority.
type SortGroup struct {
Sorts []Sort
}
// Format
//
// Formats the sort group into a slice of strings compatible
// with the meilisearch search engine API
func (sg *SortGroup) Format() []string {
// create slice to hold output
var result []string
// iterate sorts formatting them into their string format and appending to the result slice
for _, sort := range sg.Sorts {
result = append(result, sort.String())
}
return result
}