Skip to content

Commit a893682

Browse files
iann0036ianlancetaylor
authored andcommitted
net/url: add Values.Has
Adds a method within Values for detecting whether a query parameter is set. Fixes #45100 Change-Id: I6bb49417e8547e11cc7e8d55c5211d24ee436ec1 GitHub-Last-Rev: 0b27cda GitHub-Pull-Request: #45835 Reviewed-on: https://go-review.googlesource.com/c/go/+/314850 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Trust: Cherry Zhang <[email protected]>
1 parent 3366556 commit a893682

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/net/url/url.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,12 @@ func (v Values) Del(key string) {
909909
delete(v, key)
910910
}
911911

912+
// Has checks whether a given key is set.
913+
func (v Values) Has(key string) bool {
914+
_, ok := v[key]
915+
return ok
916+
}
917+
912918
// ParseQuery parses the URL-encoded query string and returns
913919
// a map listing the values specified for each key.
914920
// ParseQuery always returns a non-nil map containing all the

src/net/url/url_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,10 +1295,10 @@ func TestResolveReference(t *testing.T) {
12951295
}
12961296

12971297
func TestQueryValues(t *testing.T) {
1298-
u, _ := Parse("http://x.com?foo=bar&bar=1&bar=2")
1298+
u, _ := Parse("http://x.com?foo=bar&bar=1&bar=2&baz")
12991299
v := u.Query()
1300-
if len(v) != 2 {
1301-
t.Errorf("got %d keys in Query values, want 2", len(v))
1300+
if len(v) != 3 {
1301+
t.Errorf("got %d keys in Query values, want 3", len(v))
13021302
}
13031303
if g, e := v.Get("foo"), "bar"; g != e {
13041304
t.Errorf("Get(foo) = %q, want %q", g, e)
@@ -1313,6 +1313,18 @@ func TestQueryValues(t *testing.T) {
13131313
if g, e := v.Get("baz"), ""; g != e {
13141314
t.Errorf("Get(baz) = %q, want %q", g, e)
13151315
}
1316+
if h, e := v.Has("foo"), true; h != e {
1317+
t.Errorf("Has(foo) = %t, want %t", h, e)
1318+
}
1319+
if h, e := v.Has("bar"), true; h != e {
1320+
t.Errorf("Has(bar) = %t, want %t", h, e)
1321+
}
1322+
if h, e := v.Has("baz"), true; h != e {
1323+
t.Errorf("Has(baz) = %t, want %t", h, e)
1324+
}
1325+
if h, e := v.Has("noexist"), false; h != e {
1326+
t.Errorf("Has(noexist) = %t, want %t", h, e)
1327+
}
13161328
v.Del("bar")
13171329
if g, e := v.Get("bar"), ""; g != e {
13181330
t.Errorf("second Get(bar) = %q, want %q", g, e)

0 commit comments

Comments
 (0)