mock_sink.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. "fmt"
  17. "github.com/lf-edge/ekuiper/pkg/api"
  18. )
  19. type MockSink struct {
  20. results [][]byte
  21. }
  22. func NewMockSink() *MockSink {
  23. m := &MockSink{}
  24. return m
  25. }
  26. func (m *MockSink) Open(ctx api.StreamContext) error {
  27. log := ctx.GetLogger()
  28. log.Debugln("Opening mock sink")
  29. m.results = make([][]byte, 0)
  30. return nil
  31. }
  32. func (m *MockSink) Collect(ctx api.StreamContext, item interface{}) error {
  33. logger := ctx.GetLogger()
  34. fmt.Println("mock sink receive ", item)
  35. if v, _, err := ctx.TransformOutput(item); err == nil {
  36. logger.Debugf("mock sink receive %s", item)
  37. m.results = append(m.results, v)
  38. } else {
  39. logger.Info("mock sink transform data error: %v", err)
  40. }
  41. return nil
  42. }
  43. func (m *MockSink) Close(ctx api.StreamContext) error {
  44. // do nothing
  45. return nil
  46. }
  47. func (m *MockSink) Configure(props map[string]interface{}) error {
  48. return nil
  49. }
  50. func (m *MockSink) GetResults() [][]byte {
  51. return m.results
  52. }