This repository has been archived by the owner on Sep 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.go
145 lines (122 loc) · 3.3 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) KwanJunWen
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
package pgquery
import (
"encoding/json"
"errors"
"strings"
"github.com/go-pg/pg/v10/types"
)
// OrderDirection order direction enum type.
type OrderDirection int
const (
// OrderDirectionAsc order direction ascending enum.
OrderDirectionAsc OrderDirection = iota
// OrderDirectionDesc order direction descending enum.
OrderDirectionDesc
)
// String returns the string presentation for the order direction.
func (d OrderDirection) String() string {
return [...]string{"ASC", "DESC"}[d]
}
// Order order common sorter.
type Order struct {
column string
Direction *OrderDirection `json:"direction,omitempty"`
}
// MarshalJSON custom JSON marshaler.
func (s *Order) MarshalJSON() ([]byte, error) {
return json.Marshal(s.Direction.String())
}
// UnmarshalJSON custom JSON unmarshaler.
func (s *Order) UnmarshalJSON(b []byte) error {
type alias Order
m1 := struct {
Direction string `json:"direction,omitempty"`
*alias
}{alias: (*alias)(s)}
var m2 string
m3 := struct {
Direction int `json:"direction,omitempty"`
*alias
}{alias: (*alias)(s)}
var m4 int
if err := json.Unmarshal(b, &m1); err == nil {
d := strings.ToLower(m1.Direction)
if d == strings.ToLower(OrderDirectionAsc.String()) {
*s.Direction = OrderDirectionAsc
}
if d == strings.ToLower(OrderDirectionDesc.String()) {
*s.Direction = OrderDirectionDesc
}
return nil
}
if err := json.Unmarshal(b, &m2); err == nil {
d := strings.ToLower(m2)
if d == strings.ToLower(OrderDirectionAsc.String()) {
*s.Direction = OrderDirectionAsc
}
if d == strings.ToLower(OrderDirectionDesc.String()) {
*s.Direction = OrderDirectionDesc
}
return nil
}
if err := json.Unmarshal(b, &m3); err == nil {
d := OrderDirection(m3.Direction)
if d == OrderDirectionAsc {
*s.Direction = OrderDirectionAsc
}
if d == OrderDirectionDesc {
*s.Direction = OrderDirectionDesc
}
return nil
}
if err := json.Unmarshal(b, &m4); err == nil {
d := OrderDirection(m4)
if d == OrderDirectionAsc {
*s.Direction = OrderDirectionAsc
}
if d == OrderDirectionDesc {
*s.Direction = OrderDirectionDesc
}
return nil
}
return errors.New("[Order]: unsupported format when unmarshalling json")
}
// NewOrder initializes a new order sorter.
func NewOrder(column string) *Order {
return &Order{
column: column,
Direction: new(OrderDirection),
}
}
// NewOrderAsc initializes a new ascending order sorter.
func NewOrderAsc(column string) *Order {
o := NewOrder(column)
return o.Asc()
}
// NewOrderDesc initializes a new order sorter.
func NewOrderDesc(column string) *Order {
o := NewOrder(column)
return o.Desc()
}
// Column sets the column for the order sorter.
func (s *Order) Column(column string) *Order {
s.column = column
return s
}
// Asc sets the direction to ascending order.
func (s *Order) Asc() *Order {
*s.Direction = OrderDirectionAsc
return s
}
// Desc sets the direction to descending order.
func (s *Order) Desc() *Order {
*s.Direction = OrderDirectionDesc
return s
}
// Appender returns parameters for cond appender.
func (s *Order) Appender() (string, interface{}, interface{}) {
return "? ?", types.Ident(s.column), types.Safe(s.Direction.String())
}