Skip to content

Commit

Permalink
add QueryParams() url.Values function for caching multiple access to …
Browse files Browse the repository at this point in the history
…URL.Query() Values
  • Loading branch information
Dean Karn authored and Dean Karn committed Aug 31, 2016
1 parent 130a621 commit 284dfc0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
##LARS
<img align="right" src="https://raw.githubusercontent.com/go-playground/lars/master/examples/README/test.gif">
![Project status](https://img.shields.io/badge/version-3.3.1-green.svg)
![Project status](https://img.shields.io/badge/version-3.4.0-green.svg)
[![Build Status](https://semaphoreci.com/api/v1/projects/4351aa2d-2f94-40be-a6ef-85c248490378/679708/badge.svg)](https://semaphoreci.com/joeybloggs/lars)
[![Coverage Status](https://coveralls.io/repos/github/go-playground/lars/badge.svg?branch=master)](https://coveralls.io/github/go-playground/lars?branch=master)
[![Go Report Card](https://goreportcard.com/badge/go-playground/lars)](https://goreportcard.com/report/go-playground/lars)
Expand Down
17 changes: 17 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net"
"net/http"
"net/url"
"strings"

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

// QueryParams returns the http.Request.URL.Query() values
// this function is not for convenience, but rather performance
// URL.Query() reparses the RawQuery every time it's called, but this
// function will cache the initial parsing so it doesn't have to reparse;
// which is useful if when accessing these Params from multiple middleware.
func (c *Ctx) QueryParams() url.Values {

if c.queryParams != nil {
return c.queryParams
}

c.queryParams = c.request.URL.Query()

return c.queryParams
}

// ParseForm calls the underlying http.Request ParseForm
// but also adds the URL params to the request Form as if
// they were defined as query params i.e. ?id=13&ok=true but
Expand Down
4 changes: 4 additions & 0 deletions context_16.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package lars
import (
"io"
"net/http"
"net/url"
"time"

"github.com/gorilla/websocket"
Expand All @@ -18,6 +19,7 @@ type Context interface {
Response() *Response
WebSocket() *websocket.Conn
Param(name string) string
QueryParams() url.Values
ParseForm() error
ParseMultipartForm(maxMemory int64) error
Set(key interface{}, value interface{})
Expand Down Expand Up @@ -57,6 +59,7 @@ type Ctx struct {
response *Response
websocket *websocket.Conn
params Params
queryParams url.Values
handlers HandlersChain
parent Context
handlerName string
Expand All @@ -70,6 +73,7 @@ func (c *Ctx) RequestStart(w http.ResponseWriter, r *http.Request) {
c.request = r
c.response.reset(w)
c.params = c.params[0:0]
c.queryParams = nil
c.netContext = context.Background() // in go 1.7 will call r.Context(), netContext will go away and be replaced with the Request objects Context
c.index = -1
c.handlers = nil
Expand Down
4 changes: 4 additions & 0 deletions context_17.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"io"
"net/http"
"net/url"
"time"

"github.com/gorilla/websocket"
Expand All @@ -18,6 +19,7 @@ type Context interface {
Response() *Response
WebSocket() *websocket.Conn
Param(name string) string
QueryParams() url.Values
ParseForm() error
ParseMultipartForm(maxMemory int64) error
Set(key interface{}, value interface{})
Expand Down Expand Up @@ -56,6 +58,7 @@ type Ctx struct {
response *Response
websocket *websocket.Conn
params Params
queryParams url.Values
handlers HandlersChain
parent Context
handlerName string
Expand All @@ -69,6 +72,7 @@ func (c *Ctx) RequestStart(w http.ResponseWriter, r *http.Request) {
c.request = r
c.response.reset(w)
c.params = c.params[0:0]
c.queryParams = nil
c.index = -1
c.handlers = nil
c.formParsed = false
Expand Down
25 changes: 25 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,28 @@ func TestText(t *testing.T) {
Equal(t, w.Header().Get(ContentType), TextPlainCharsetUTF8)
Equal(t, w.Body.String(), txtData)
}

func TestCachedQueryParams(t *testing.T) {

var val1, val2 string

l := New()
l.Use(func(c Context) {
val1 = c.QueryParams()["key1"][0]

c.Next()
})
l.Get("/test", func(c Context) {
val2 = c.QueryParams()["key2"][0]
})

hf := l.Serve()

r, _ := http.NewRequest(GET, "/test?key1=val1&key2=val2", nil)
w := httptest.NewRecorder()
hf.ServeHTTP(w, r)

Equal(t, w.Code, http.StatusOK)
Equal(t, val1, "val1")
Equal(t, val2, "val2")
}
8 changes: 4 additions & 4 deletions lars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestFindOneOffs(t *testing.T) {
PanicMatches(t, func() { l.Get("/zombies/:id/", basicHandler) }, "handlers are already registered for path '/zombies/:id/'")
}

func Testlars(t *testing.T) {
func TestLARS(t *testing.T) {
l := New()

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

func TestlarsStatic(t *testing.T) {
func TestLARSStatic(t *testing.T) {
l := New()
path := "/github.com/go-playground/:id"
l.Get(path, basicHandler)
Expand All @@ -100,7 +100,7 @@ func TestlarsStatic(t *testing.T) {
Equal(t, body, "")
}

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

func TestlarsTwoParam(t *testing.T) {
func TestLARSTwoParam(t *testing.T) {
var p1 string
var p2 string

Expand Down

0 comments on commit 284dfc0

Please sign in to comment.