reply.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. )
  19. var (
  20. errUnexpectedReplyType = errors.New("redigo: unexpected reply type")
  21. )
  22. // Int is a helper that converts a Redis reply to an int. Integer replies are
  23. // returned directly. Bulk replies are interpreted as signed decimal strings.
  24. // If err is not equal to nil or the reply is not an integer or bulk value,
  25. // then Int returns an error.
  26. func Int(v interface{}, err error) (int, error) {
  27. if err != nil {
  28. return 0, err
  29. }
  30. switch v := v.(type) {
  31. case int64:
  32. return int(v), nil
  33. case []byte:
  34. n, err := strconv.ParseInt(string(v), 10, 0)
  35. return int(n), err
  36. case Error:
  37. return 0, v
  38. }
  39. return 0, errUnexpectedReplyType
  40. }
  41. // String is a helper that converts a Redis reply to a string. Bulk replies are
  42. // returned as a string. Integer replies are formatted as as a signed decimal
  43. // string. If err is not equal to nil or the reply is not an integer or bulk
  44. // value, then Int returns an error.
  45. func String(v interface{}, err error) (string, error) {
  46. if err != nil {
  47. return "", err
  48. }
  49. switch v := v.(type) {
  50. case int64:
  51. return strconv.FormatInt(v, 10), nil
  52. case []byte:
  53. return string(v), nil
  54. case Error:
  55. return "", v
  56. }
  57. return "", errUnexpectedReplyType
  58. }
  59. // Bytes is a helper that converts a Redis reply to slice of bytes. Bulk
  60. // replies are returned as is. Integer replies are formatted as as a signed
  61. // decimal string. If err is not equal to nil or the reply is not an integer
  62. // or bulk value, then Int returns an error.
  63. func Bytes(v interface{}, err error) ([]byte, error) {
  64. if err != nil {
  65. return nil, err
  66. }
  67. switch v := v.(type) {
  68. case int64:
  69. return strconv.AppendInt(nil, v, 10), nil
  70. case []byte:
  71. return v, nil
  72. case Error:
  73. return nil, v
  74. }
  75. return nil, errUnexpectedReplyType
  76. }
  77. // Bool is a helper that converts a Redis reply eo a bool. Bool returns true if
  78. // the reply is the integer 1 and false if the reply is the integer 0. If err
  79. // is not equal to nil or the reply is not the integer 0 or 1, then Bool
  80. // returns an error.
  81. func Bool(v interface{}, err error) (bool, error) {
  82. if err != nil {
  83. return false, err
  84. }
  85. switch v := v.(type) {
  86. case int64:
  87. switch v {
  88. case 0:
  89. return false, nil
  90. case 1:
  91. return true, nil
  92. }
  93. case Error:
  94. return false, v
  95. }
  96. return false, errUnexpectedReplyType
  97. }
  98. // Subscribe represents a subscribe or unsubscribe notification.
  99. type Subscription struct {
  100. // Kind is "subscribe", "unsubscribe", "psubscribe" or "punsubscribe"
  101. Kind string
  102. // The channel that was changed.
  103. Channel string
  104. // The current number of subscriptions for connection.
  105. Count int
  106. }
  107. // Message represents a message notification.
  108. type Message struct {
  109. // The originating channel.
  110. Channel string
  111. // The message data.
  112. Data []byte
  113. }
  114. // Notification is a helper that returns a pub/sub notification as a
  115. // Subscription or a Message.
  116. func Notification(v interface{}, err error) (interface{}, error) {
  117. if err != nil {
  118. return nil, err
  119. }
  120. err = errUnexpectedReplyType
  121. s, ok := v.([]interface{})
  122. if !ok || len(s) != 3 {
  123. return nil, errUnexpectedReplyType
  124. }
  125. b, ok := s[0].([]byte)
  126. if !ok {
  127. return nil, errUnexpectedReplyType
  128. }
  129. kind := string(b)
  130. b, ok = s[1].([]byte)
  131. if !ok {
  132. return nil, errUnexpectedReplyType
  133. }
  134. channel := string(b)
  135. if kind == "message" {
  136. data, ok := s[2].([]byte)
  137. if !ok {
  138. return nil, errUnexpectedReplyType
  139. }
  140. return Message{channel, data}, nil
  141. }
  142. count, ok := s[2].(int64)
  143. if !ok {
  144. return nil, errUnexpectedReplyType
  145. }
  146. return Subscription{kind, channel, int(count)}, nil
  147. }