reply.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 string 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 string 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. var errNegativeInt = errors.New("redigo: unexpected value for Uint64")
  79. // Uint64 is a helper that converts a command reply to 64 bit integer. If err is
  80. // not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the
  81. // reply to an int64 as follows:
  82. //
  83. // Reply type Result
  84. // integer reply, nil
  85. // bulk string parsed reply, nil
  86. // nil 0, ErrNil
  87. // other 0, error
  88. func Uint64(reply interface{}, err error) (uint64, error) {
  89. if err != nil {
  90. return 0, err
  91. }
  92. switch reply := reply.(type) {
  93. case int64:
  94. if reply < 0 {
  95. return 0, errNegativeInt
  96. }
  97. return uint64(reply), nil
  98. case []byte:
  99. n, err := strconv.ParseUint(string(reply), 10, 64)
  100. return n, err
  101. case nil:
  102. return 0, ErrNil
  103. case Error:
  104. return 0, reply
  105. }
  106. return 0, fmt.Errorf("redigo: unexpected type for Uint64, got type %T", reply)
  107. }
  108. // Float64 is a helper that converts a command reply to 64 bit float. If err is
  109. // not equal to nil, then Float64 returns 0, err. Otherwise, Float64 converts
  110. // the reply to an int as follows:
  111. //
  112. // Reply type Result
  113. // bulk string parsed reply, nil
  114. // nil 0, ErrNil
  115. // other 0, error
  116. func Float64(reply interface{}, err error) (float64, error) {
  117. if err != nil {
  118. return 0, err
  119. }
  120. switch reply := reply.(type) {
  121. case []byte:
  122. n, err := strconv.ParseFloat(string(reply), 64)
  123. return n, err
  124. case nil:
  125. return 0, ErrNil
  126. case Error:
  127. return 0, reply
  128. }
  129. return 0, fmt.Errorf("redigo: unexpected type for Float64, got type %T", reply)
  130. }
  131. // String is a helper that converts a command reply to a string. If err is not
  132. // equal to nil, then String returns "", err. Otherwise String converts the
  133. // reply to a string as follows:
  134. //
  135. // Reply type Result
  136. // bulk string string(reply), nil
  137. // simple string reply, nil
  138. // nil "", ErrNil
  139. // other "", error
  140. func String(reply interface{}, err error) (string, error) {
  141. if err != nil {
  142. return "", err
  143. }
  144. switch reply := reply.(type) {
  145. case []byte:
  146. return string(reply), nil
  147. case string:
  148. return reply, nil
  149. case nil:
  150. return "", ErrNil
  151. case Error:
  152. return "", reply
  153. }
  154. return "", fmt.Errorf("redigo: unexpected type for String, got type %T", reply)
  155. }
  156. // Bytes is a helper that converts a command reply to a slice of bytes. If err
  157. // is not equal to nil, then Bytes returns nil, err. Otherwise Bytes converts
  158. // the reply to a slice of bytes as follows:
  159. //
  160. // Reply type Result
  161. // bulk string reply, nil
  162. // simple string []byte(reply), nil
  163. // nil nil, ErrNil
  164. // other nil, error
  165. func Bytes(reply interface{}, err error) ([]byte, error) {
  166. if err != nil {
  167. return nil, err
  168. }
  169. switch reply := reply.(type) {
  170. case []byte:
  171. return reply, nil
  172. case string:
  173. return []byte(reply), nil
  174. case nil:
  175. return nil, ErrNil
  176. case Error:
  177. return nil, reply
  178. }
  179. return nil, fmt.Errorf("redigo: unexpected type for Bytes, got type %T", reply)
  180. }
  181. // Bool is a helper that converts a command reply to a boolean. If err is not
  182. // equal to nil, then Bool returns false, err. Otherwise Bool converts the
  183. // reply to boolean as follows:
  184. //
  185. // Reply type Result
  186. // integer value != 0, nil
  187. // bulk string strconv.ParseBool(reply)
  188. // nil false, ErrNil
  189. // other false, error
  190. func Bool(reply interface{}, err error) (bool, error) {
  191. if err != nil {
  192. return false, err
  193. }
  194. switch reply := reply.(type) {
  195. case int64:
  196. return reply != 0, nil
  197. case []byte:
  198. return strconv.ParseBool(string(reply))
  199. case nil:
  200. return false, ErrNil
  201. case Error:
  202. return false, reply
  203. }
  204. return false, fmt.Errorf("redigo: unexpected type for Bool, got type %T", reply)
  205. }
  206. // MultiBulk is deprecated. Use Values.
  207. func MultiBulk(reply interface{}, err error) ([]interface{}, error) { return Values(reply, err) }
  208. // Values is a helper that converts an array command reply to a []interface{}.
  209. // If err is not equal to nil, then Values returns nil, err. Otherwise, Values
  210. // converts the reply as follows:
  211. //
  212. // Reply type Result
  213. // array reply, nil
  214. // nil nil, ErrNil
  215. // other nil, error
  216. func Values(reply interface{}, err error) ([]interface{}, error) {
  217. if err != nil {
  218. return nil, err
  219. }
  220. switch reply := reply.(type) {
  221. case []interface{}:
  222. return reply, nil
  223. case nil:
  224. return nil, ErrNil
  225. case Error:
  226. return nil, reply
  227. }
  228. return nil, fmt.Errorf("redigo: unexpected type for Values, got type %T", reply)
  229. }
  230. // Strings is a helper that converts an array command reply to a []string. If
  231. // err is not equal to nil, then Strings returns nil, err. Nil array items are
  232. // converted to "" in the output slice. Strings returns an error if an array
  233. // item is not a bulk string or nil.
  234. func Strings(reply interface{}, err error) ([]string, error) {
  235. if err != nil {
  236. return nil, err
  237. }
  238. switch reply := reply.(type) {
  239. case []interface{}:
  240. result := make([]string, len(reply))
  241. for i := range reply {
  242. if reply[i] == nil {
  243. continue
  244. }
  245. p, ok := reply[i].([]byte)
  246. if !ok {
  247. return nil, fmt.Errorf("redigo: unexpected element type for Strings, got type %T", reply[i])
  248. }
  249. result[i] = string(p)
  250. }
  251. return result, nil
  252. case nil:
  253. return nil, ErrNil
  254. case Error:
  255. return nil, reply
  256. }
  257. return nil, fmt.Errorf("redigo: unexpected type for Strings, got type %T", reply)
  258. }
  259. // Ints is a helper that converts an array command reply to a []int. If
  260. // err is not equal to nil, then Ints returns nil, err.
  261. func Ints(reply interface{}, err error) ([]int, error) {
  262. var ints []int
  263. if reply == nil {
  264. return ints, ErrNil
  265. }
  266. values, err := Values(reply, err)
  267. if err != nil {
  268. return ints, err
  269. }
  270. if err := ScanSlice(values, &ints); err != nil {
  271. return ints, err
  272. }
  273. return ints, nil
  274. }
  275. // StringMap is a helper that converts an array of strings (alternating key, value)
  276. // into a map[string]string. The HGETALL and CONFIG GET commands return replies in this format.
  277. // Requires an even number of values in result.
  278. func StringMap(result interface{}, err error) (map[string]string, error) {
  279. values, err := Values(result, err)
  280. if err != nil {
  281. return nil, err
  282. }
  283. if len(values)%2 != 0 {
  284. return nil, errors.New("redigo: StringMap expects even number of values result")
  285. }
  286. m := make(map[string]string, len(values)/2)
  287. for i := 0; i < len(values); i += 2 {
  288. key, okKey := values[i].([]byte)
  289. value, okValue := values[i+1].([]byte)
  290. if !okKey || !okValue {
  291. return nil, errors.New("redigo: ScanMap key not a bulk string value")
  292. }
  293. m[string(key)] = string(value)
  294. }
  295. return m, nil
  296. }