Skip to content

Commit ba12479

Browse files
Dean KarnDean Karn
authored andcommitted
add another decode case for blank, "", for when you want only Query() params
1 parent 709d4f2 commit ba12479

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
##Pure
22
<img align="right" src="https://raw.githubusercontent.com/go-playground/pure/master/logo.png">
3-
![Project status](https://img.shields.io/badge/version-2.0.0-green.svg)
3+
![Project status](https://img.shields.io/badge/version-2.1.0-green.svg)
44
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/pure/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/pure)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pure/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pure?branch=master)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/pure)](https://goreportcard.com/report/github.com/go-playground/pure)

constants.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,6 @@ package pure
22

33
// HTTP Constant Terms and Variables
44
const (
5-
// // CONNECT HTTP method
6-
// CONNECT = "CONNECT"
7-
// // DELETE HTTP method
8-
// DELETE = "DELETE"
9-
// // GET HTTP method
10-
// GET = "GET"
11-
// // HEAD HTTP method
12-
// HEAD = "HEAD"
13-
// // OPTIONS HTTP method
14-
// OPTIONS = "OPTIONS"
15-
// // PATCH HTTP method
16-
// PATCH = "PATCH"
17-
// // POST HTTP method
18-
// POST = "POST"
19-
// // PUT HTTP method
20-
// PUT = "PUT"
21-
// // TRACE HTTP method
22-
// TRACE = "TRACE"
235

246
//-------------
257
// Media types
@@ -32,6 +14,7 @@ const (
3214
ApplicationXML = "application/xml"
3315
ApplicationXMLCharsetUTF8 = ApplicationXML + "; " + CharsetUTF8
3416
ApplicationForm = "application/x-www-form-urlencoded"
17+
ApplicationQueryParams = ""
3518
ApplicationProtobuf = "application/protobuf"
3619
ApplicationMsgpack = "application/msgpack"
3720
TextHTML = "text/html"

helpers.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,21 @@ func Decode(r *http.Request, includeFormQueryParams bool, maxMemory int64, v int
295295
err = DefaultDecoder.Decode(v, r.MultipartForm.Value)
296296
}
297297
}
298+
299+
case ApplicationQueryParams:
300+
301+
qp := r.URL.Query()
302+
303+
if rvi := r.Context().Value(defaultContextIdentifier); rvi != nil {
304+
305+
rv := rvi.(*requestVars)
306+
307+
for _, p := range rv.params {
308+
qp.Add(p.Key, p.Value)
309+
}
310+
}
311+
312+
err = DefaultDecoder.Decode(v, qp)
298313
}
299314
return
300315
}

helpers_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,30 @@ func TestDecode(t *testing.T) {
9292
err := Decode(r, true, 16<<10, test)
9393
Equal(t, err, nil)
9494
})
95+
p.Get("/parse-params/:Posted", func(w http.ResponseWriter, r *http.Request) {
96+
err := Decode(r, true, 16<<10, test)
97+
Equal(t, err, nil)
98+
})
9599

96100
hf := p.Serve()
97101

102+
r, _ := http.NewRequest(http.MethodGet, "/parse-params/pval?id=5", nil)
103+
w := httptest.NewRecorder()
104+
105+
hf.ServeHTTP(w, r)
106+
107+
Equal(t, w.Code, http.StatusOK)
108+
Equal(t, test.ID, 5)
109+
Equal(t, test.Posted, "pval")
110+
Equal(t, test.MultiPartPosted, "")
111+
98112
form := url.Values{}
99113
form.Add("Posted", "value")
100114

101-
r, _ := http.NewRequest(http.MethodPost, "/decode/14?id=13", strings.NewReader(form.Encode()))
115+
test = new(TestStruct)
116+
r, _ = http.NewRequest(http.MethodPost, "/decode/14?id=13", strings.NewReader(form.Encode()))
102117
r.Header.Set(ContentType, ApplicationForm)
103-
w := httptest.NewRecorder()
118+
w = httptest.NewRecorder()
104119

105120
hf.ServeHTTP(w, r)
106121

@@ -109,6 +124,7 @@ func TestDecode(t *testing.T) {
109124
Equal(t, test.Posted, "value")
110125
Equal(t, test.MultiPartPosted, "")
111126

127+
test = new(TestStruct)
112128
r, _ = http.NewRequest(http.MethodPost, "/decode/14", strings.NewReader(form.Encode()))
113129
r.Header.Set(ContentType, ApplicationForm)
114130
w = httptest.NewRecorder()

0 commit comments

Comments
 (0)