Skip to content

Commit

Permalink
Changed Params attr to a function call
Browse files Browse the repository at this point in the history
  • Loading branch information
Ric Allinson committed Oct 6, 2015
1 parent 4b01a16 commit 62b2005
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
19 changes: 15 additions & 4 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ type Request struct {
Protocol string
// Check if a TLS connection is established. This is a short-hand for: "https" == req.Protocol
Secure bool
// This property is a slice containing properties mapped to the named route "parameters".
// This property is a map containing properties mapped to the named route "parameters".
// For example if you have the route "/user/:name", then the "name" property is available
// to you as req.params["name"]. This object defaults to {}.
Params map[string]string
params map[string]string
// The currently matched Route containing several properties such as the
// route's original path string, the regexp generated, and so on.
Route *Route
Expand All @@ -81,7 +81,6 @@ func CreateRequest(raw *http.Request, app *Application) *Request {
this.Xhr = this.Header.Get("X-Requested-With") == "XMLHttpRequest"
this.Protocol = this.URL.Scheme
this.Secure = this.Protocol == "https"
this.Params = map[string]string{}
this.app = app
// This could have been set by middleware so check if it's empty.
if this.Map == nil {
Expand Down Expand Up @@ -167,7 +166,7 @@ func (this *Request) Queries() map[string]string {
func (this *Request) Param(n string) string {
var v string
var ok bool
v, ok = this.Params[n]
v, ok = this.Params()[n]
if ok {
return v
}
Expand All @@ -182,6 +181,18 @@ func (this *Request) Param(n string) string {
return ""
}

// Returns a map containing properties mapped to the named route "parameters".
// If a map is passed in it will replace the current map.
func (this *Request) Params(p ...map[string]string) map[string]string {
if this.params == nil {
this.params = map[string]string{}
}
if len(p) > 0 {
this.params = p[0]
}
return this.params
}

// Return the value for the given key if found in the request files.
func (this *Request) File(key string) interface{} {
return this.Files()[key]
Expand Down
6 changes: 3 additions & 3 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestRequest(t *testing.T) {
Describe("Param()", func() {

It("should return [bar]", func() {
req.Params = map[string]string{"foo": "bar"}
req.params = map[string]string{"foo": "bar"}
r := req.Param("foo")
AssertEqual(r, "bar")
})
Expand All @@ -96,7 +96,7 @@ func TestRequest(t *testing.T) {
})

It("should return [bar]", func() {
req.Params = map[string]string{"foo": "bar"}
req.params = map[string]string{"foo": "bar"}
req.bodies = map[string]string{"foo": "bar1"}
req.queries = map[string]string{"foo": "bar2"}
r := req.Param("foo")
Expand All @@ -111,7 +111,7 @@ func TestRequest(t *testing.T) {
})

It("should return []", func() {
req.Params = map[string]string{"foo": "bar"}
req.params = map[string]string{"foo": "bar"}
req.bodies = map[string]string{"foo": "bar1"}
req.queries = map[string]string{"foo": "bar2"}
r := req.Param("bar")
Expand Down
4 changes: 2 additions & 2 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (this *Router) AddRoute(verb string, path string, funcs ...func(*Request, *
// Calls the function defined for each key in "req.Params" if available.
func (this *Router) executeParamFuncs(req *Request, res *Response, next func()) bool {
// Call each "param" function if one is set.
for name := range req.Params {
for name := range req.Params() {
if pfn, ok := this.ParamFuncs[name]; ok {
pfn(req, res, next)
// If the response has been "closed" by a "param" function then return.
Expand Down Expand Up @@ -69,7 +69,7 @@ func (this *Router) handle(req *Request, res *Response, next func()) {
// If the route matches use it.
if params, ok := route.Match(req.Method, path); ok {
// Set the route "params" found in the path.
req.Params = params
req.Params(params)
// Set the matched route.
req.Route = route
// Call "param" functions if they are defined.
Expand Down

0 comments on commit 62b2005

Please sign in to comment.