reply.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. "strconv"
  18. pkgerr "github.com/pkg/errors"
  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, pkgerr.WithStack(strconv.ErrRange)
  40. }
  41. return x, nil
  42. case []byte:
  43. n, err := strconv.ParseInt(string(reply), 10, 0)
  44. return int(n), pkgerr.WithStack(err)
  45. case nil:
  46. return 0, ErrNil
  47. case Error:
  48. return 0, reply
  49. }
  50. return 0, pkgerr.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, pkgerr.WithStack(err)
  71. case nil:
  72. return 0, ErrNil
  73. case Error:
  74. return 0, reply
  75. }
  76. return 0, pkgerr.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, pkgerr.WithStack(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, pkgerr.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, pkgerr.WithStack(err)
  124. case nil:
  125. return 0, ErrNil
  126. case Error:
  127. return 0, reply
  128. }
  129. return 0, pkgerr.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 "", pkgerr.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, pkgerr.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. b, e := strconv.ParseBool(string(reply))
  199. return b, pkgerr.WithStack(e)
  200. case nil:
  201. return false, ErrNil
  202. case Error:
  203. return false, reply
  204. }
  205. return false, pkgerr.Errorf("redigo: unexpected type for Bool, got type %T", reply)
  206. }
  207. // MultiBulk is a helper that converts an array command reply to a []interface{}.
  208. //
  209. // Deprecated: Use Values instead.
  210. func MultiBulk(reply interface{}, err error) ([]interface{}, error) { return Values(reply, err) }
  211. // Values is a helper that converts an array command reply to a []interface{}.
  212. // If err is not equal to nil, then Values returns nil, err. Otherwise, Values
  213. // converts the reply as follows:
  214. //
  215. // Reply type Result
  216. // array reply, nil
  217. // nil nil, ErrNil
  218. // other nil, error
  219. func Values(reply interface{}, err error) ([]interface{}, error) {
  220. if err != nil {
  221. return nil, err
  222. }
  223. switch reply := reply.(type) {
  224. case []interface{}:
  225. return reply, nil
  226. case nil:
  227. return nil, ErrNil
  228. case Error:
  229. return nil, reply
  230. }
  231. return nil, pkgerr.Errorf("redigo: unexpected type for Values, got type %T", reply)
  232. }
  233. // Strings is a helper that converts an array command reply to a []string. If
  234. // err is not equal to nil, then Strings returns nil, err. Nil array items are
  235. // converted to "" in the output slice. Strings returns an error if an array
  236. // item is not a bulk string or nil.
  237. func Strings(reply interface{}, err error) ([]string, error) {
  238. if err != nil {
  239. return nil, err
  240. }
  241. switch reply := reply.(type) {
  242. case []interface{}:
  243. result := make([]string, len(reply))
  244. for i := range reply {
  245. if reply[i] == nil {
  246. continue
  247. }
  248. p, ok := reply[i].([]byte)
  249. if !ok {
  250. return nil, pkgerr.Errorf("redigo: unexpected element type for Strings, got type %T", reply[i])
  251. }
  252. result[i] = string(p)
  253. }
  254. return result, nil
  255. case nil:
  256. return nil, ErrNil
  257. case Error:
  258. return nil, reply
  259. }
  260. return nil, pkgerr.Errorf("redigo: unexpected type for Strings, got type %T", reply)
  261. }
  262. // ByteSlices is a helper that converts an array command reply to a [][]byte.
  263. // If err is not equal to nil, then ByteSlices returns nil, err. Nil array
  264. // items are stay nil. ByteSlices returns an error if an array item is not a
  265. // bulk string or nil.
  266. func ByteSlices(reply interface{}, err error) ([][]byte, error) {
  267. if err != nil {
  268. return nil, err
  269. }
  270. switch reply := reply.(type) {
  271. case []interface{}:
  272. result := make([][]byte, len(reply))
  273. for i := range reply {
  274. if reply[i] == nil {
  275. continue
  276. }
  277. p, ok := reply[i].([]byte)
  278. if !ok {
  279. return nil, pkgerr.Errorf("redigo: unexpected element type for ByteSlices, got type %T", reply[i])
  280. }
  281. result[i] = p
  282. }
  283. return result, nil
  284. case nil:
  285. return nil, ErrNil
  286. case Error:
  287. return nil, reply
  288. }
  289. return nil, pkgerr.Errorf("redigo: unexpected type for ByteSlices, got type %T", reply)
  290. }
  291. // Ints is a helper that converts an array command reply to a []int. If
  292. // err is not equal to nil, then Ints returns nil, err.
  293. func Ints(reply interface{}, err error) ([]int, error) {
  294. var ints []int
  295. values, err := Values(reply, err)
  296. if err != nil {
  297. return ints, err
  298. }
  299. if err := ScanSlice(values, &ints); err != nil {
  300. return ints, err
  301. }
  302. return ints, nil
  303. }
  304. // Int64s is a helper that converts an array command reply to a []int64. If
  305. // err is not equal to nil, then Int64s returns nil, err.
  306. func Int64s(reply interface{}, err error) ([]int64, error) {
  307. var int64s []int64
  308. values, err := Values(reply, err)
  309. if err != nil {
  310. return int64s, err
  311. }
  312. if err := ScanSlice(values, &int64s); err != nil {
  313. return int64s, err
  314. }
  315. return int64s, nil
  316. }
  317. // StringMap is a helper that converts an array of strings (alternating key, value)
  318. // into a map[string]string. The HGETALL and CONFIG GET commands return replies in this format.
  319. // Requires an even number of values in result.
  320. func StringMap(result interface{}, err error) (map[string]string, error) {
  321. values, err := Values(result, err)
  322. if err != nil {
  323. return nil, err
  324. }
  325. if len(values)%2 != 0 {
  326. return nil, pkgerr.New("redigo: StringMap expects even number of values result")
  327. }
  328. m := make(map[string]string, len(values)/2)
  329. for i := 0; i < len(values); i += 2 {
  330. key, okKey := values[i].([]byte)
  331. value, okValue := values[i+1].([]byte)
  332. if !okKey || !okValue {
  333. return nil, pkgerr.New("redigo: ScanMap key not a bulk string value")
  334. }
  335. m[string(key)] = string(value)
  336. }
  337. return m, nil
  338. }
  339. // IntMap is a helper that converts an array of strings (alternating key, value)
  340. // into a map[string]int. The HGETALL commands return replies in this format.
  341. // Requires an even number of values in result.
  342. func IntMap(result interface{}, err error) (map[string]int, error) {
  343. values, err := Values(result, err)
  344. if err != nil {
  345. return nil, err
  346. }
  347. if len(values)%2 != 0 {
  348. return nil, pkgerr.New("redigo: IntMap expects even number of values result")
  349. }
  350. m := make(map[string]int, len(values)/2)
  351. for i := 0; i < len(values); i += 2 {
  352. key, ok := values[i].([]byte)
  353. if !ok {
  354. return nil, pkgerr.New("redigo: ScanMap key not a bulk string value")
  355. }
  356. value, err := Int(values[i+1], nil)
  357. if err != nil {
  358. return nil, err
  359. }
  360. m[string(key)] = value
  361. }
  362. return m, nil
  363. }
  364. // Int64Map is a helper that converts an array of strings (alternating key, value)
  365. // into a map[string]int64. The HGETALL commands return replies in this format.
  366. // Requires an even number of values in result.
  367. func Int64Map(result interface{}, err error) (map[string]int64, error) {
  368. values, err := Values(result, err)
  369. if err != nil {
  370. return nil, err
  371. }
  372. if len(values)%2 != 0 {
  373. return nil, pkgerr.New("redigo: Int64Map expects even number of values result")
  374. }
  375. m := make(map[string]int64, len(values)/2)
  376. for i := 0; i < len(values); i += 2 {
  377. key, ok := values[i].([]byte)
  378. if !ok {
  379. return nil, pkgerr.New("redigo: ScanMap key not a bulk string value")
  380. }
  381. value, err := Int64(values[i+1], nil)
  382. if err != nil {
  383. return nil, err
  384. }
  385. m[string(key)] = value
  386. }
  387. return m, nil
  388. }