From b5a101843a33d0cb579346b910c3ca092ba043db Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 19 Nov 2015 12:27:01 +0200 Subject: [PATCH] Added SetUint helper to Args --- args.go | 12 ++++++++++++ args_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/args.go b/args.go index 3e4b946050..765da308df 100644 --- a/args.go +++ b/args.go @@ -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. diff --git a/args_test.go b/args_test.go index 311dc3687e..c12c60c85e 100644 --- a/args_test.go +++ b/args_test.go @@ -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