mediocregopher / radix.v2

Redis client for Go
http://godoc.org/github.com/mediocregopher/radix.v2
MIT License
433 stars 92 forks source link

nil but return wrong type #55

Closed zhaoyuxi closed 7 years ago

zhaoyuxi commented 7 years ago

calling Bytes() for "HGET". Nil means no data. But Bytes() returns error. I fix it like this: func (r Resp) Bytes() ([]byte, error) { if r.Err != nil { return nil, r.Err } else if r.typ == Nil { //return nil not error return nil, nil } else if !r.IsType(Str) { return nil,errBadType } .... } func (r Resp) Str() (string, error) { b, err := r.Bytes() if err != nil { return "", err } if b == nil { //nil will return now. return "", errBadType //Probably here "return "", nil" is better } return string(b), nil }

mediocregopher commented 7 years ago

Hey there @zhaoyuxi I'm deliberately keeping the Nil response in these cases, because a Nil response has a different meaning th,an "" or []byte(nil) or []byte{}, which are all roughly equivalent ways of saying "empty string". You can use r.IsType(redis.Nil) to check if a response is Nil if you're expecting that it might be.

zhaoyuxi commented 7 years ago

@mediocregopher A nice way.Thanks very much.