-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
123 lines (102 loc) · 2.85 KB
/
query.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
package farm
import (
"net/http"
"net/url"
"strings"
"github.com/SimonRichardson/coherence/pkg/selectors"
"github.com/pkg/errors"
)
const (
defaultContentType = "application/json"
)
// KeyParams represents a parameter that has a selectors.Key
type KeyParams interface {
Key() selectors.Key
}
// FieldParams represents a object that has a selectors.Field
type FieldParams interface {
Field() selectors.Field
}
// KeyQueryParams defines all the dimensions of a query.
type KeyQueryParams struct {
key selectors.Key
quorum selectors.Quorum
}
// Key returns the key value from the parameters
func (qp KeyQueryParams) Key() selectors.Key {
return qp.key
}
// DecodeFrom populates a KeyQueryParams from a URL.
func (qp *KeyQueryParams) DecodeFrom(u *url.URL, h http.Header, rb queryBehavior) error {
if rb == queryRequired {
if contentType := h.Get("Content-Type"); rb == queryRequired && strings.ToLower(contentType) != "application/json" {
return errors.Errorf("expected 'application/json' content-type, got %q", contentType)
}
}
key := u.Query().Get("key")
if key == "" {
return errors.Errorf("expected 'key' but got %q", key)
}
qp.key = selectors.Key(key)
var (
err error
quorum = u.Query().Get("quorum")
)
if quorum != "" {
if qp.quorum, err = selectors.ParseQuorum(quorum); err != nil {
return errors.Errorf("expected 'quorum' but got %q", quorum)
}
} else {
qp.quorum = selectors.Strong
}
return nil
}
// KeyFieldQueryParams defines all the dimensions of a query.
type KeyFieldQueryParams struct {
key selectors.Key
field selectors.Field
quorum selectors.Quorum
}
// Key returns the key value from the parameters
func (qp KeyFieldQueryParams) Key() selectors.Key {
return qp.key
}
// Field returns the field value from the parameters
func (qp KeyFieldQueryParams) Field() selectors.Field {
return qp.field
}
// DecodeFrom populates a KeyFieldQueryParams from a URL.
func (qp *KeyFieldQueryParams) DecodeFrom(u *url.URL, h http.Header, rb queryBehavior) error {
if rb == queryRequired {
if contentType := h.Get("Content-Type"); rb == queryRequired && strings.ToLower(contentType) != "application/json" {
return errors.Errorf("expected 'application/json' content-type, got %q", contentType)
}
}
key := u.Query().Get("key")
if key == "" {
return errors.Errorf("expected 'key' but got %q", key)
}
qp.key = selectors.Key(key)
field := u.Query().Get("field")
if field == "" {
return errors.Errorf("expected 'field' but got %q", field)
}
qp.field = selectors.Field(field)
var (
err error
quorum = u.Query().Get("quorum")
)
if quorum != "" {
if qp.quorum, err = selectors.ParseQuorum(quorum); err != nil {
return errors.Errorf("expected 'quorum' but got %q", quorum)
}
} else {
qp.quorum = selectors.Strong
}
return nil
}
type queryBehavior int
const (
queryRequired queryBehavior = iota
queryOptional
)