fake_custom_store.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cache
  14. // FakeStore lets you define custom functions for store operations
  15. type FakeCustomStore struct {
  16. AddFunc func(obj interface{}) error
  17. UpdateFunc func(obj interface{}) error
  18. DeleteFunc func(obj interface{}) error
  19. ListFunc func() []interface{}
  20. ListKeysFunc func() []string
  21. GetFunc func(obj interface{}) (item interface{}, exists bool, err error)
  22. GetByKeyFunc func(key string) (item interface{}, exists bool, err error)
  23. ReplaceFunc func(list []interface{}, resourceVerion string) error
  24. ResyncFunc func() error
  25. }
  26. // Add calls the custom Add function if defined
  27. func (f *FakeCustomStore) Add(obj interface{}) error {
  28. if f.AddFunc != nil {
  29. return f.AddFunc(obj)
  30. }
  31. return nil
  32. }
  33. // Update calls the custom Update function if defined
  34. func (f *FakeCustomStore) Update(obj interface{}) error {
  35. if f.UpdateFunc != nil {
  36. return f.Update(obj)
  37. }
  38. return nil
  39. }
  40. // Delete calls the custom Delete function if defined
  41. func (f *FakeCustomStore) Delete(obj interface{}) error {
  42. if f.DeleteFunc != nil {
  43. return f.DeleteFunc(obj)
  44. }
  45. return nil
  46. }
  47. // List calls the custom List function if defined
  48. func (f *FakeCustomStore) List() []interface{} {
  49. if f.ListFunc != nil {
  50. return f.ListFunc()
  51. }
  52. return nil
  53. }
  54. // ListKeys calls the custom ListKeys function if defined
  55. func (f *FakeCustomStore) ListKeys() []string {
  56. if f.ListKeysFunc != nil {
  57. return f.ListKeysFunc()
  58. }
  59. return nil
  60. }
  61. // Get calls the custom Get function if defined
  62. func (f *FakeCustomStore) Get(obj interface{}) (item interface{}, exists bool, err error) {
  63. if f.GetFunc != nil {
  64. return f.GetFunc(obj)
  65. }
  66. return nil, false, nil
  67. }
  68. // GetByKey calls the custom GetByKey function if defined
  69. func (f *FakeCustomStore) GetByKey(key string) (item interface{}, exists bool, err error) {
  70. if f.GetByKeyFunc != nil {
  71. return f.GetByKeyFunc(key)
  72. }
  73. return nil, false, nil
  74. }
  75. // Replace calls the custom Replace function if defined
  76. func (f *FakeCustomStore) Replace(list []interface{}, resourceVersion string) error {
  77. if f.ReplaceFunc != nil {
  78. return f.ReplaceFunc(list, resourceVersion)
  79. }
  80. return nil
  81. }
  82. // Resync calls the custom Resync function if defined
  83. func (f *FakeCustomStore) Resync() error {
  84. if f.ResyncFunc != nil {
  85. return f.ResyncFunc()
  86. }
  87. return nil
  88. }