Skip to content

Commit 284dfc0

Browse files
Dean KarnDean Karn
authored andcommitted
add QueryParams() url.Values function for caching multiple access to URL.Query() Values
1 parent 130a621 commit 284dfc0

File tree

6 files changed

+55
-5
lines changed

6 files changed

+55
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
##LARS
22
<img align="right" src="https://github.com/raw/go-playground/lars/master/examples/README/test.gif">
3-
![Project status](https://img.shields.io/badge/version-3.3.1-green.svg)
3+
![Project status](https://img.shields.io/badge/version-3.4.0-green.svg)
44
[![Build Status](https://semaphoreci.com/api/v1/projects/4351aa2d-2f94-40be-a6ef-85c248490378/679708/badge.svg)](https://semaphoreci.com/joeybloggs/lars)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/lars/badge.svg?branch=master)](https://coveralls.io/github/go-playground/lars?branch=master)
66
[![Go Report Card](https://goreportcard.com/badge/go-playground/lars)](https://goreportcard.com/report/go-playground/lars)

context.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"net"
88
"net/http"
9+
"net/url"
910
"strings"
1011

1112
"github.com/gorilla/websocket"
@@ -74,6 +75,22 @@ func (c *Ctx) Param(name string) string {
7475
return blank
7576
}
7677

78+
// QueryParams returns the http.Request.URL.Query() values
79+
// this function is not for convenience, but rather performance
80+
// URL.Query() reparses the RawQuery every time it's called, but this
81+
// function will cache the initial parsing so it doesn't have to reparse;
82+
// which is useful if when accessing these Params from multiple middleware.
83+
func (c *Ctx) QueryParams() url.Values {
84+
85+
if c.queryParams != nil {
86+
return c.queryParams
87+
}
88+
89+
c.queryParams = c.request.URL.Query()
90+
91+
return c.queryParams
92+
}
93+
7794
// ParseForm calls the underlying http.Request ParseForm
7895
// but also adds the URL params to the request Form as if
7996
// they were defined as query params i.e. ?id=13&ok=true but

context_16.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package lars
55
import (
66
"io"
77
"net/http"
8+
"net/url"
89
"time"
910

1011
"github.com/gorilla/websocket"
@@ -18,6 +19,7 @@ type Context interface {
1819
Response() *Response
1920
WebSocket() *websocket.Conn
2021
Param(name string) string
22+
QueryParams() url.Values
2123
ParseForm() error
2224
ParseMultipartForm(maxMemory int64) error
2325
Set(key interface{}, value interface{})
@@ -57,6 +59,7 @@ type Ctx struct {
5759
response *Response
5860
websocket *websocket.Conn
5961
params Params
62+
queryParams url.Values
6063
handlers HandlersChain
6164
parent Context
6265
handlerName string
@@ -70,6 +73,7 @@ func (c *Ctx) RequestStart(w http.ResponseWriter, r *http.Request) {
7073
c.request = r
7174
c.response.reset(w)
7275
c.params = c.params[0:0]
76+
c.queryParams = nil
7377
c.netContext = context.Background() // in go 1.7 will call r.Context(), netContext will go away and be replaced with the Request objects Context
7478
c.index = -1
7579
c.handlers = nil

context_17.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"io"
88
"net/http"
9+
"net/url"
910
"time"
1011

1112
"github.com/gorilla/websocket"
@@ -18,6 +19,7 @@ type Context interface {
1819
Response() *Response
1920
WebSocket() *websocket.Conn
2021
Param(name string) string
22+
QueryParams() url.Values
2123
ParseForm() error
2224
ParseMultipartForm(maxMemory int64) error
2325
Set(key interface{}, value interface{})
@@ -56,6 +58,7 @@ type Ctx struct {
5658
response *Response
5759
websocket *websocket.Conn
5860
params Params
61+
queryParams url.Values
5962
handlers HandlersChain
6063
parent Context
6164
handlerName string
@@ -69,6 +72,7 @@ func (c *Ctx) RequestStart(w http.ResponseWriter, r *http.Request) {
6972
c.request = r
7073
c.response.reset(w)
7174
c.params = c.params[0:0]
75+
c.queryParams = nil
7276
c.index = -1
7377
c.handlers = nil
7478
c.formParsed = false

context_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,3 +723,28 @@ func TestText(t *testing.T) {
723723
Equal(t, w.Header().Get(ContentType), TextPlainCharsetUTF8)
724724
Equal(t, w.Body.String(), txtData)
725725
}
726+
727+
func TestCachedQueryParams(t *testing.T) {
728+
729+
var val1, val2 string
730+
731+
l := New()
732+
l.Use(func(c Context) {
733+
val1 = c.QueryParams()["key1"][0]
734+
735+
c.Next()
736+
})
737+
l.Get("/test", func(c Context) {
738+
val2 = c.QueryParams()["key2"][0]
739+
})
740+
741+
hf := l.Serve()
742+
743+
r, _ := http.NewRequest(GET, "/test?key1=val1&key2=val2", nil)
744+
w := httptest.NewRecorder()
745+
hf.ServeHTTP(w, r)
746+
747+
Equal(t, w.Code, http.StatusOK)
748+
Equal(t, val1, "val1")
749+
Equal(t, val2, "val2")
750+
}

lars_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestFindOneOffs(t *testing.T) {
7777
PanicMatches(t, func() { l.Get("/zombies/:id/", basicHandler) }, "handlers are already registered for path '/zombies/:id/'")
7878
}
7979

80-
func Testlars(t *testing.T) {
80+
func TestLARS(t *testing.T) {
8181
l := New()
8282

8383
l.Get("/", func(c Context) {
@@ -91,7 +91,7 @@ func Testlars(t *testing.T) {
9191
Equal(t, body, "home")
9292
}
9393

94-
func TestlarsStatic(t *testing.T) {
94+
func TestLARSStatic(t *testing.T) {
9595
l := New()
9696
path := "/github.com/go-playground/:id"
9797
l.Get(path, basicHandler)
@@ -100,7 +100,7 @@ func TestlarsStatic(t *testing.T) {
100100
Equal(t, body, "")
101101
}
102102

103-
func TestlarsParam(t *testing.T) {
103+
func TestLARSParam(t *testing.T) {
104104
l := New()
105105
path := "/github.com/go-playground/:id/"
106106
l.Get(path, func(c Context) {
@@ -115,7 +115,7 @@ func TestlarsParam(t *testing.T) {
115115
Equal(t, body, "808w70")
116116
}
117117

118-
func TestlarsTwoParam(t *testing.T) {
118+
func TestLARSTwoParam(t *testing.T) {
119119
var p1 string
120120
var p2 string
121121

0 commit comments

Comments
 (0)