reply.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 strconv.ParseInt(reply, 10, 0)
  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, Int converts the reply
  53. // to an int as follows:
  54. //
  55. // Reply type Result
  56. // integer reply, nil
  57. // bulk strconv.ParseInt(reply, 10, 64)
  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. // String is a helper that converts a command reply to a string. If err is not
  78. // equal to nil, then String returns "", err. Otherwise String converts the
  79. // reply to a string as follows:
  80. //
  81. // Reply type Result
  82. // bulk string(reply), nil
  83. // string reply, nil
  84. // nil "", ErrNil
  85. // other "", error
  86. func String(reply interface{}, err error) (string, error) {
  87. if err != nil {
  88. return "", err
  89. }
  90. switch reply := reply.(type) {
  91. case []byte:
  92. return string(reply), nil
  93. case string:
  94. return reply, nil
  95. case nil:
  96. return "", ErrNil
  97. case Error:
  98. return "", reply
  99. }
  100. return "", fmt.Errorf("redigo: unexpected type for String, got type %T", reply)
  101. }
  102. // Bytes is a helper that converts a command reply to a slice of bytes. If err
  103. // is not equal to nil, then Bytes returns nil, err. Otherwise Bytes converts
  104. // the reply to a slice of bytes as follows:
  105. //
  106. // Reply type Result
  107. // bulk reply, nil
  108. // string []byte(reply), nil
  109. // nil nil, ErrNil
  110. // other nil, error
  111. func Bytes(reply interface{}, err error) ([]byte, error) {
  112. if err != nil {
  113. return nil, err
  114. }
  115. switch reply := reply.(type) {
  116. case []byte:
  117. return reply, nil
  118. case string:
  119. return []byte(reply), nil
  120. case nil:
  121. return nil, ErrNil
  122. case Error:
  123. return nil, reply
  124. }
  125. return nil, fmt.Errorf("redigo: unexpected type for Bytes, got type %T", reply)
  126. }
  127. // Bool is a helper that converts a command reply to a boolean. If err is not
  128. // equal to nil, then Bool returns false, err. Otherwise Bool converts the
  129. // reply to boolean as follows:
  130. //
  131. // Reply type Result
  132. // integer value != 0, nil
  133. // bulk strconv.ParseBool(reply)
  134. // nil false, ErrNil
  135. // other false, error
  136. func Bool(reply interface{}, err error) (bool, error) {
  137. if err != nil {
  138. return false, err
  139. }
  140. switch reply := reply.(type) {
  141. case int64:
  142. return reply != 0, nil
  143. case []byte:
  144. return strconv.ParseBool(string(reply))
  145. case nil:
  146. return false, ErrNil
  147. case Error:
  148. return false, reply
  149. }
  150. return false, fmt.Errorf("redigo: unexpected type for Bool, got type %T", reply)
  151. }
  152. // MultiBulk is deprecated. Use Values.
  153. func MultiBulk(reply interface{}, err error) ([]interface{}, error) { return Values(reply, err) }
  154. // Values is a helper that converts a multi-bulk command reply to a
  155. // []interface{}. If err is not equal to nil, then Values returns nil, err.
  156. // Otherwise, Multi converts the reply as follows:
  157. //
  158. // Reply type Result
  159. // multi-bulk reply, nil
  160. // nil nil, ErrNil
  161. // other nil, error
  162. func Values(reply interface{}, err error) ([]interface{}, error) {
  163. if err != nil {
  164. return nil, err
  165. }
  166. switch reply := reply.(type) {
  167. case []interface{}:
  168. return reply, nil
  169. case nil:
  170. return nil, ErrNil
  171. case Error:
  172. return nil, reply
  173. }
  174. return nil, fmt.Errorf("redigo: unexpected type for Multi, got type %T", reply)
  175. }