From 78cf6f5eae2489a9dd3ab63fc0ce598f35194dc7 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Tue, 13 Jan 2015 12:33:45 +0200 Subject: [PATCH] Add ability to reset command state. Fixes #57. --- command.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/command.go b/command.go index 68c438c9..4636fd9c 100644 --- a/command.go +++ b/command.go @@ -34,6 +34,8 @@ type Cmder interface { writeTimeout() *time.Duration readTimeout() *time.Duration + // Reset resets internal state of the command. + Reset() Err() error String() string } @@ -117,6 +119,11 @@ func NewCmd(args ...string) *Cmd { } } +func (cmd *Cmd) Reset() { + cmd.val = nil + cmd.err = nil +} + func (cmd *Cmd) Val() interface{} { return cmd.val } @@ -148,6 +155,11 @@ func NewSliceCmd(args ...string) *SliceCmd { } } +func (cmd *SliceCmd) Reset() { + cmd.val = nil + cmd.err = nil +} + func (cmd *SliceCmd) Val() []interface{} { return cmd.val } @@ -184,6 +196,11 @@ func NewStatusCmd(args ...string) *StatusCmd { } } +func (cmd *StatusCmd) Reset() { + cmd.val = "" + cmd.err = nil +} + func (cmd *StatusCmd) Val() string { return cmd.val } @@ -220,6 +237,11 @@ func NewIntCmd(args ...string) *IntCmd { } } +func (cmd *IntCmd) Reset() { + cmd.val = 0 + cmd.err = nil +} + func (cmd *IntCmd) Val() int64 { return cmd.val } @@ -258,6 +280,11 @@ func NewDurationCmd(precision time.Duration, args ...string) *DurationCmd { } } +func (cmd *DurationCmd) Reset() { + cmd.val = 0 + cmd.err = nil +} + func (cmd *DurationCmd) Val() time.Duration { return cmd.val } @@ -294,6 +321,11 @@ func NewBoolCmd(args ...string) *BoolCmd { } } +func (cmd *BoolCmd) Reset() { + cmd.val = false + cmd.err = nil +} + func (cmd *BoolCmd) Val() bool { return cmd.val } @@ -330,6 +362,11 @@ func NewStringCmd(args ...string) *StringCmd { } } +func (cmd *StringCmd) Reset() { + cmd.val = "" + cmd.err = nil +} + func (cmd *StringCmd) Val() string { return cmd.val } @@ -387,6 +424,11 @@ func NewFloatCmd(args ...string) *FloatCmd { } } +func (cmd *FloatCmd) Reset() { + cmd.val = 0 + cmd.err = nil +} + func (cmd *FloatCmd) Val() float64 { return cmd.val } @@ -419,6 +461,11 @@ func NewStringSliceCmd(args ...string) *StringSliceCmd { } } +func (cmd *StringSliceCmd) Reset() { + cmd.val = nil + cmd.err = nil +} + func (cmd *StringSliceCmd) Val() []string { return cmd.val } @@ -455,6 +502,11 @@ func NewBoolSliceCmd(args ...string) *BoolSliceCmd { } } +func (cmd *BoolSliceCmd) Reset() { + cmd.val = nil + cmd.err = nil +} + func (cmd *BoolSliceCmd) Val() []bool { return cmd.val } @@ -491,6 +543,11 @@ func NewStringStringMapCmd(args ...string) *StringStringMapCmd { } } +func (cmd *StringStringMapCmd) Reset() { + cmd.val = nil + cmd.err = nil +} + func (cmd *StringStringMapCmd) Val() map[string]string { return cmd.val } @@ -563,6 +620,11 @@ func NewZSliceCmd(args ...string) *ZSliceCmd { } } +func (cmd *ZSliceCmd) Reset() { + cmd.val = nil + cmd.err = nil +} + func (cmd *ZSliceCmd) Val() []Z { return cmd.val } @@ -600,6 +662,12 @@ func NewScanCmd(args ...string) *ScanCmd { } } +func (cmd *ScanCmd) Reset() { + cmd.cursor = 0 + cmd.keys = nil + cmd.err = nil +} + func (cmd *ScanCmd) Val() (int64, []string) { return cmd.cursor, cmd.keys }