Repository navigation
Expand file tree
/
Copy pathquery_codec.go
More file actions
204 lines (182 loc) · 6.18 KB
/
Copy pathquery_codec.go
File metadata and controls
204 lines (182 loc) · 6.18 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package router
import (
"webtyp.com/fmt"
"webtyp.com/model"
)
const (
queryObjectSep = "."
hexDigits = "0123456789ABCDEF"
errQueryArray = "router: a query string cannot carry an array; send the arguments in a request body"
)
// PathOf returns the path of a request target, without its query and fragment ("/api/q?a=1#f" is "/api/q").
// It is what every transport's Context.Path answers.
//
// path := router.PathOf("/api/q?a=1") // "/api/q"
func PathOf(target string) string {
if idx := fmt.Index(target, string(queryFragment)); idx != -1 {
target = target[:idx]
}
if idx := fmt.Index(target, string(queryStart)); idx != -1 {
target = target[:idx]
}
return target
}
// QueryOf returns the query of a request path ("" when it has none), without the "?" and without any fragment.
//
// args.DecodeFields(router.QueryReader(router.QueryOf(ctx.Path())))
func QueryOf(path string) string {
if idx := fmt.Index(path, string(queryFragment)); idx != -1 {
path = path[:idx]
}
idx := fmt.Index(path, string(queryStart))
if idx == -1 {
return ""
}
return path[idx+1:]
}
// QueryEscape percent-encodes s for a query: every byte except A-Z a-z 0-9 - _ . ~ becomes %XX.
func QueryEscape(s string) string {
out := make([]byte, 0, len(s))
for i := 0; i < len(s); i++ {
c := s[i]
if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-' || c == '_' || c == '.' || c == '~' {
out = append(out, c)
continue
}
out = append(out, queryEscape, hexDigits[c>>4], hexDigits[c&15])
}
return string(out)
}
// QueryReader reads the fields of a record from a query ("a=b&c=d", without the "?"). A present key with no value is present and empty.
// An object field "filter" is read from the keys "filter.<field>". Arrays are not supported in a query: Array returns false.
func QueryReader(query string) model.FieldReader { return &queryReader{query: query} }
type queryReader struct {
query string
prefix string // "filter." while reading the object "filter"
}
func (q *queryReader) String(name string) (string, bool) { return QueryParam("?"+q.query, q.prefix+name) }
func (q *queryReader) Raw(name string) (string, bool) { return q.String(name) }
func (q *queryReader) Int(name string) (int64, bool) {
s, ok := q.String(name)
if !ok {
return 0, false
}
n, err := fmt.Convert(s).Int64()
if err != nil {
return 0, false
}
return n, true
}
func (q *queryReader) Float(name string) (float64, bool) {
s, ok := q.String(name)
if !ok {
return 0, false
}
f, err := fmt.Convert(s).Float64()
if err != nil {
return 0, false
}
return f, true
}
func (q *queryReader) Bool(name string) (bool, bool) {
s, ok := q.String(name)
if !ok {
return false, false
}
switch fmt.ToLower(s) {
case "true", "on", "1":
return true, true
case "false", "off", "0", "":
return false, true
}
return false, false
}
func (q *queryReader) Bytes(name string) ([]byte, bool) {
s, ok := q.String(name)
return []byte(s), ok
}
func (q *queryReader) Object(name string, into model.Decodable) bool {
prefix := q.prefix + name + queryObjectSep
found := false
for _, part := range fmt.Split(q.query, string(querySep)) {
if len(part) > len(prefix) && QueryUnescape(part[:len(prefix)]) == prefix {
found = true
break
}
}
if !found {
return false
}
into.DecodeFields(&queryReader{query: q.query, prefix: prefix})
return true
}
func (q *queryReader) Array(name string) (model.ArrayReader, bool) { return nil, false }
const methodGet = "GET"
// DecodeArgs reads the arguments of a request into into. The method decides where they travel: a GET carries them in its query
// and never in a body (a body on a GET is ignored); every other method carries them in its body, decoded by fromBody. It is the
// one place that policy lives; httpd and the edge both call it.
//
// err := router.DecodeArgs(ctx.Method(), router.QueryOf(ctx.Path()), ctx.Body(), &args, decodeJSON)
func DecodeArgs(method, query string, body []byte, into model.Decodable, fromBody func(body []byte, into model.Decodable) error) error {
if method == methodGet {
into.DecodeFields(QueryReader(query))
return nil
}
return fromBody(body, into)
}
// EncodeQuery writes the fields of v as a query ("a=b&c=d"), the counterpart of QueryReader. An object field is written as
// "name.<field>"; an array is an error (send a body instead). A Null field is left out.
//
// query, err := router.EncodeQuery(&crud.ReadArgs{Search: "12.345"})
func EncodeQuery(v model.Encodable) (string, error) {
w := &queryWriter{}
v.EncodeFields(w)
if w.failed {
return "", fmt.Err(errQueryArray)
}
return string(w.out), nil
}
type queryWriter struct {
out []byte
prefix string
failed bool
}
func (w *queryWriter) put(name, val string) {
if len(w.out) > 0 {
w.out = append(w.out, querySep)
}
w.out = append(w.out, QueryEscape(w.prefix+name)...)
w.out = append(w.out, queryEq)
w.out = append(w.out, QueryEscape(val)...)
}
func (w *queryWriter) String(name, val string) { w.put(name, val) }
func (w *queryWriter) Raw(name, val string) { w.put(name, val) }
func (w *queryWriter) Int(name string, val int64) { w.put(name, fmt.Convert(val).String()) }
func (w *queryWriter) Float(name string, val float64) { w.put(name, fmt.Convert(val).String()) }
func (w *queryWriter) Bool(name string, val bool) {
if val {
w.put(name, "true")
return
}
w.put(name, "false")
}
func (w *queryWriter) Bytes(name string, val []byte) { w.put(name, string(val)) }
func (w *queryWriter) Null(name string) {}
func (w *queryWriter) Object(name string, val model.Encodable) {
inner := &queryWriter{out: w.out, prefix: w.prefix + name + queryObjectSep}
val.EncodeFields(inner)
w.out, w.failed = inner.out, w.failed || inner.failed
}
func (w *queryWriter) Array(name string, n int) model.ArrayWriter {
w.failed = true
return discardArray{}
}
// discardArray swallows the elements of an array the query refused.
type discardArray struct{}
func (discardArray) String(string) {}
func (discardArray) Int(int64) {}
func (discardArray) Float(float64) {}
func (discardArray) Bool(bool) {}
func (discardArray) Bytes([]byte) {}
func (discardArray) Object(model.Encodable) {}
func (discardArray) Close() {}