redis/req.go

258 lines
4.3 KiB
Go
Raw Normal View History

2012-07-25 17:00:50 +04:00
package redis
import (
"strconv"
)
type Req interface {
2012-08-14 19:20:22 +04:00
Args() []string
2012-08-17 22:36:48 +04:00
ParseReply(reader) (interface{}, error)
2012-07-25 17:00:50 +04:00
SetErr(error)
2012-08-05 16:09:43 +04:00
Err() error
2012-07-26 22:43:21 +04:00
SetVal(interface{})
2012-08-17 22:36:48 +04:00
IfaceVal() interface{}
2012-07-25 17:00:50 +04:00
}
//------------------------------------------------------------------------------
type BaseReq struct {
args []string
2012-07-29 13:42:00 +04:00
val interface{}
err error
2012-07-25 17:00:50 +04:00
}
func NewBaseReq(args ...string) *BaseReq {
return &BaseReq{
args: args,
}
}
2012-08-14 19:20:22 +04:00
func (r *BaseReq) Args() []string {
return r.args
2012-07-25 17:00:50 +04:00
}
func (r *BaseReq) SetErr(err error) {
2012-07-26 22:43:21 +04:00
if err == nil {
panic("non-nil value expected")
}
2012-07-25 17:00:50 +04:00
r.err = err
}
2012-08-05 16:09:43 +04:00
func (r *BaseReq) Err() error {
2012-08-06 16:09:48 +04:00
if r.err != nil {
return r.err
}
if r.val == nil {
return errValNotSet
2012-08-06 16:09:48 +04:00
}
return nil
2012-08-05 16:09:43 +04:00
}
2012-07-26 22:43:21 +04:00
func (r *BaseReq) SetVal(val interface{}) {
if val == nil {
panic("non-nil value expected")
}
r.val = val
}
2012-08-17 22:36:48 +04:00
func (r *BaseReq) IfaceVal() interface{} {
2012-08-05 16:09:43 +04:00
return r.val
}
2012-08-17 22:36:48 +04:00
func (r *BaseReq) ParseReply(rd reader) (interface{}, error) {
return parseReply(rd)
2012-07-25 17:00:50 +04:00
}
//------------------------------------------------------------------------------
2012-08-20 14:42:33 +04:00
type IfaceReq struct {
*BaseReq
}
func NewIfaceReq(args ...string) *IfaceReq {
return &IfaceReq{
BaseReq: NewBaseReq(args...),
}
}
func (r *IfaceReq) Val() interface{} {
return r.val
}
//------------------------------------------------------------------------------
2012-07-25 17:00:50 +04:00
type StatusReq struct {
*BaseReq
}
func NewStatusReq(args ...string) *StatusReq {
return &StatusReq{
BaseReq: NewBaseReq(args...),
}
}
2012-08-06 16:09:48 +04:00
func (r *StatusReq) Val() string {
if r.val == nil {
return ""
2012-07-26 22:43:21 +04:00
}
2012-08-06 16:09:48 +04:00
return r.val.(string)
2012-07-25 17:00:50 +04:00
}
//------------------------------------------------------------------------------
type IntReq struct {
*BaseReq
}
func NewIntReq(args ...string) *IntReq {
return &IntReq{
BaseReq: NewBaseReq(args...),
}
}
2012-08-06 16:09:48 +04:00
func (r *IntReq) Val() int64 {
if r.val == nil {
return 0
2012-07-26 22:43:21 +04:00
}
2012-08-06 16:09:48 +04:00
return r.val.(int64)
2012-07-25 17:00:50 +04:00
}
//------------------------------------------------------------------------------
type BoolReq struct {
*BaseReq
}
func NewBoolReq(args ...string) *BoolReq {
return &BoolReq{
BaseReq: NewBaseReq(args...),
}
}
2012-08-17 22:36:48 +04:00
func (r *BoolReq) ParseReply(rd reader) (interface{}, error) {
v, err := parseReply(rd)
2012-07-26 22:43:21 +04:00
if err != nil {
return nil, err
2012-07-25 17:00:50 +04:00
}
return v.(int64) == 1, nil
2012-07-25 17:00:50 +04:00
}
2012-08-06 16:09:48 +04:00
func (r *BoolReq) Val() bool {
if r.val == nil {
return false
2012-07-26 22:43:21 +04:00
}
2012-08-06 16:09:48 +04:00
return r.val.(bool)
2012-07-25 17:00:50 +04:00
}
//------------------------------------------------------------------------------
2012-08-17 22:36:48 +04:00
type StringReq struct {
2012-07-25 17:00:50 +04:00
*BaseReq
}
2012-08-17 22:36:48 +04:00
func NewStringReq(args ...string) *StringReq {
return &StringReq{
2012-07-25 17:00:50 +04:00
BaseReq: NewBaseReq(args...),
}
}
2012-08-17 22:36:48 +04:00
func (r *StringReq) Val() string {
2012-08-06 16:09:48 +04:00
if r.val == nil {
return ""
2012-07-26 22:43:21 +04:00
}
2012-08-06 16:09:48 +04:00
return r.val.(string)
2012-07-25 17:00:50 +04:00
}
//------------------------------------------------------------------------------
2012-07-27 18:21:50 +04:00
type FloatReq struct {
*BaseReq
}
func NewFloatReq(args ...string) *FloatReq {
return &FloatReq{
BaseReq: NewBaseReq(args...),
}
}
2012-08-17 22:36:48 +04:00
func (r *FloatReq) ParseReply(rd reader) (interface{}, error) {
v, err := parseReply(rd)
2012-07-27 18:21:50 +04:00
if err != nil {
return nil, err
}
return strconv.ParseFloat(v.(string), 64)
2012-07-27 18:21:50 +04:00
}
2012-08-06 16:09:48 +04:00
func (r *FloatReq) Val() float64 {
if r.val == nil {
return 0
2012-07-27 18:21:50 +04:00
}
2012-08-06 16:09:48 +04:00
return r.val.(float64)
2012-07-27 18:21:50 +04:00
}
//------------------------------------------------------------------------------
2012-08-17 22:36:48 +04:00
type IfaceSliceReq struct {
2012-07-25 17:00:50 +04:00
*BaseReq
}
2012-08-17 22:36:48 +04:00
func NewIfaceSliceReq(args ...string) *IfaceSliceReq {
return &IfaceSliceReq{
2012-07-25 17:00:50 +04:00
BaseReq: NewBaseReq(args...),
}
}
2012-08-17 22:36:48 +04:00
func (r *IfaceSliceReq) Val() []interface{} {
2012-08-06 16:09:48 +04:00
if r.val == nil {
return nil
2012-07-26 22:43:21 +04:00
}
2012-08-06 16:09:48 +04:00
return r.val.([]interface{})
2012-07-25 17:00:50 +04:00
}
2012-08-17 22:36:48 +04:00
//------------------------------------------------------------------------------
type StringSliceReq struct {
*BaseReq
}
func NewStringSliceReq(args ...string) *StringSliceReq {
return &StringSliceReq{
BaseReq: NewBaseReq(args...),
}
}
2012-08-20 14:42:33 +04:00
func (r *StringSliceReq) ParseReply(rd reader) (interface{}, error) {
return parseStringSliceReply(rd)
}
2012-08-17 22:36:48 +04:00
func (r *StringSliceReq) Val() []string {
if r.val == nil {
return nil
}
return r.val.([]string)
}
2012-08-20 14:42:33 +04:00
//------------------------------------------------------------------------------
type BoolSliceReq struct {
*BaseReq
}
func NewBoolSliceReq(args ...string) *BoolSliceReq {
return &BoolSliceReq{
BaseReq: NewBaseReq(args...),
}
}
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)
}