redis/v2/req.go

338 lines
5.9 KiB
Go
Raw Normal View History

2013-07-02 15:17:31 +04:00
package redis
import (
"fmt"
"strconv"
"strings"
2013-09-11 20:22:10 +04:00
"time"
2013-07-02 15:17:31 +04:00
)
type Req interface {
Args() []string
ParseReply(reader) (interface{}, error)
SetErr(error)
Err() error
SetVal(interface{})
IfaceVal() interface{}
2013-09-11 20:22:10 +04:00
writeTimeout() *time.Duration
readTimeout() *time.Duration
2013-07-02 15:17:31 +04:00
}
//------------------------------------------------------------------------------
2013-07-02 15:21:54 +04:00
type baseReq struct {
2013-07-02 15:17:31 +04:00
args []string
val interface{}
err error
2013-09-11 20:22:10 +04:00
_writeTimeout, _readTimeout *time.Duration
2013-07-02 15:17:31 +04:00
}
2013-07-02 15:21:54 +04:00
func newBaseReq(args ...string) *baseReq {
return &baseReq{
2013-07-02 15:17:31 +04:00
args: args,
}
}
2013-07-02 15:21:54 +04:00
func (r *baseReq) Args() []string {
2013-07-02 15:17:31 +04:00
return r.args
}
2013-07-02 15:21:54 +04:00
func (r *baseReq) SetErr(err error) {
2013-07-02 15:17:31 +04:00
if err == nil {
panic("non-nil value expected")
}
r.err = err
}
2013-07-02 15:21:54 +04:00
func (r *baseReq) Err() error {
2013-07-02 15:17:31 +04:00
if r.err != nil {
return r.err
}
if r.val == nil {
return errValNotSet
}
return nil
}
2013-07-02 15:21:54 +04:00
func (r *baseReq) SetVal(val interface{}) {
2013-07-02 15:17:31 +04:00
if val == nil {
panic("non-nil value expected")
}
r.val = val
}
2013-07-02 15:21:54 +04:00
func (r *baseReq) IfaceVal() interface{} {
2013-07-02 15:17:31 +04:00
return r.val
}
2013-07-02 15:21:54 +04:00
func (r *baseReq) ParseReply(rd reader) (interface{}, error) {
2013-07-02 15:17:31 +04:00
return parseReply(rd)
}
2013-07-02 15:21:54 +04:00
func (r *baseReq) String() string {
2013-07-02 15:17:31 +04:00
args := strings.Join(r.args, " ")
if r.err != nil {
return args + ": " + r.err.Error()
} else if r.val != nil {
return args + ": " + fmt.Sprint(r.val)
}
return args
}
2013-09-11 20:22:10 +04:00
func (r *baseReq) readTimeout() *time.Duration {
return r._readTimeout
}
func (r *baseReq) setReadTimeout(d time.Duration) {
r._readTimeout = &d
}
func (r *baseReq) writeTimeout() *time.Duration {
return r._writeTimeout
}
func (r *baseReq) setWriteTimeout(d time.Duration) {
r._writeTimeout = &d
}
2013-07-02 15:17:31 +04:00
//------------------------------------------------------------------------------
type IfaceReq struct {
2013-07-02 15:21:54 +04:00
*baseReq
2013-07-02 15:17:31 +04:00
}
func NewIfaceReq(args ...string) *IfaceReq {
return &IfaceReq{
2013-07-02 15:21:54 +04:00
baseReq: newBaseReq(args...),
2013-07-02 15:17:31 +04:00
}
}
func (r *IfaceReq) Val() interface{} {
return r.val
}
//------------------------------------------------------------------------------
type StatusReq struct {
2013-07-02 15:21:54 +04:00
*baseReq
2013-07-02 15:17:31 +04:00
}
func NewStatusReq(args ...string) *StatusReq {
return &StatusReq{
2013-07-02 15:21:54 +04:00
baseReq: newBaseReq(args...),
2013-07-02 15:17:31 +04:00
}
}
func (r *StatusReq) Val() string {
if r.val == nil {
return ""
}
return r.val.(string)
}
//------------------------------------------------------------------------------
type IntReq struct {
2013-07-02 15:21:54 +04:00
*baseReq
2013-07-02 15:17:31 +04:00
}
func NewIntReq(args ...string) *IntReq {
return &IntReq{
2013-07-02 15:21:54 +04:00
baseReq: newBaseReq(args...),
2013-07-02 15:17:31 +04:00
}
}
func (r *IntReq) Val() int64 {
if r.val == nil {
return 0
}
return r.val.(int64)
}
//------------------------------------------------------------------------------
type BoolReq struct {
2013-07-02 15:21:54 +04:00
*baseReq
2013-07-02 15:17:31 +04:00
}
func NewBoolReq(args ...string) *BoolReq {
return &BoolReq{
2013-07-02 15:21:54 +04:00
baseReq: newBaseReq(args...),
2013-07-02 15:17:31 +04:00
}
}
func (r *BoolReq) ParseReply(rd reader) (interface{}, error) {
v, err := parseReply(rd)
if err != nil {
return nil, err
}
return v.(int64) == 1, nil
}
func (r *BoolReq) Val() bool {
if r.val == nil {
return false
}
return r.val.(bool)
}
//------------------------------------------------------------------------------
type StringReq struct {
2013-07-02 15:21:54 +04:00
*baseReq
2013-07-02 15:17:31 +04:00
}
func NewStringReq(args ...string) *StringReq {
return &StringReq{
2013-07-02 15:21:54 +04:00
baseReq: newBaseReq(args...),
2013-07-02 15:17:31 +04:00
}
}
func (r *StringReq) Val() string {
if r.val == nil {
return ""
}
return r.val.(string)
}
//------------------------------------------------------------------------------
type FloatReq struct {
2013-07-02 15:21:54 +04:00
*baseReq
2013-07-02 15:17:31 +04:00
}
func NewFloatReq(args ...string) *FloatReq {
return &FloatReq{
2013-07-02 15:21:54 +04:00
baseReq: newBaseReq(args...),
2013-07-02 15:17:31 +04:00
}
}
func (r *FloatReq) ParseReply(rd reader) (interface{}, error) {
v, err := parseReply(rd)
if err != nil {
return nil, err
}
return strconv.ParseFloat(v.(string), 64)
}
func (r *FloatReq) Val() float64 {
if r.val == nil {
return 0
}
return r.val.(float64)
}
//------------------------------------------------------------------------------
type IfaceSliceReq struct {
2013-07-02 15:21:54 +04:00
*baseReq
2013-07-02 15:17:31 +04:00
}
func NewIfaceSliceReq(args ...string) *IfaceSliceReq {
return &IfaceSliceReq{
2013-07-02 15:21:54 +04:00
baseReq: newBaseReq(args...),
2013-07-02 15:17:31 +04:00
}
}
func (r *IfaceSliceReq) Val() []interface{} {
if r.val == nil {
return nil
}
return r.val.([]interface{})
}
//------------------------------------------------------------------------------
type StringSliceReq struct {
2013-07-02 15:21:54 +04:00
*baseReq
2013-07-02 15:17:31 +04:00
}
func NewStringSliceReq(args ...string) *StringSliceReq {
return &StringSliceReq{
2013-07-02 15:21:54 +04:00
baseReq: newBaseReq(args...),
2013-07-02 15:17:31 +04:00
}
}
func (r *StringSliceReq) ParseReply(rd reader) (interface{}, error) {
return parseStringSliceReply(rd)
}
func (r *StringSliceReq) Val() []string {
if r.val == nil {
return nil
}
return r.val.([]string)
}
//------------------------------------------------------------------------------
type BoolSliceReq struct {
2013-07-02 15:21:54 +04:00
*baseReq
2013-07-02 15:17:31 +04:00
}
func NewBoolSliceReq(args ...string) *BoolSliceReq {
return &BoolSliceReq{
2013-07-02 15:21:54 +04:00
baseReq: newBaseReq(args...),
2013-07-02 15:17:31 +04:00
}
}
func (r *BoolSliceReq) ParseReply(rd reader) (interface{}, error) {
return parseBoolSliceReply(rd)
}
func (r *BoolSliceReq) Val() []bool {
if r.val == nil {
return nil
}
return r.val.([]bool)
}
//------------------------------------------------------------------------------
type StringStringMapReq struct {
2013-07-02 15:21:54 +04:00
*baseReq
2013-07-02 15:17:31 +04:00
}
func NewStringStringMapReq(args ...string) *StringStringMapReq {
return &StringStringMapReq{
2013-07-02 15:21:54 +04:00
baseReq: newBaseReq(args...),
2013-07-02 15:17:31 +04:00
}
}
func (r *StringStringMapReq) ParseReply(rd reader) (interface{}, error) {
return parseStringStringMapReply(rd)
}
func (r *StringStringMapReq) Val() map[string]string {
if r.val == nil {
return nil
}
return r.val.(map[string]string)
}
//------------------------------------------------------------------------------
type StringFloatMapReq struct {
2013-07-02 15:21:54 +04:00
*baseReq
2013-07-02 15:17:31 +04:00
}
func NewStringFloatMapReq(args ...string) *StringFloatMapReq {
return &StringFloatMapReq{
2013-07-02 15:21:54 +04:00
baseReq: newBaseReq(args...),
2013-07-02 15:17:31 +04:00
}
}
func (r *StringFloatMapReq) ParseReply(rd reader) (interface{}, error) {
return parseStringFloatMapReply(rd)
}
func (r *StringFloatMapReq) Val() map[string]float64 {
if r.val == nil {
return nil
}
return r.val.(map[string]float64)
}