Skip to content

Commit 71312a3

Browse files
committed
added URLParams type for upcoming router tree
1 parent 17b990a commit 71312a3

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

request.go

+37
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ type Request struct {
7171
Header http.Header
7272

7373
// PathParams value is URL path parameters.
74+
// Will be DEPRECATED in upcoming v0.12.0, use URLParams instead.
7475
PathParams PathParams
7576

77+
// URLParams value is URL path parameters.
78+
URLParams URLParams
79+
7680
// Referer value is HTTP 'Referrer' (or 'Referer') header.
7781
Referer string
7882

@@ -277,6 +281,7 @@ func (r *Request) Reset() {
277281
r.Path = ""
278282
r.Header = nil
279283
r.PathParams = nil
284+
r.URLParams = nil
280285
r.Referer = ""
281286
r.UserAgent = ""
282287
r.IsGzipAccepted = false
@@ -294,6 +299,38 @@ func (r *Request) cleanupMutlipart() {
294299
}
295300
}
296301

302+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
303+
// URLParam
304+
//___________________________________
305+
306+
// URLParam struct holds single URL parameter value.
307+
type URLParam struct {
308+
Key string
309+
Value string
310+
}
311+
312+
// URLParams type is slice of type URLParam.
313+
type URLParams []URLParam
314+
315+
// Get method returns the value for the given key otherwise empty string.
316+
func (u URLParams) Get(key string) string {
317+
for i := range u {
318+
if u[i].Key == key {
319+
return u[i].Value
320+
}
321+
}
322+
return ""
323+
}
324+
325+
// ToMap method returns URL parameters in type map.
326+
func (u URLParams) ToMap() map[string]string {
327+
ps := make(map[string]string)
328+
for _, p := range u {
329+
ps[p.Key] = p.Value
330+
}
331+
return ps
332+
}
333+
297334
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
298335
// PathParams
299336
//___________________________________

request_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,28 @@ func TestRequestSaveFileForExistingFile(t *testing.T) {
263263
assert.Equal(t, int64(0), size)
264264
}
265265

266+
func TestURLParams(t *testing.T) {
267+
params := URLParams{
268+
{
269+
Key: "test1",
270+
Value: "value1",
271+
},
272+
{
273+
Key: "test2",
274+
Value: "value2",
275+
},
276+
{
277+
Key: "test3",
278+
Value: "value3",
279+
},
280+
}
281+
282+
assert.Equal(t, 3, len(params))
283+
assert.Equal(t, "value2", params.Get("test2"))
284+
assert.Equal(t, "", params.Get("not-exists"))
285+
assert.Equal(t, map[string]string{"test1": "value1", "test2": "value2", "test3": "value3"}, params.ToMap())
286+
}
287+
266288
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
267289
// test unexported methods
268290
//___________________________________

version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
package ahttp
66

77
// Version no. of aah framework ahttp library
8-
const Version = "0.11.2"
8+
const Version = "0.11.3"

0 commit comments

Comments
 (0)