forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting.go
67 lines (55 loc) · 1.76 KB
/
sorting.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package query
import (
"fmt"
"strings"
)
// IsAsc returns true if sort criteria has ascending sort order, otherwise false.
func (c SortCriteria) IsAsc() bool {
return c.Order == SortCriteria_ASC
}
// IsDesc returns true if sort criteria has descending sort order, otherwise false.
func (c SortCriteria) IsDesc() bool {
return c.Order == SortCriteria_DESC
}
// GoString implements fmt.GoStringer interface
// return string representation of a sort criteria in next form:
// "<tag_name> (ASC|DESC)".
func (c SortCriteria) GoString() string {
return fmt.Sprintf("%s %s", c.Tag, c.Order)
}
// ParseSorting parses raw string that represent sort criteria into a Sorting
// data structure.
// Provided string is supposed to be in accordance with the sorting collection
// operator from REST API Syntax.
// See: https://github.com/infobloxopen/atlas-app-toolkit#sorting
func ParseSorting(s string) (*Sorting, error) {
var sorting Sorting
for _, craw := range strings.Split(s, ",") {
v := strings.Fields(craw)
var c SortCriteria
switch len(v) {
case 1:
c.Tag, c.Order = v[0], SortCriteria_ASC
case 2:
if o, ok := SortCriteria_Order_value[strings.ToUpper(v[1])]; !ok {
return nil, fmt.Errorf("invalid sort order - %q in %q", v[1], craw)
} else {
c.Tag, c.Order = v[0], SortCriteria_Order(o)
}
default:
return nil, fmt.Errorf("invalid sort criteria: %s", craw)
}
sorting.Criterias = append(sorting.Criterias, &c)
}
return &sorting, nil
}
// GoString implements fmt.GoStringer interface
// Returns string representation of sorting in next form:
// "<name> (ASC|DESC) [, <tag_name> (ASC|DESC)]"
func (s Sorting) GoString() string {
var l []string
for _, c := range s.GetCriterias() {
l = append(l, c.GoString())
}
return strings.Join(l, ", ")
}