This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.go
110 lines (97 loc) · 2.66 KB
/
order.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package apiutil
import (
"fmt"
"net/http"
"net/url"
"strings"
)
var (
// OrderASC defines the ascending order keyword
OrderASC = "asc"
// OrderDESC defines the descending order keyword
OrderDESC = "desc"
)
// OrderBy represents ordering information
type OrderBy []Order
// Order represents ordering information for a single key
type Order struct {
Key string
Direction string
}
// NewOrder constructs a basic order struct
func NewOrder(key, orderDirection string) Order {
if orderDirection != OrderASC && orderDirection != OrderDESC {
orderDirection = OrderDESC
}
return Order{
Key: key,
Direction: orderDirection,
}
}
// String implements the stringer interface for OrderBy
func (o OrderBy) String() string {
var b strings.Builder
for i, oi := range o {
if i > 0 {
b.WriteString(",")
}
b.WriteString(oi.String())
}
return b.String()
}
// String implements the stringer interface for Order
func (o Order) String() string {
return fmt.Sprintf("%s,%s", o.Key, o.Direction)
}
// SetQueryParams adds order by info to a url as query parameters
func (o OrderBy) SetQueryParams(u *url.URL) *url.URL {
q := u.Query()
if len(o) > 0 {
q.Set("orderBy", o.String())
}
u.RawQuery = q.Encode()
return u
}
// OrderByFromRequest extracts orderBy params from an http request
func OrderByFromRequest(r *http.Request) OrderBy {
orderBy := r.FormValue("orderBy")
return NewOrderByFromString(orderBy, nil)
}
// OrderByFromRequestWithKeys extracts orderBy params from an
// http request and only takes the specified keys
func OrderByFromRequestWithKeys(r *http.Request, validKeys []string) OrderBy {
orderBy := r.FormValue("orderBy")
return NewOrderByFromString(orderBy, validKeys)
}
// NewOrderByFromString converts a commaa delimited string to an OrderBy struct
func NewOrderByFromString(orderBy string, validKeys []string) OrderBy {
orderComponents := strings.Split(orderBy, ",")
if orderBy == "" || len(orderComponents) == 0 || (len(orderComponents) != 1 && len(orderComponents)%2 == 1) {
return []Order{}
}
res := []Order{}
if len(orderComponents) == 1 {
res = append(res, NewOrder(strings.TrimSpace(orderComponents[0]), OrderDESC))
return res
}
for i := 0; i <= len(orderComponents)/2; i += 2 {
orderDirection := ""
if strings.ToLower(orderComponents[i+1]) == OrderASC {
orderDirection = OrderASC
} else {
orderDirection = OrderDESC
}
orderKey := strings.TrimSpace(orderComponents[i])
if validKeys != nil {
for _, vkey := range validKeys {
if vkey == orderKey {
res = append(res, NewOrder(orderKey, orderDirection))
break
}
}
} else {
res = append(res, NewOrder(orderKey, orderDirection))
}
}
return res
}