diff --git a/README.md b/README.md index fa5ba01..1e3de49 100644 --- a/README.md +++ b/README.md @@ -3,14 +3,14 @@

HTTP extension library by aah framework

-

Build Status Code Coverage Go Report Card Release Version Godoc Twitter @aahframework

+

Build Status Code Coverage Go Report Card Release Version Godoc Twitter @aahframework

HTTP extension Library is used to handle/process Request and Response (headers, body, gzip, etc). ### News - * `v0.11.2` [released](https://github.com/go-aah/ahttp/releases/latest) and tagged on Jul 27, 2018. + * `v0.11.3` [released](https://github.com/go-aah/ahttp/releases/latest) and tagged on Aug 15, 2018. ## Installation diff --git a/request.go b/request.go index af33ab8..199fd36 100644 --- a/request.go +++ b/request.go @@ -71,8 +71,12 @@ type Request struct { Header http.Header // PathParams value is URL path parameters. + // Will be DEPRECATED in upcoming v0.12.0, URLParams will take place. PathParams PathParams + // URLParams value is URL path parameters. + URLParams URLParams + // Referer value is HTTP 'Referrer' (or 'Referer') header. Referer string @@ -197,6 +201,9 @@ func (r *Request) URL() *url.URL { // PathValue method returns value for given Path param key otherwise empty string. // For eg.: /users/:userId => PathValue("userId") func (r *Request) PathValue(key string) string { + if v := r.URLParams.Get(key); len(v) > 0 { + return v + } return r.PathParams.Get(key) } @@ -277,6 +284,7 @@ func (r *Request) Reset() { r.Path = "" r.Header = nil r.PathParams = nil + r.URLParams = nil r.Referer = "" r.UserAgent = "" r.IsGzipAccepted = false @@ -294,6 +302,38 @@ func (r *Request) cleanupMutlipart() { } } +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +// URLParam +//___________________________________ + +// URLParam struct holds single URL parameter value. +type URLParam struct { + Key string + Value string +} + +// URLParams type is slice of type URLParam. +type URLParams []URLParam + +// Get method returns the value for the given key otherwise empty string. +func (u URLParams) Get(key string) string { + for i := range u { + if u[i].Key == key { + return u[i].Value + } + } + return "" +} + +// ToMap method returns URL parameters in type map. +func (u URLParams) ToMap() map[string]string { + ps := make(map[string]string) + for _, p := range u { + ps[p.Key] = p.Value + } + return ps +} + //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ // PathParams //___________________________________ diff --git a/request_test.go b/request_test.go index 1e7f408..32d223d 100644 --- a/request_test.go +++ b/request_test.go @@ -263,6 +263,28 @@ func TestRequestSaveFileForExistingFile(t *testing.T) { assert.Equal(t, int64(0), size) } +func TestURLParams(t *testing.T) { + params := URLParams{ + { + Key: "test1", + Value: "value1", + }, + { + Key: "test2", + Value: "value2", + }, + { + Key: "test3", + Value: "value3", + }, + } + + assert.Equal(t, 3, len(params)) + assert.Equal(t, "value2", params.Get("test2")) + assert.Equal(t, "", params.Get("not-exists")) + assert.Equal(t, map[string]string{"test1": "value1", "test2": "value2", "test3": "value3"}, params.ToMap()) +} + //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ // test unexported methods //___________________________________ diff --git a/version.go b/version.go index 139c039..925900d 100644 --- a/version.go +++ b/version.go @@ -5,4 +5,4 @@ package ahttp // Version no. of aah framework ahttp library -const Version = "0.11.2" +const Version = "0.11.3"