mock_sink.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2021-2023 EMQ Technologies Co., Ltd.
  2. //
  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. //
  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,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mocknode
  15. import (
  16. "github.com/lf-edge/ekuiper/pkg/api"
  17. )
  18. type MockSink struct {
  19. results [][]byte
  20. }
  21. func NewMockSink() *MockSink {
  22. m := &MockSink{}
  23. return m
  24. }
  25. func (m *MockSink) Open(ctx api.StreamContext) error {
  26. log := ctx.GetLogger()
  27. log.Debugln("Opening mock sink")
  28. m.results = make([][]byte, 0)
  29. return nil
  30. }
  31. func (m *MockSink) Collect(ctx api.StreamContext, item interface{}) error {
  32. logger := ctx.GetLogger()
  33. if v, _, err := ctx.TransformOutput(item); err == nil {
  34. logger.Debugf("mock sink receive %s", item)
  35. m.results = append(m.results, v)
  36. } else {
  37. logger.Info("mock sink transform data error: %v", err)
  38. }
  39. return nil
  40. }
  41. func (m *MockSink) Close(_ api.StreamContext) error {
  42. // do nothing
  43. return nil
  44. }
  45. func (m *MockSink) Configure(_ map[string]interface{}) error {
  46. return nil
  47. }
  48. func (m *MockSink) GetResults() [][]byte {
  49. return m.results
  50. }