helpers_kv.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
  2. // See LICENSE.txt for license information.
  3. package plugin
  4. import (
  5. "encoding/json"
  6. "strings"
  7. "github.com/pkg/errors"
  8. )
  9. // KVSetJSON implements Helpers.KVSetJSON.
  10. func (p *HelpersImpl) KVSetJSON(key string, value interface{}) error {
  11. err := p.ensureServerVersion("5.2.0")
  12. if err != nil {
  13. return err
  14. }
  15. data, err := json.Marshal(value)
  16. if err != nil {
  17. return err
  18. }
  19. appErr := p.API.KVSet(key, data)
  20. if appErr != nil {
  21. return appErr
  22. }
  23. return nil
  24. }
  25. // KVCompareAndSetJSON implements Helpers.KVCompareAndSetJSON.
  26. func (p *HelpersImpl) KVCompareAndSetJSON(key string, oldValue interface{}, newValue interface{}) (bool, error) {
  27. var err error
  28. err = p.ensureServerVersion("5.12.0")
  29. if err != nil {
  30. return false, err
  31. }
  32. var oldData, newData []byte
  33. if oldValue != nil {
  34. oldData, err = json.Marshal(oldValue)
  35. if err != nil {
  36. return false, errors.Wrap(err, "unable to marshal old value")
  37. }
  38. }
  39. if newValue != nil {
  40. newData, err = json.Marshal(newValue)
  41. if err != nil {
  42. return false, errors.Wrap(err, "unable to marshal new value")
  43. }
  44. }
  45. set, appErr := p.API.KVCompareAndSet(key, oldData, newData)
  46. if appErr != nil {
  47. return set, appErr
  48. }
  49. return set, nil
  50. }
  51. // KVCompareAndDeleteJSON implements Helpers.KVCompareAndDeleteJSON.
  52. func (p *HelpersImpl) KVCompareAndDeleteJSON(key string, oldValue interface{}) (bool, error) {
  53. var err error
  54. err = p.ensureServerVersion("5.16.0")
  55. if err != nil {
  56. return false, err
  57. }
  58. var oldData []byte
  59. if oldValue != nil {
  60. oldData, err = json.Marshal(oldValue)
  61. if err != nil {
  62. return false, errors.Wrap(err, "unable to marshal old value")
  63. }
  64. }
  65. deleted, appErr := p.API.KVCompareAndDelete(key, oldData)
  66. if appErr != nil {
  67. return deleted, appErr
  68. }
  69. return deleted, nil
  70. }
  71. // KVGetJSON implements Helpers.KVGetJSON.
  72. func (p *HelpersImpl) KVGetJSON(key string, value interface{}) (bool, error) {
  73. err := p.ensureServerVersion("5.2.0")
  74. if err != nil {
  75. return false, err
  76. }
  77. data, appErr := p.API.KVGet(key)
  78. if appErr != nil {
  79. return false, appErr
  80. }
  81. if data == nil {
  82. return false, nil
  83. }
  84. err = json.Unmarshal(data, value)
  85. if err != nil {
  86. return false, err
  87. }
  88. return true, nil
  89. }
  90. // KVSetWithExpiryJSON is a wrapper around KVSetWithExpiry to simplify atomically writing a JSON object with expiry to the key value store.
  91. func (p *HelpersImpl) KVSetWithExpiryJSON(key string, value interface{}, expireInSeconds int64) error {
  92. err := p.ensureServerVersion("5.6.0")
  93. if err != nil {
  94. return err
  95. }
  96. data, err := json.Marshal(value)
  97. if err != nil {
  98. return err
  99. }
  100. appErr := p.API.KVSetWithExpiry(key, data, expireInSeconds)
  101. if appErr != nil {
  102. return appErr
  103. }
  104. return nil
  105. }
  106. type kvListOptions struct {
  107. checkers []func(key string) (keep bool, err error)
  108. }
  109. func (o *kvListOptions) checkAll(key string) (keep bool, err error) {
  110. for _, check := range o.checkers {
  111. keep, err := check(key)
  112. if err != nil {
  113. return false, err
  114. }
  115. if !keep {
  116. return false, nil
  117. }
  118. }
  119. // key made it through all checkers
  120. return true, nil
  121. }
  122. // KVListOption represents a single input option for KVListWithOptions
  123. type KVListOption func(*kvListOptions)
  124. // WithPrefix only return keys that start with the given string.
  125. func WithPrefix(prefix string) KVListOption {
  126. return WithChecker(func(key string) (keep bool, err error) {
  127. return strings.HasPrefix(key, prefix), nil
  128. })
  129. }
  130. // WithChecker allows for a custom filter function to determine which keys to return.
  131. // Returning true will keep the key and false will filter it out. Returning an error
  132. // will halt KVListWithOptions immediately and pass the error up (with no other results).
  133. func WithChecker(f func(key string) (keep bool, err error)) KVListOption {
  134. return func(args *kvListOptions) {
  135. args.checkers = append(args.checkers, f)
  136. }
  137. }
  138. // kvListPerPage is the number of keys KVListWithOptions gets per request
  139. const kvListPerPage = 100
  140. // KVListWithOptions implements Helpers.KVListWithOptions.
  141. func (p *HelpersImpl) KVListWithOptions(options ...KVListOption) ([]string, error) {
  142. err := p.ensureServerVersion("5.6.0")
  143. if err != nil {
  144. return nil, err
  145. }
  146. // convert functional options into args struct
  147. args := &kvListOptions{}
  148. for _, opt := range options {
  149. opt(args)
  150. }
  151. ret := make([]string, 0)
  152. // get our keys a batch at a time, filter out the ones we don't want based on our args
  153. // any errors will hault the whole process and return the error raw
  154. for i := 0; ; i++ {
  155. keys, appErr := p.API.KVList(i, kvListPerPage)
  156. if appErr != nil {
  157. return nil, appErr
  158. }
  159. if len(args.checkers) == 0 {
  160. // no checkers, just append the whole block at once
  161. ret = append(ret, keys...)
  162. } else {
  163. // we have a filter, so check each key, all checkers must say key
  164. // for us to keep a key
  165. for _, key := range keys {
  166. keep, err := args.checkAll(key)
  167. if err != nil {
  168. return nil, err
  169. }
  170. if !keep {
  171. continue
  172. }
  173. // didn't get filtered out, add to our return
  174. ret = append(ret, key)
  175. }
  176. }
  177. if len(keys) < kvListPerPage {
  178. break
  179. }
  180. }
  181. return ret, nil
  182. }