-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathordering.go
51 lines (44 loc) · 893 Bytes
/
ordering.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
package query
import (
"github.com/pkg/errors"
)
// The ordering of a returned set of records
type Ordering uint
const (
Ascending Ordering = iota
Descending
)
func ToOrdering(val string) (Ordering, error) {
switch val {
case "asc":
return Ascending, nil
case "desc":
return Descending, nil
default:
return 0, errors.Errorf("unexpected value: %v", val)
}
}
func FromOrdering(val Ordering) (string, error) {
switch val {
case Ascending:
return "asc", nil
case Descending:
return "desc", nil
default:
return "", errors.Errorf("unexpected value: %v", val)
}
}
func ToOrderingWithFallback(val string, fallback Ordering) Ordering {
res, err := ToOrdering(val)
if err != nil {
return fallback
}
return res
}
func FromOrderingWithFallback(val Ordering, fallback string) string {
res, err := FromOrdering(val)
if err != nil {
return fallback
}
return res
}