reply.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright 2012 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package redis
  15. import (
  16. "errors"
  17. "fmt"
  18. "strconv"
  19. )
  20. // ErrNil indicates that a reply value is nil.
  21. var ErrNil = errors.New("redigo: nil returned")
  22. // Int is a helper that converts a command reply to an integer. If err is not
  23. // equal to nil, then Int returns 0, err. Otherwise, Int converts the
  24. // reply to an int as follows:
  25. //
  26. // Reply type Result
  27. // integer int(reply), nil
  28. // bulk parsed reply, nil
  29. // nil 0, ErrNil
  30. // other 0, error
  31. func Int(reply interface{}, err error) (int, error) {
  32. if err != nil {
  33. return 0, err
  34. }
  35. switch reply := reply.(type) {
  36. case int64:
  37. x := int(reply)
  38. if int64(x) != reply {
  39. return 0, strconv.ErrRange
  40. }
  41. return x, nil
  42. case []byte:
  43. n, err := strconv.ParseInt(string(reply), 10, 0)
  44. return int(n), err
  45. case nil:
  46. return 0, ErrNil
  47. case Error:
  48. return 0, reply
  49. }
  50. return 0, fmt.Errorf("redigo: unexpected type for Int, got type %T", reply)
  51. }
  52. // Int64 is a helper that converts a command reply to 64 bit integer. If err is
  53. // not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the
  54. // reply to an int64 as follows:
  55. //
  56. // Reply type Result
  57. // integer reply, nil
  58. // bulk parsed reply, nil
  59. // nil 0, ErrNil
  60. // other 0, error
  61. func Int64(reply interface{}, err error) (int64, error) {
  62. if err != nil {
  63. return 0, err
  64. }
  65. switch reply := reply.(type) {
  66. case int64:
  67. return reply, nil
  68. case []byte:
  69. n, err := strconv.ParseInt(string(reply), 10, 64)
  70. return n, err
  71. case nil:
  72. return 0, ErrNil
  73. case Error:
  74. return 0, reply
  75. }
  76. return 0, fmt.Errorf("redigo: unexpected type for Int64, got type %T", reply)
  77. }
  78. // Float64 is a helper that converts a command reply to 64 bit float. If err is
  79. // not equal to nil, then Float64 returns 0, err. Otherwise, Float64 converts
  80. // the reply to an int as follows:
  81. //
  82. // Reply type Result
  83. // bulk parsed reply, nil
  84. // nil 0, ErrNil
  85. // other 0, error
  86. func Float64(reply interface{}, err error) (float64, error) {
  87. if err != nil {
  88. return 0, err
  89. }
  90. switch reply := reply.(type) {
  91. case []byte:
  92. n, err := strconv.ParseFloat(string(reply), 64)
  93. return n, err
  94. case nil:
  95. return 0, ErrNil
  96. case Error:
  97. return 0, reply
  98. }
  99. return 0, fmt.Errorf("redigo: unexpected type for Float64, got type %T", reply)
  100. }
  101. // String is a helper that converts a command reply to a string. If err is not
  102. // equal to nil, then String returns "", err. Otherwise String converts the
  103. // reply to a string as follows:
  104. //
  105. // Reply type Result
  106. // bulk string(reply), nil
  107. // string reply, nil
  108. // nil "", ErrNil
  109. // other "", error
  110. func String(reply interface{}, err error) (string, error) {
  111. if err != nil {
  112. return "", err
  113. }
  114. switch reply := reply.(type) {
  115. case []byte:
  116. return string(reply), nil
  117. case string:
  118. return reply, nil
  119. case nil:
  120. return "", ErrNil
  121. case Error:
  122. return "", reply
  123. }
  124. return "", fmt.Errorf("redigo: unexpected type for String, got type %T", reply)
  125. }
  126. // Bytes is a helper that converts a command reply to a slice of bytes. If err
  127. // is not equal to nil, then Bytes returns nil, err. Otherwise Bytes converts
  128. // the reply to a slice of bytes as follows:
  129. //
  130. // Reply type Result
  131. // bulk reply, nil
  132. // string []byte(reply), nil
  133. // nil nil, ErrNil
  134. // other nil, error
  135. func Bytes(reply interface{}, err error) ([]byte, error) {
  136. if err != nil {
  137. return nil, err
  138. }
  139. switch reply := reply.(type) {
  140. case []byte:
  141. return reply, nil
  142. case string:
  143. return []byte(reply), nil
  144. case nil:
  145. return nil, ErrNil
  146. case Error:
  147. return nil, reply
  148. }
  149. return nil, fmt.Errorf("redigo: unexpected type for Bytes, got type %T", reply)
  150. }
  151. // Bool is a helper that converts a command reply to a boolean. If err is not
  152. // equal to nil, then Bool returns false, err. Otherwise Bool converts the
  153. // reply to boolean as follows:
  154. //
  155. // Reply type Result
  156. // integer value != 0, nil
  157. // bulk strconv.ParseBool(reply)
  158. // nil false, ErrNil
  159. // other false, error
  160. func Bool(reply interface{}, err error) (bool, error) {
  161. if err != nil {
  162. return false, err
  163. }
  164. switch reply := reply.(type) {
  165. case int64:
  166. return reply != 0, nil
  167. case []byte:
  168. return strconv.ParseBool(string(reply))
  169. case nil:
  170. return false, ErrNil
  171. case Error:
  172. return false, reply
  173. }
  174. return false, fmt.Errorf("redigo: unexpected type for Bool, got type %T", reply)
  175. }
  176. // MultiBulk is deprecated. Use Values.
  177. func MultiBulk(reply interface{}, err error) ([]interface{}, error) { return Values(reply, err) }
  178. // Values is a helper that converts a multi-bulk command reply to a
  179. // []interface{}. If err is not equal to nil, then Values returns nil, err.
  180. // Otherwise, Multi converts the reply as follows:
  181. //
  182. // Reply type Result
  183. // multi-bulk reply, nil
  184. // nil nil, ErrNil
  185. // other nil, error
  186. func Values(reply interface{}, err error) ([]interface{}, error) {
  187. if err != nil {
  188. return nil, err
  189. }
  190. switch reply := reply.(type) {
  191. case []interface{}:
  192. return reply, nil
  193. case nil:
  194. return nil, ErrNil
  195. case Error:
  196. return nil, reply
  197. }
  198. return nil, fmt.Errorf("redigo: unexpected type for Values, got type %T", reply)
  199. }
  200. // Strings is a helper that converts a multi-bulk command reply to a []string.
  201. // If err is not equal to nil, then Strings returns nil, err. If one if the
  202. // multi-bulk items is not a bulk value or nil, then Strings returns an error.
  203. func Strings(reply interface{}, err error) ([]string, error) {
  204. if err != nil {
  205. return nil, err
  206. }
  207. switch reply := reply.(type) {
  208. case []interface{}:
  209. result := make([]string, len(reply))
  210. for i := range reply {
  211. if reply[i] == nil {
  212. continue
  213. }
  214. p, ok := reply[i].([]byte)
  215. if !ok {
  216. return nil, fmt.Errorf("redigo: unexpected element type for Strings, got type %T", reply[i])
  217. }
  218. result[i] = string(p)
  219. }
  220. return result, nil
  221. case nil:
  222. return nil, ErrNil
  223. case Error:
  224. return nil, reply
  225. }
  226. return nil, fmt.Errorf("redigo: unexpected type for Strings, got type %T", reply)
  227. }