-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsort.go
65 lines (53 loc) · 1.1 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package pagorminator
import (
"fmt"
"strings"
)
func NewSort(orders ...Order) Sort {
return orders
}
func Unsorted() Sort {
return Sort{}
}
type Sort []Order
func (s Sort) String() string {
orderStrings := make([]string, len(s))
for i, order := range s {
orderStrings[i] = order.String()
}
return strings.Join(orderStrings, ", ")
}
type Direction string
const (
ASC Direction = "asc"
DESC Direction = "desc"
)
func NewOrder(property string, direction Direction) (Order, error) {
if property == "" {
return Order{}, ErrOrderPropertyIsEmpty
}
if direction != "" && direction != ASC && direction != DESC {
return Order{}, ErrOrderDirectionNotValid{Direction: direction}
}
return Order{
property: property,
direction: direction,
}, nil
}
func MustNewOrder(property string, direction Direction) Order {
order, err := NewOrder(property, direction)
if err != nil {
panic(err)
}
return order
}
type Order struct {
property string
direction Direction
}
func (o Order) String() string {
if o.direction == "" {
return o.property
}
return fmt.Sprintf("%s %s", o.property, o.direction)
}