reply.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 a helper that converts an array command reply to a []interface{}.
  207. //
  208. // Deprecated: Use Values instead.
  209. func MultiBulk(reply interface{}, err error) ([]interface{}, error) { return Values(reply, err) }
  210. // Values is a helper that converts an array command reply to a []interface{}.
  211. // If err is not equal to nil, then Values returns nil, err. Otherwise, Values
  212. // converts the reply as follows:
  213. //
  214. // Reply type Result
  215. // array reply, nil
  216. // nil nil, ErrNil
  217. // other nil, error
  218. func Values(reply interface{}, err error) ([]interface{}, error) {
  219. if err != nil {
  220. return nil, err
  221. }
  222. switch reply := reply.(type) {
  223. case []interface{}:
  224. return reply, nil
  225. case nil:
  226. return nil, ErrNil
  227. case Error:
  228. return nil, reply
  229. }
  230. return nil, fmt.Errorf("redigo: unexpected type for Values, got type %T", reply)
  231. }
  232. func sliceHelper(reply interface{}, err error, name string, makeSlice func(int), assign func(int, interface{}) error) error {
  233. if err != nil {
  234. return err
  235. }
  236. switch reply := reply.(type) {
  237. case []interface{}:
  238. makeSlice(len(reply))
  239. for i := range reply {
  240. if reply[i] == nil {
  241. continue
  242. }
  243. if err := assign(i, reply[i]); err != nil {
  244. return err
  245. }
  246. }
  247. return nil
  248. case nil:
  249. return ErrNil
  250. case Error:
  251. return reply
  252. }
  253. return fmt.Errorf("redigo: unexpected type for %s, got type %T", name, reply)
  254. }
  255. // Float64s is a helper that converts an array command reply to a []float64. If
  256. // err is not equal to nil, then Float64s returns nil, err. Nil array items are
  257. // converted to 0 in the output slice. Floats64 returns an error if an array
  258. // item is not a bulk string or nil.
  259. func Float64s(reply interface{}, err error) ([]float64, error) {
  260. var result []float64
  261. err = sliceHelper(reply, err, "Float64s", func(n int) { result = make([]float64, n) }, func(i int, v interface{}) error {
  262. p, ok := v.([]byte)
  263. if !ok {
  264. return fmt.Errorf("redigo: unexpected element type for Floats64, got type %T", v)
  265. }
  266. f, err := strconv.ParseFloat(string(p), 64)
  267. result[i] = f
  268. return err
  269. })
  270. return result, err
  271. }
  272. // Strings is a helper that converts an array command reply to a []string. If
  273. // err is not equal to nil, then Strings returns nil, err. Nil array items are
  274. // converted to "" in the output slice. Strings returns an error if an array
  275. // item is not a bulk string or nil.
  276. func Strings(reply interface{}, err error) ([]string, error) {
  277. var result []string
  278. err = sliceHelper(reply, err, "Strings", func(n int) { result = make([]string, n) }, func(i int, v interface{}) error {
  279. switch v := v.(type) {
  280. case string:
  281. result[i] = v
  282. return nil
  283. case []byte:
  284. result[i] = string(v)
  285. return nil
  286. default:
  287. return fmt.Errorf("redigo: unexpected element type for Strings, got type %T", v)
  288. }
  289. })
  290. return result, err
  291. }
  292. // ByteSlices is a helper that converts an array command reply to a [][]byte.
  293. // If err is not equal to nil, then ByteSlices returns nil, err. Nil array
  294. // items are stay nil. ByteSlices returns an error if an array item is not a
  295. // bulk string or nil.
  296. func ByteSlices(reply interface{}, err error) ([][]byte, error) {
  297. var result [][]byte
  298. err = sliceHelper(reply, err, "ByteSlices", func(n int) { result = make([][]byte, n) }, func(i int, v interface{}) error {
  299. p, ok := v.([]byte)
  300. if !ok {
  301. return fmt.Errorf("redigo: unexpected element type for ByteSlices, got type %T", v)
  302. }
  303. result[i] = p
  304. return nil
  305. })
  306. return result, err
  307. }
  308. // Int64s is a helper that converts an array command reply to a []int64.
  309. // If err is not equal to nil, then Int64s returns nil, err. Nil array
  310. // items are stay nil. Int64s returns an error if an array item is not a
  311. // bulk string or nil.
  312. func Int64s(reply interface{}, err error) ([]int64, error) {
  313. var result []int64
  314. err = sliceHelper(reply, err, "Int64s", func(n int) { result = make([]int64, n) }, func(i int, v interface{}) error {
  315. switch v := v.(type) {
  316. case int64:
  317. result[i] = v
  318. return nil
  319. case []byte:
  320. n, err := strconv.ParseInt(string(v), 10, 64)
  321. result[i] = n
  322. return err
  323. default:
  324. return fmt.Errorf("redigo: unexpected element type for Int64s, got type %T", v)
  325. }
  326. })
  327. return result, err
  328. }
  329. // Ints is a helper that converts an array command reply to a []in.
  330. // If err is not equal to nil, then Ints returns nil, err. Nil array
  331. // items are stay nil. Ints returns an error if an array item is not a
  332. // bulk string or nil.
  333. func Ints(reply interface{}, err error) ([]int, error) {
  334. var result []int
  335. err = sliceHelper(reply, err, "Ints", func(n int) { result = make([]int, n) }, func(i int, v interface{}) error {
  336. switch v := v.(type) {
  337. case int64:
  338. n := int(v)
  339. if int64(n) != v {
  340. return strconv.ErrRange
  341. }
  342. result[i] = n
  343. return nil
  344. case []byte:
  345. n, err := strconv.Atoi(string(v))
  346. result[i] = n
  347. return err
  348. default:
  349. return fmt.Errorf("redigo: unexpected element type for Ints, got type %T", v)
  350. }
  351. })
  352. return result, err
  353. }
  354. // StringMap is a helper that converts an array of strings (alternating key, value)
  355. // into a map[string]string. The HGETALL and CONFIG GET commands return replies in this format.
  356. // Requires an even number of values in result.
  357. func StringMap(result interface{}, err error) (map[string]string, error) {
  358. values, err := Values(result, err)
  359. if err != nil {
  360. return nil, err
  361. }
  362. if len(values)%2 != 0 {
  363. return nil, errors.New("redigo: StringMap expects even number of values result")
  364. }
  365. m := make(map[string]string, len(values)/2)
  366. for i := 0; i < len(values); i += 2 {
  367. key, okKey := values[i].([]byte)
  368. value, okValue := values[i+1].([]byte)
  369. if !okKey || !okValue {
  370. return nil, errors.New("redigo: StringMap key not a bulk string value")
  371. }
  372. m[string(key)] = string(value)
  373. }
  374. return m, nil
  375. }
  376. // IntMap is a helper that converts an array of strings (alternating key, value)
  377. // into a map[string]int. The HGETALL commands return replies in this format.
  378. // Requires an even number of values in result.
  379. func IntMap(result interface{}, err error) (map[string]int, error) {
  380. values, err := Values(result, err)
  381. if err != nil {
  382. return nil, err
  383. }
  384. if len(values)%2 != 0 {
  385. return nil, errors.New("redigo: IntMap expects even number of values result")
  386. }
  387. m := make(map[string]int, len(values)/2)
  388. for i := 0; i < len(values); i += 2 {
  389. key, ok := values[i].([]byte)
  390. if !ok {
  391. return nil, errors.New("redigo: IntMap key not a bulk string value")
  392. }
  393. value, err := Int(values[i+1], nil)
  394. if err != nil {
  395. return nil, err
  396. }
  397. m[string(key)] = value
  398. }
  399. return m, nil
  400. }
  401. // Int64Map is a helper that converts an array of strings (alternating key, value)
  402. // into a map[string]int64. The HGETALL commands return replies in this format.
  403. // Requires an even number of values in result.
  404. func Int64Map(result interface{}, err error) (map[string]int64, error) {
  405. values, err := Values(result, err)
  406. if err != nil {
  407. return nil, err
  408. }
  409. if len(values)%2 != 0 {
  410. return nil, errors.New("redigo: Int64Map expects even number of values result")
  411. }
  412. m := make(map[string]int64, len(values)/2)
  413. for i := 0; i < len(values); i += 2 {
  414. key, ok := values[i].([]byte)
  415. if !ok {
  416. return nil, errors.New("redigo: Int64Map key not a bulk string value")
  417. }
  418. value, err := Int64(values[i+1], nil)
  419. if err != nil {
  420. return nil, err
  421. }
  422. m[string(key)] = value
  423. }
  424. return m, nil
  425. }
  426. // Positions is a helper that converts an array of positions (lat, long)
  427. // into a [][2]float64. The GEOPOS command returns replies in this format.
  428. func Positions(result interface{}, err error) ([]*[2]float64, error) {
  429. values, err := Values(result, err)
  430. if err != nil {
  431. return nil, err
  432. }
  433. positions := make([]*[2]float64, len(values))
  434. for i := range values {
  435. if values[i] == nil {
  436. continue
  437. }
  438. p, ok := values[i].([]interface{})
  439. if !ok {
  440. return nil, fmt.Errorf("redigo: unexpected element type for interface slice, got type %T", values[i])
  441. }
  442. if len(p) != 2 {
  443. return nil, fmt.Errorf("redigo: unexpected number of values for a member position, got %d", len(p))
  444. }
  445. lat, err := Float64(p[0], nil)
  446. if err != nil {
  447. return nil, err
  448. }
  449. long, err := Float64(p[1], nil)
  450. if err != nil {
  451. return nil, err
  452. }
  453. positions[i] = &[2]float64{lat, long}
  454. }
  455. return positions, nil
  456. }