Skip to content

Commit

Permalink
Added SetUint helper to Args
Browse files Browse the repository at this point in the history
  • Loading branch information
valyala committed Nov 19, 2015
1 parent ed68dfc commit b5a1018
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
12 changes: 12 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ func (a *Args) GetUint(key string) (int, error) {
return ParseUint(value)
}

// SetUint sets uint value for the given key.
func (a *Args) SetUint(key string, value int) {
a.bufKV.key = AppendBytesStr(a.bufKV.key[:0], key)
a.SetUintBytes(a.bufKV.key, value)
}

// SetUintBytes sets uint value for the given key.
func (a *Args) SetUintBytes(key []byte, value int) {
a.bufKV.value = AppendUint(a.bufKV.value[:0], value)
a.SetBytesKV(key, a.bufKV.value)
}

// GetUintOrZero returns uint value for the given key.
//
// Zero (0) is returned on error.
Expand Down
33 changes: 33 additions & 0 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,39 @@ import (
"testing"
)

func TestArgsUint(t *testing.T) {
var a Args
a.SetUint("foo", 123)
a.SetUint("bar", 0)
a.SetUint("aaaa", 34566)

expectedS := "foo=123&bar=0&aaaa=34566"
s := string(a.AppendBytes(nil))
if s != expectedS {
t.Fatalf("unexpected args %q. Expecting %q", s, expectedS)
}

if a.GetUintOrZero("foo") != 123 {
t.Fatalf("unexpected arg value %d. Expecting %d", a.GetUintOrZero("foo"), 123)
}
if a.GetUintOrZero("bar") != 0 {
t.Fatalf("unexpected arg value %d. Expecting %d", a.GetUintOrZero("bar"), 0)
}
if a.GetUintOrZero("aaaa") != 34566 {
t.Fatalf("unexpected arg value %d. Expecting %d", a.GetUintOrZero("aaaa"), 34566)
}

if string(a.Peek("foo")) != "123" {
t.Fatalf("unexpected arg value %q. Expecting %q", a.Peek("foo"), "123")
}
if string(a.Peek("bar")) != "0" {
t.Fatalf("unexpected arg value %q. Expecting %q", a.Peek("bar"), "0")
}
if string(a.Peek("aaaa")) != "34566" {
t.Fatalf("unexpected arg value %q. Expecting %q", a.Peek("aaaa"), "34566")
}
}

func TestArgsCopyTo(t *testing.T) {
var a Args

Expand Down

0 comments on commit b5a1018

Please sign in to comment.