reply.go 6.5 KB

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