You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

394 lines
11 KiB

  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. // Strings is a helper that converts an array command reply to a []string. If
  233. // err is not equal to nil, then Strings returns nil, err. Nil array items are
  234. // converted to "" in the output slice. Strings returns an error if an array
  235. // item is not a bulk string or nil.
  236. func Strings(reply interface{}, err error) ([]string, error) {
  237. if err != nil {
  238. return nil, err
  239. }
  240. switch reply := reply.(type) {
  241. case []interface{}:
  242. result := make([]string, len(reply))
  243. for i := range reply {
  244. if reply[i] == nil {
  245. continue
  246. }
  247. p, ok := reply[i].([]byte)
  248. if !ok {
  249. return nil, fmt.Errorf("redigo: unexpected element type for Strings, got type %T", reply[i])
  250. }
  251. result[i] = string(p)
  252. }
  253. return result, nil
  254. case nil:
  255. return nil, ErrNil
  256. case Error:
  257. return nil, reply
  258. }
  259. return nil, fmt.Errorf("redigo: unexpected type for Strings, got type %T", reply)
  260. }
  261. // ByteSlices is a helper that converts an array command reply to a [][]byte.
  262. // If err is not equal to nil, then ByteSlices returns nil, err. Nil array
  263. // items are stay nil. ByteSlices returns an error if an array item is not a
  264. // bulk string or nil.
  265. func ByteSlices(reply interface{}, err error) ([][]byte, error) {
  266. if err != nil {
  267. return nil, err
  268. }
  269. switch reply := reply.(type) {
  270. case []interface{}:
  271. result := make([][]byte, len(reply))
  272. for i := range reply {
  273. if reply[i] == nil {
  274. continue
  275. }
  276. p, ok := reply[i].([]byte)
  277. if !ok {
  278. return nil, fmt.Errorf("redigo: unexpected element type for ByteSlices, got type %T", reply[i])
  279. }
  280. result[i] = p
  281. }
  282. return result, nil
  283. case nil:
  284. return nil, ErrNil
  285. case Error:
  286. return nil, reply
  287. }
  288. return nil, fmt.Errorf("redigo: unexpected type for ByteSlices, got type %T", reply)
  289. }
  290. // Ints is a helper that converts an array command reply to a []int. If
  291. // err is not equal to nil, then Ints returns nil, err.
  292. func Ints(reply interface{}, err error) ([]int, error) {
  293. var ints []int
  294. values, err := Values(reply, err)
  295. if err != nil {
  296. return ints, err
  297. }
  298. if err := ScanSlice(values, &ints); err != nil {
  299. return ints, err
  300. }
  301. return ints, nil
  302. }
  303. // StringMap is a helper that converts an array of strings (alternating key, value)
  304. // into a map[string]string. The HGETALL and CONFIG GET commands return replies in this format.
  305. // Requires an even number of values in result.
  306. func StringMap(result interface{}, err error) (map[string]string, error) {
  307. values, err := Values(result, err)
  308. if err != nil {
  309. return nil, err
  310. }
  311. if len(values)%2 != 0 {
  312. return nil, errors.New("redigo: StringMap expects even number of values result")
  313. }
  314. m := make(map[string]string, len(values)/2)
  315. for i := 0; i < len(values); i += 2 {
  316. key, okKey := values[i].([]byte)
  317. value, okValue := values[i+1].([]byte)
  318. if !okKey || !okValue {
  319. return nil, errors.New("redigo: ScanMap key not a bulk string value")
  320. }
  321. m[string(key)] = string(value)
  322. }
  323. return m, nil
  324. }
  325. // IntMap is a helper that converts an array of strings (alternating key, value)
  326. // into a map[string]int. The HGETALL commands return replies in this format.
  327. // Requires an even number of values in result.
  328. func IntMap(result interface{}, err error) (map[string]int, error) {
  329. values, err := Values(result, err)
  330. if err != nil {
  331. return nil, err
  332. }
  333. if len(values)%2 != 0 {
  334. return nil, errors.New("redigo: IntMap expects even number of values result")
  335. }
  336. m := make(map[string]int, len(values)/2)
  337. for i := 0; i < len(values); i += 2 {
  338. key, ok := values[i].([]byte)
  339. if !ok {
  340. return nil, errors.New("redigo: ScanMap key not a bulk string value")
  341. }
  342. value, err := Int(values[i+1], nil)
  343. if err != nil {
  344. return nil, err
  345. }
  346. m[string(key)] = value
  347. }
  348. return m, nil
  349. }
  350. // Int64Map is a helper that converts an array of strings (alternating key, value)
  351. // into a map[string]int64. The HGETALL commands return replies in this format.
  352. // Requires an even number of values in result.
  353. func Int64Map(result interface{}, err error) (map[string]int64, error) {
  354. values, err := Values(result, err)
  355. if err != nil {
  356. return nil, err
  357. }
  358. if len(values)%2 != 0 {
  359. return nil, errors.New("redigo: Int64Map expects even number of values result")
  360. }
  361. m := make(map[string]int64, len(values)/2)
  362. for i := 0; i < len(values); i += 2 {
  363. key, ok := values[i].([]byte)
  364. if !ok {
  365. return nil, errors.New("redigo: ScanMap key not a bulk string value")
  366. }
  367. value, err := Int64(values[i+1], nil)
  368. if err != nil {
  369. return nil, err
  370. }
  371. m[string(key)] = value
  372. }
  373. return m, nil
  374. }