-
Notifications
You must be signed in to change notification settings - Fork 8
/
fields.go
72 lines (62 loc) · 1.56 KB
/
fields.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
package qstring
import (
"errors"
"fmt"
"strings"
"time"
)
// parseOperator parses a leading logical operator out of the provided string
func parseOperator(s string) string {
switch s[0] {
case 60: // "<"
switch s[1] {
case 61: // "="
return "<="
default:
return "<"
}
case 62: // ">"
switch s[1] {
case 61: // "="
return ">="
default:
return ">"
}
default:
// no operator found, default to "="
return "="
}
}
// ComparativeTime is a field that can be used for specifying a query parameter
// which includes a conditional operator and a timestamp
type ComparativeTime struct {
Operator string
Time time.Time
}
// NewComparativeTime returns a new ComparativeTime instance with a default
// operator of "="
func NewComparativeTime() *ComparativeTime {
return &ComparativeTime{Operator: "="}
}
// Parse is used to parse a query string into a ComparativeTime instance
func (c *ComparativeTime) Parse(query string) error {
if len(query) <= 2 {
return errors.New("qstring: Invalid Timestamp Query")
}
c.Operator = parseOperator(query)
// if no operator was provided and we defaulted to an equality operator
if !strings.HasPrefix(query, c.Operator) {
query = fmt.Sprintf("=%s", query)
}
var err error
c.Time, err = time.Parse(time.RFC3339, query[len(c.Operator):])
if err != nil {
return err
}
return nil
}
// String returns this ComparativeTime instance in the form of the query
// parameter that it came in on
func (c ComparativeTime) String() string {
return fmt.Sprintf("%s%s", c.Operator, c.Time.Format(time.RFC3339))
}