reply.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. var ErrNil = errors.New("redigo: nil returned")
  21. // Int is a helper that converts a command reply to an integer. If err is not
  22. // equal to nil, then Int returns 0, err. Otherwise, Int converts the
  23. // reply to an int as follows:
  24. //
  25. // Reply type Result
  26. // integer int(reply), nil
  27. // bulk strconv.ParseInt(reply, 10, 0)
  28. // nil 0, ErrNil
  29. // other 0, error
  30. func Int(reply interface{}, err error) (int, error) {
  31. if err != nil {
  32. return 0, err
  33. }
  34. switch reply := reply.(type) {
  35. case int64:
  36. x := int(reply)
  37. if int64(x) != reply {
  38. return 0, strconv.ErrRange
  39. }
  40. return x, nil
  41. case []byte:
  42. n, err := strconv.ParseInt(string(reply), 10, 0)
  43. return int(n), err
  44. case nil:
  45. return 0, ErrNil
  46. case Error:
  47. return 0, reply
  48. }
  49. return 0, fmt.Errorf("redigo: unexpected type for Int, got type %T", reply)
  50. }
  51. // String is a helper that converts a command reply to a string. If err is not
  52. // equal to nil, then String returns "", err. Otherwise String converts the
  53. // reply to a string as follows:
  54. //
  55. // Reply type Result
  56. // bulk string(reply), nil
  57. // string reply, nil
  58. // nil "", ErrNil
  59. // other "", error
  60. func String(reply interface{}, err error) (string, error) {
  61. if err != nil {
  62. return "", err
  63. }
  64. switch reply := reply.(type) {
  65. case []byte:
  66. return string(reply), nil
  67. case string:
  68. return reply, nil
  69. case nil:
  70. return "", ErrNil
  71. case Error:
  72. return "", reply
  73. }
  74. return "", fmt.Errorf("redigo: unexpected type for String, got type %T", reply)
  75. }
  76. // Bytes is a helper that converts a command reply to a slice of bytes. If err
  77. // is not equal to nil, then Bytes returns nil, err. Otherwise Bytes converts
  78. // the reply to a slice of bytes as follows:
  79. //
  80. // Reply type Result
  81. // bulk reply, nil
  82. // string []byte(reply), nil
  83. // nil nil, ErrNil
  84. // other nil, error
  85. func Bytes(reply interface{}, err error) ([]byte, error) {
  86. if err != nil {
  87. return nil, err
  88. }
  89. switch reply := reply.(type) {
  90. case []byte:
  91. return reply, nil
  92. case string:
  93. return []byte(reply), nil
  94. case nil:
  95. return nil, ErrNil
  96. case Error:
  97. return nil, reply
  98. }
  99. return nil, fmt.Errorf("redigo: unexpected type for Bytes, got type %T", reply)
  100. }
  101. // Bool is a helper that converts a command reply to a boolean. If err is not
  102. // equal to nil, then Bool returns false, err. Otherwise Bool converts the
  103. // reply to boolean as follows:
  104. //
  105. // Reply type Result
  106. // integer value != 0, nil
  107. // bulk strconv.ParseBool(reply)
  108. // nil false, ErrNil
  109. // other false, error
  110. func Bool(reply interface{}, err error) (bool, error) {
  111. if err != nil {
  112. return false, err
  113. }
  114. switch reply := reply.(type) {
  115. case int64:
  116. return reply != 0, nil
  117. case []byte:
  118. return strconv.ParseBool(string(reply))
  119. case nil:
  120. return false, ErrNil
  121. case Error:
  122. return false, reply
  123. }
  124. return false, fmt.Errorf("redigo: unexpected type for Bool, got type %T", reply)
  125. }
  126. // MultiBulk is deprecated. Use Values.
  127. func MultiBulk(reply interface{}, err error) ([]interface{}, error) { return Values(reply, err) }
  128. // Values is a helper that converts a multi-bulk command reply to a
  129. // []interface{}. If err is not equal to nil, then Values returns nil, err.
  130. // Otherwise, Multi converts the reply as follows:
  131. //
  132. // Reply type Result
  133. // multi-bulk reply, nil
  134. // nil nil, ErrNil
  135. // other nil, error
  136. func Values(reply interface{}, err error) ([]interface{}, error) {
  137. if err != nil {
  138. return nil, err
  139. }
  140. switch reply := reply.(type) {
  141. case []interface{}:
  142. return reply, nil
  143. case nil:
  144. return nil, ErrNil
  145. case Error:
  146. return nil, reply
  147. }
  148. return nil, fmt.Errorf("redigo: unexpected type for Multi, got type %T", reply)
  149. }