reply.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. "time"
  20. )
  21. // ErrNil indicates that a reply value is nil.
  22. var ErrNil = errors.New("redigo: nil returned")
  23. // Int is a helper that converts a command reply to an integer. If err is not
  24. // equal to nil, then Int returns 0, err. Otherwise, Int converts the
  25. // reply to an int as follows:
  26. //
  27. // Reply type Result
  28. // integer int(reply), nil
  29. // bulk string parsed reply, nil
  30. // nil 0, ErrNil
  31. // other 0, error
  32. func Int(reply interface{}, err error) (int, error) {
  33. if err != nil {
  34. return 0, err
  35. }
  36. switch reply := reply.(type) {
  37. case int64:
  38. x := int(reply)
  39. if int64(x) != reply {
  40. return 0, strconv.ErrRange
  41. }
  42. return x, nil
  43. case []byte:
  44. n, err := strconv.ParseInt(string(reply), 10, 0)
  45. return int(n), err
  46. case nil:
  47. return 0, ErrNil
  48. case Error:
  49. return 0, reply
  50. }
  51. return 0, fmt.Errorf("redigo: unexpected type for Int, got type %T", reply)
  52. }
  53. // Int64 is a helper that converts a command reply to 64 bit integer. If err is
  54. // not equal to nil, then Int64 returns 0, err. Otherwise, Int64 converts the
  55. // reply to an int64 as follows:
  56. //
  57. // Reply type Result
  58. // integer reply, nil
  59. // bulk string parsed reply, nil
  60. // nil 0, ErrNil
  61. // other 0, error
  62. func Int64(reply interface{}, err error) (int64, error) {
  63. if err != nil {
  64. return 0, err
  65. }
  66. switch reply := reply.(type) {
  67. case int64:
  68. return reply, nil
  69. case []byte:
  70. n, err := strconv.ParseInt(string(reply), 10, 64)
  71. return n, err
  72. case nil:
  73. return 0, ErrNil
  74. case Error:
  75. return 0, reply
  76. }
  77. return 0, fmt.Errorf("redigo: unexpected type for Int64, got type %T", reply)
  78. }
  79. func errNegativeInt(v int64) error {
  80. return fmt.Errorf("redigo: unexpected negative value %v for Uint64", v)
  81. }
  82. // Uint64 is a helper that converts a command reply to 64 bit unsigned integer.
  83. // If err is not equal to nil, then Uint64 returns 0, err. Otherwise, Uint64 converts the
  84. // reply to an uint64 as follows:
  85. //
  86. // Reply type Result
  87. // +integer reply, nil
  88. // bulk string parsed reply, nil
  89. // nil 0, ErrNil
  90. // other 0, error
  91. func Uint64(reply interface{}, err error) (uint64, error) {
  92. if err != nil {
  93. return 0, err
  94. }
  95. switch reply := reply.(type) {
  96. case int64:
  97. if reply < 0 {
  98. return 0, errNegativeInt(reply)
  99. }
  100. return uint64(reply), nil
  101. case []byte:
  102. n, err := strconv.ParseUint(string(reply), 10, 64)
  103. return n, err
  104. case nil:
  105. return 0, ErrNil
  106. case Error:
  107. return 0, reply
  108. }
  109. return 0, fmt.Errorf("redigo: unexpected type for Uint64, got type %T", reply)
  110. }
  111. // Float64 is a helper that converts a command reply to 64 bit float. If err is
  112. // not equal to nil, then Float64 returns 0, err. Otherwise, Float64 converts
  113. // the reply to a float64 as follows:
  114. //
  115. // Reply type Result
  116. // bulk string parsed reply, nil
  117. // nil 0, ErrNil
  118. // other 0, error
  119. func Float64(reply interface{}, err error) (float64, error) {
  120. if err != nil {
  121. return 0, err
  122. }
  123. switch reply := reply.(type) {
  124. case []byte:
  125. n, err := strconv.ParseFloat(string(reply), 64)
  126. return n, err
  127. case nil:
  128. return 0, ErrNil
  129. case Error:
  130. return 0, reply
  131. }
  132. return 0, fmt.Errorf("redigo: unexpected type for Float64, got type %T", reply)
  133. }
  134. // String is a helper that converts a command reply to a string. If err is not
  135. // equal to nil, then String returns "", err. Otherwise String converts the
  136. // reply to a string as follows:
  137. //
  138. // Reply type Result
  139. // bulk string string(reply), nil
  140. // simple string reply, nil
  141. // nil "", ErrNil
  142. // other "", error
  143. func String(reply interface{}, err error) (string, error) {
  144. if err != nil {
  145. return "", err
  146. }
  147. switch reply := reply.(type) {
  148. case []byte:
  149. return string(reply), nil
  150. case string:
  151. return reply, nil
  152. case nil:
  153. return "", ErrNil
  154. case Error:
  155. return "", reply
  156. }
  157. return "", fmt.Errorf("redigo: unexpected type for String, got type %T", reply)
  158. }
  159. // Bytes is a helper that converts a command reply to a slice of bytes. If err
  160. // is not equal to nil, then Bytes returns nil, err. Otherwise Bytes converts
  161. // the reply to a slice of bytes as follows:
  162. //
  163. // Reply type Result
  164. // bulk string reply, nil
  165. // simple string []byte(reply), nil
  166. // nil nil, ErrNil
  167. // other nil, error
  168. func Bytes(reply interface{}, err error) ([]byte, error) {
  169. if err != nil {
  170. return nil, err
  171. }
  172. switch reply := reply.(type) {
  173. case []byte:
  174. return reply, nil
  175. case string:
  176. return []byte(reply), nil
  177. case nil:
  178. return nil, ErrNil
  179. case Error:
  180. return nil, reply
  181. }
  182. return nil, fmt.Errorf("redigo: unexpected type for Bytes, got type %T", reply)
  183. }
  184. // Bool is a helper that converts a command reply to a boolean. If err is not
  185. // equal to nil, then Bool returns false, err. Otherwise Bool converts the
  186. // reply to boolean as follows:
  187. //
  188. // Reply type Result
  189. // integer value != 0, nil
  190. // bulk string strconv.ParseBool(reply)
  191. // nil false, ErrNil
  192. // other false, error
  193. func Bool(reply interface{}, err error) (bool, error) {
  194. if err != nil {
  195. return false, err
  196. }
  197. switch reply := reply.(type) {
  198. case int64:
  199. return reply != 0, nil
  200. case []byte:
  201. return strconv.ParseBool(string(reply))
  202. case nil:
  203. return false, ErrNil
  204. case Error:
  205. return false, reply
  206. }
  207. return false, fmt.Errorf("redigo: unexpected type for Bool, got type %T", reply)
  208. }
  209. // MultiBulk is a helper that converts an array command reply to a []interface{}.
  210. //
  211. // Deprecated: Use Values instead.
  212. func MultiBulk(reply interface{}, err error) ([]interface{}, error) { return Values(reply, err) }
  213. // Values is a helper that converts an array command reply to a []interface{}.
  214. // If err is not equal to nil, then Values returns nil, err. Otherwise, Values
  215. // converts the reply as follows:
  216. //
  217. // Reply type Result
  218. // array reply, nil
  219. // nil nil, ErrNil
  220. // other nil, error
  221. func Values(reply interface{}, err error) ([]interface{}, error) {
  222. if err != nil {
  223. return nil, err
  224. }
  225. switch reply := reply.(type) {
  226. case []interface{}:
  227. return reply, nil
  228. case nil:
  229. return nil, ErrNil
  230. case Error:
  231. return nil, reply
  232. }
  233. return nil, fmt.Errorf("redigo: unexpected type for Values, got type %T", reply)
  234. }
  235. func sliceHelper(reply interface{}, err error, name string, makeSlice func(int), assign func(int, interface{}) error) error {
  236. if err != nil {
  237. return err
  238. }
  239. switch reply := reply.(type) {
  240. case []interface{}:
  241. makeSlice(len(reply))
  242. for i := range reply {
  243. if reply[i] == nil {
  244. continue
  245. }
  246. if err := assign(i, reply[i]); err != nil {
  247. return err
  248. }
  249. }
  250. return nil
  251. case nil:
  252. return ErrNil
  253. case Error:
  254. return reply
  255. }
  256. return fmt.Errorf("redigo: unexpected type for %s, got type %T", name, reply)
  257. }
  258. // Float64s is a helper that converts an array command reply to a []float64. If
  259. // err is not equal to nil, then Float64s returns nil, err. Nil array items are
  260. // converted to 0 in the output slice. Floats64 returns an error if an array
  261. // item is not a bulk string or nil.
  262. func Float64s(reply interface{}, err error) ([]float64, error) {
  263. var result []float64
  264. err = sliceHelper(reply, err, "Float64s", func(n int) { result = make([]float64, n) }, func(i int, v interface{}) error {
  265. p, ok := v.([]byte)
  266. if !ok {
  267. return fmt.Errorf("redigo: unexpected element type for Floats64, got type %T", v)
  268. }
  269. f, err := strconv.ParseFloat(string(p), 64)
  270. result[i] = f
  271. return err
  272. })
  273. return result, err
  274. }
  275. // Strings is a helper that converts an array command reply to a []string. If
  276. // err is not equal to nil, then Strings returns nil, err. Nil array items are
  277. // converted to "" in the output slice. Strings returns an error if an array
  278. // item is not a bulk string or nil.
  279. func Strings(reply interface{}, err error) ([]string, error) {
  280. var result []string
  281. err = sliceHelper(reply, err, "Strings", func(n int) { result = make([]string, n) }, func(i int, v interface{}) error {
  282. switch v := v.(type) {
  283. case string:
  284. result[i] = v
  285. return nil
  286. case []byte:
  287. result[i] = string(v)
  288. return nil
  289. default:
  290. return fmt.Errorf("redigo: unexpected element type for Strings, got type %T", v)
  291. }
  292. })
  293. return result, err
  294. }
  295. // ByteSlices is a helper that converts an array command reply to a [][]byte.
  296. // If err is not equal to nil, then ByteSlices returns nil, err. Nil array
  297. // items are stay nil. ByteSlices returns an error if an array item is not a
  298. // bulk string or nil.
  299. func ByteSlices(reply interface{}, err error) ([][]byte, error) {
  300. var result [][]byte
  301. err = sliceHelper(reply, err, "ByteSlices", func(n int) { result = make([][]byte, n) }, func(i int, v interface{}) error {
  302. p, ok := v.([]byte)
  303. if !ok {
  304. return fmt.Errorf("redigo: unexpected element type for ByteSlices, got type %T", v)
  305. }
  306. result[i] = p
  307. return nil
  308. })
  309. return result, err
  310. }
  311. // Int64s is a helper that converts an array command reply to a []int64.
  312. // If err is not equal to nil, then Int64s returns nil, err. Nil array
  313. // items are stay nil. Int64s returns an error if an array item is not a
  314. // bulk string or nil.
  315. func Int64s(reply interface{}, err error) ([]int64, error) {
  316. var result []int64
  317. err = sliceHelper(reply, err, "Int64s", func(n int) { result = make([]int64, n) }, func(i int, v interface{}) error {
  318. switch v := v.(type) {
  319. case int64:
  320. result[i] = v
  321. return nil
  322. case []byte:
  323. n, err := strconv.ParseInt(string(v), 10, 64)
  324. result[i] = n
  325. return err
  326. default:
  327. return fmt.Errorf("redigo: unexpected element type for Int64s, got type %T", v)
  328. }
  329. })
  330. return result, err
  331. }
  332. // Ints is a helper that converts an array command reply to a []int.
  333. // If err is not equal to nil, then Ints returns nil, err. Nil array
  334. // items are stay nil. Ints returns an error if an array item is not a
  335. // bulk string or nil.
  336. func Ints(reply interface{}, err error) ([]int, error) {
  337. var result []int
  338. err = sliceHelper(reply, err, "Ints", func(n int) { result = make([]int, n) }, func(i int, v interface{}) error {
  339. switch v := v.(type) {
  340. case int64:
  341. n := int(v)
  342. if int64(n) != v {
  343. return strconv.ErrRange
  344. }
  345. result[i] = n
  346. return nil
  347. case []byte:
  348. n, err := strconv.Atoi(string(v))
  349. result[i] = n
  350. return err
  351. default:
  352. return fmt.Errorf("redigo: unexpected element type for Ints, got type %T", v)
  353. }
  354. })
  355. return result, err
  356. }
  357. // StringMap is a helper that converts an array of strings (alternating key, value)
  358. // into a map[string]string. The HGETALL and CONFIG GET commands return replies in this format.
  359. // Requires an even number of values in result.
  360. func StringMap(result interface{}, err error) (map[string]string, error) {
  361. values, err := Values(result, err)
  362. if err != nil {
  363. return nil, err
  364. }
  365. if len(values)%2 != 0 {
  366. return nil, errors.New("redigo: StringMap expects even number of values result")
  367. }
  368. m := make(map[string]string, len(values)/2)
  369. for i := 0; i < len(values); i += 2 {
  370. key, okKey := values[i].([]byte)
  371. value, okValue := values[i+1].([]byte)
  372. if !okKey || !okValue {
  373. return nil, errors.New("redigo: StringMap key not a bulk string value")
  374. }
  375. m[string(key)] = string(value)
  376. }
  377. return m, nil
  378. }
  379. // IntMap is a helper that converts an array of strings (alternating key, value)
  380. // into a map[string]int. The HGETALL commands return replies in this format.
  381. // Requires an even number of values in result.
  382. func IntMap(result interface{}, err error) (map[string]int, error) {
  383. values, err := Values(result, err)
  384. if err != nil {
  385. return nil, err
  386. }
  387. if len(values)%2 != 0 {
  388. return nil, errors.New("redigo: IntMap expects even number of values result")
  389. }
  390. m := make(map[string]int, len(values)/2)
  391. for i := 0; i < len(values); i += 2 {
  392. key, ok := values[i].([]byte)
  393. if !ok {
  394. return nil, errors.New("redigo: IntMap key not a bulk string value")
  395. }
  396. value, err := Int(values[i+1], nil)
  397. if err != nil {
  398. return nil, err
  399. }
  400. m[string(key)] = value
  401. }
  402. return m, nil
  403. }
  404. // Int64Map is a helper that converts an array of strings (alternating key, value)
  405. // into a map[string]int64. The HGETALL commands return replies in this format.
  406. // Requires an even number of values in result.
  407. func Int64Map(result interface{}, err error) (map[string]int64, error) {
  408. values, err := Values(result, err)
  409. if err != nil {
  410. return nil, err
  411. }
  412. if len(values)%2 != 0 {
  413. return nil, errors.New("redigo: Int64Map expects even number of values result")
  414. }
  415. m := make(map[string]int64, len(values)/2)
  416. for i := 0; i < len(values); i += 2 {
  417. key, ok := values[i].([]byte)
  418. if !ok {
  419. return nil, errors.New("redigo: Int64Map key not a bulk string value")
  420. }
  421. value, err := Int64(values[i+1], nil)
  422. if err != nil {
  423. return nil, err
  424. }
  425. m[string(key)] = value
  426. }
  427. return m, nil
  428. }
  429. // Positions is a helper that converts an array of positions (lat, long)
  430. // into a [][2]float64. The GEOPOS command returns replies in this format.
  431. func Positions(result interface{}, err error) ([]*[2]float64, error) {
  432. values, err := Values(result, err)
  433. if err != nil {
  434. return nil, err
  435. }
  436. positions := make([]*[2]float64, len(values))
  437. for i := range values {
  438. if values[i] == nil {
  439. continue
  440. }
  441. p, ok := values[i].([]interface{})
  442. if !ok {
  443. return nil, fmt.Errorf("redigo: unexpected element type for interface slice, got type %T", values[i])
  444. }
  445. if len(p) != 2 {
  446. return nil, fmt.Errorf("redigo: unexpected number of values for a member position, got %d", len(p))
  447. }
  448. lat, err := Float64(p[0], nil)
  449. if err != nil {
  450. return nil, err
  451. }
  452. long, err := Float64(p[1], nil)
  453. if err != nil {
  454. return nil, err
  455. }
  456. positions[i] = &[2]float64{lat, long}
  457. }
  458. return positions, nil
  459. }
  460. // Uint64s is a helper that converts an array command reply to a []uint64.
  461. // If err is not equal to nil, then Uint64s returns nil, err. Nil array
  462. // items are stay nil. Uint64s returns an error if an array item is not a
  463. // bulk string or nil.
  464. func Uint64s(reply interface{}, err error) ([]uint64, error) {
  465. var result []uint64
  466. err = sliceHelper(reply, err, "Uint64s", func(n int) { result = make([]uint64, n) }, func(i int, v interface{}) error {
  467. switch v := v.(type) {
  468. case uint64:
  469. result[i] = v
  470. return nil
  471. case []byte:
  472. n, err := strconv.ParseUint(string(v), 10, 64)
  473. result[i] = n
  474. return err
  475. default:
  476. return fmt.Errorf("redigo: unexpected element type for Uint64s, got type %T", v)
  477. }
  478. })
  479. return result, err
  480. }
  481. // Uint64Map is a helper that converts an array of strings (alternating key, value)
  482. // into a map[string]uint64. The HGETALL commands return replies in this format.
  483. // Requires an even number of values in result.
  484. func Uint64Map(result interface{}, err error) (map[string]uint64, error) {
  485. values, err := Values(result, err)
  486. if err != nil {
  487. return nil, err
  488. }
  489. if len(values)%2 != 0 {
  490. return nil, errors.New("redigo: Uint64Map expects even number of values result")
  491. }
  492. m := make(map[string]uint64, len(values)/2)
  493. for i := 0; i < len(values); i += 2 {
  494. key, ok := values[i].([]byte)
  495. if !ok {
  496. return nil, errors.New("redigo: Uint64Map key not a bulk string value")
  497. }
  498. value, err := Uint64(values[i+1], nil)
  499. if err != nil {
  500. return nil, err
  501. }
  502. m[string(key)] = value
  503. }
  504. return m, nil
  505. }
  506. // SlowLogs is a helper that parse the SLOWLOG GET command output and
  507. // return the array of SlowLog
  508. func SlowLogs(result interface{}, err error) ([]SlowLog, error) {
  509. rawLogs, err := Values(result, err)
  510. if err != nil {
  511. return nil, err
  512. }
  513. logs := make([]SlowLog, len(rawLogs))
  514. for i, rawLog := range rawLogs {
  515. rawLog, ok := rawLog.([]interface{})
  516. if !ok {
  517. return nil, errors.New("redigo: slowlog element is not an array")
  518. }
  519. var log SlowLog
  520. if len(rawLog) < 4 {
  521. return nil, errors.New("redigo: slowlog element has less than four elements")
  522. }
  523. log.ID, ok = rawLog[0].(int64)
  524. if !ok {
  525. return nil, errors.New("redigo: slowlog element[0] not an int64")
  526. }
  527. timestamp, ok := rawLog[1].(int64)
  528. if !ok {
  529. return nil, errors.New("redigo: slowlog element[1] not an int64")
  530. }
  531. log.Time = time.Unix(timestamp, 0)
  532. duration, ok := rawLog[2].(int64)
  533. if !ok {
  534. return nil, errors.New("redigo: slowlog element[2] not an int64")
  535. }
  536. log.ExecutionTime = time.Duration(duration) * time.Microsecond
  537. log.Args, err = Strings(rawLog[3], nil)
  538. if err != nil {
  539. return nil, fmt.Errorf("redigo: slowlog element[3] is not array of string. actual error is : %s", err.Error())
  540. }
  541. if len(rawLog) >= 6 {
  542. log.ClientAddr, err = String(rawLog[4], nil)
  543. if err != nil {
  544. return nil, fmt.Errorf("redigo: slowlog element[4] is not a string. actual error is : %s", err.Error())
  545. }
  546. log.ClientName, err = String(rawLog[5], nil)
  547. if err != nil {
  548. return nil, fmt.Errorf("redigo: slowlog element[5] is not a string. actual error is : %s", err.Error())
  549. }
  550. }
  551. logs[i] = log
  552. }
  553. return logs, nil
  554. }