reply.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package goredis
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. )
  7. // ErrNil indicates that a reply value is nil.
  8. var ErrNil = errors.New("nil returned")
  9. // Int is a helper that converts a command reply to an integer. If err is not
  10. // equal to nil, then Int returns 0, err. Otherwise, Int converts the
  11. // reply to an int as follows:
  12. //
  13. // Reply type Result
  14. // integer int(reply), nil
  15. // bulk string parsed reply, nil
  16. // nil 0, ErrNil
  17. // other 0, error
  18. func Int(reply interface{}, err error) (int, error) {
  19. if err != nil {
  20. return 0, err
  21. }
  22. switch reply := reply.(type) {
  23. case int64:
  24. x := int(reply)
  25. if int64(x) != reply {
  26. return 0, strconv.ErrRange
  27. }
  28. return x, nil
  29. case []byte:
  30. n, err := strconv.ParseInt(string(reply), 10, 0)
  31. return int(n), err
  32. case nil:
  33. return 0, ErrNil
  34. case Error:
  35. return 0, reply
  36. }
  37. return 0, fmt.Errorf("unexpected type for Int, got type %T", reply)
  38. }
  39. // Int64 is a helper that converts a command reply to 64 bit integer. If err is
  40. // not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the
  41. // reply to an int64 as follows:
  42. //
  43. // Reply type Result
  44. // integer reply, nil
  45. // bulk string parsed reply, nil
  46. // nil 0, ErrNil
  47. // other 0, error
  48. func Int64(reply interface{}, err error) (int64, error) {
  49. if err != nil {
  50. return 0, err
  51. }
  52. switch reply := reply.(type) {
  53. case int64:
  54. return reply, nil
  55. case []byte:
  56. n, err := strconv.ParseInt(string(reply), 10, 64)
  57. return n, err
  58. case nil:
  59. return 0, ErrNil
  60. case Error:
  61. return 0, reply
  62. }
  63. return 0, fmt.Errorf("unexpected type for Int64, got type %T", reply)
  64. }
  65. var errNegativeInt = errors.New("unexpected value for Uint64")
  66. // Uint64 is a helper that converts a command reply to 64 bit integer. If err is
  67. // not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the
  68. // reply to an int64 as follows:
  69. //
  70. // Reply type Result
  71. // integer reply, nil
  72. // bulk string parsed reply, nil
  73. // nil 0, ErrNil
  74. // other 0, error
  75. func Uint64(reply interface{}, err error) (uint64, error) {
  76. if err != nil {
  77. return 0, err
  78. }
  79. switch reply := reply.(type) {
  80. case int64:
  81. if reply < 0 {
  82. return 0, errNegativeInt
  83. }
  84. return uint64(reply), nil
  85. case []byte:
  86. n, err := strconv.ParseUint(string(reply), 10, 64)
  87. return n, err
  88. case nil:
  89. return 0, ErrNil
  90. case Error:
  91. return 0, reply
  92. }
  93. return 0, fmt.Errorf("unexpected type for Uint64, got type %T", reply)
  94. }
  95. // Float64 is a helper that converts a command reply to 64 bit float. If err is
  96. // not equal to nil, then Float64 returns 0, err. Otherwise, Float64 converts
  97. // the reply to an int as follows:
  98. //
  99. // Reply type Result
  100. // bulk string parsed reply, nil
  101. // nil 0, ErrNil
  102. // other 0, error
  103. func Float64(reply interface{}, err error) (float64, error) {
  104. if err != nil {
  105. return 0, err
  106. }
  107. switch reply := reply.(type) {
  108. case []byte:
  109. n, err := strconv.ParseFloat(string(reply), 64)
  110. return n, err
  111. case nil:
  112. return 0, ErrNil
  113. case Error:
  114. return 0, reply
  115. }
  116. return 0, fmt.Errorf("unexpected type for Float64, got type %T", reply)
  117. }
  118. // String is a helper that converts a command reply to a string. If err is not
  119. // equal to nil, then String returns "", err. Otherwise String converts the
  120. // reply to a string as follows:
  121. //
  122. // Reply type Result
  123. // bulk string string(reply), nil
  124. // simple string reply, nil
  125. // nil "", ErrNil
  126. // other "", error
  127. func String(reply interface{}, err error) (string, error) {
  128. if err != nil {
  129. return "", err
  130. }
  131. switch reply := reply.(type) {
  132. case []byte:
  133. return string(reply), nil
  134. case string:
  135. return reply, nil
  136. case nil:
  137. return "", ErrNil
  138. case Error:
  139. return "", reply
  140. }
  141. return "", fmt.Errorf("unexpected type for String, got type %T", reply)
  142. }
  143. // Bytes is a helper that converts a command reply to a slice of bytes. If err
  144. // is not equal to nil, then Bytes returns nil, err. Otherwise Bytes converts
  145. // the reply to a slice of bytes as follows:
  146. //
  147. // Reply type Result
  148. // bulk string reply, nil
  149. // simple string []byte(reply), nil
  150. // nil nil, ErrNil
  151. // other nil, error
  152. func Bytes(reply interface{}, err error) ([]byte, error) {
  153. if err != nil {
  154. return nil, err
  155. }
  156. switch reply := reply.(type) {
  157. case []byte:
  158. return reply, nil
  159. case string:
  160. return []byte(reply), nil
  161. case nil:
  162. return nil, ErrNil
  163. case Error:
  164. return nil, reply
  165. }
  166. return nil, fmt.Errorf("unexpected type for Bytes, got type %T", reply)
  167. }
  168. // Bool is a helper that converts a command reply to a boolean. If err is not
  169. // equal to nil, then Bool returns false, err. Otherwise Bool converts the
  170. // reply to boolean as follows:
  171. //
  172. // Reply type Result
  173. // integer value != 0, nil
  174. // bulk string strconv.ParseBool(reply)
  175. // nil false, ErrNil
  176. // other false, error
  177. func Bool(reply interface{}, err error) (bool, error) {
  178. if err != nil {
  179. return false, err
  180. }
  181. switch reply := reply.(type) {
  182. case int64:
  183. return reply != 0, nil
  184. case []byte:
  185. return strconv.ParseBool(string(reply))
  186. case nil:
  187. return false, ErrNil
  188. case Error:
  189. return false, reply
  190. }
  191. return false, fmt.Errorf("unexpected type for Bool, got type %T", reply)
  192. }
  193. // MultiBulk is deprecated. Use Values.
  194. func MultiBulk(reply interface{}, err error) ([]interface{}, error) { return Values(reply, err) }
  195. // Values is a helper that converts an array command reply to a []interface{}.
  196. // If err is not equal to nil, then Values returns nil, err. Otherwise, Values
  197. // converts the reply as follows:
  198. //
  199. // Reply type Result
  200. // array reply, nil
  201. // nil nil, ErrNil
  202. // other nil, error
  203. func Values(reply interface{}, err error) ([]interface{}, error) {
  204. if err != nil {
  205. return nil, err
  206. }
  207. switch reply := reply.(type) {
  208. case []interface{}:
  209. return reply, nil
  210. case nil:
  211. return nil, ErrNil
  212. case Error:
  213. return nil, reply
  214. }
  215. return nil, fmt.Errorf("unexpected type for Values, got type %T", reply)
  216. }
  217. // Strings is a helper that converts an array command reply to a []string. If
  218. // err is not equal to nil, then Strings returns nil, err. If one of the array
  219. // items is not a bulk string or nil, then Strings returns an error.
  220. func Strings(reply interface{}, err error) ([]string, error) {
  221. if err != nil {
  222. return nil, err
  223. }
  224. switch reply := reply.(type) {
  225. case []interface{}:
  226. result := make([]string, len(reply))
  227. for i := range reply {
  228. if reply[i] == nil {
  229. continue
  230. }
  231. p, ok := reply[i].([]byte)
  232. if !ok {
  233. return nil, fmt.Errorf("unexpected element type for Strings, got type %T", reply[i])
  234. }
  235. result[i] = string(p)
  236. }
  237. return result, nil
  238. case nil:
  239. return nil, ErrNil
  240. case Error:
  241. return nil, reply
  242. }
  243. return nil, fmt.Errorf("unexpected type for Strings, got type %T", reply)
  244. }