func.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 collector
  15. import (
  16. "errors"
  17. "github.com/lf-edge/ekuiper/pkg/api"
  18. )
  19. // CollectorFunc is a function used to colllect
  20. // incoming stream data. It can be used as a
  21. // stream sink.
  22. type CollectorFunc func(api.StreamContext, interface{}) error
  23. // FuncCollector is a colletor that uses a function
  24. // to collect data. The specified function must be
  25. // of type:
  26. //
  27. // CollectorFunc
  28. type FuncCollector struct {
  29. f CollectorFunc
  30. }
  31. // Func creates a new value *FuncCollector that
  32. // will use the specified function parameter to
  33. // collect streaming data.
  34. func Func(f CollectorFunc) *FuncCollector {
  35. return &FuncCollector{f: f}
  36. }
  37. func (c *FuncCollector) Configure(props map[string]interface{}) error {
  38. // do nothing
  39. return nil
  40. }
  41. // Open is the starting point that starts the collector
  42. func (c *FuncCollector) Open(ctx api.StreamContext) error {
  43. log := ctx.GetLogger()
  44. log.Infoln("Opening func collector")
  45. if c.f == nil {
  46. return errors.New("func collector missing function")
  47. }
  48. return nil
  49. }
  50. func (c *FuncCollector) Collect(ctx api.StreamContext, item interface{}) error {
  51. return c.f(ctx, item)
  52. }
  53. func (c *FuncCollector) Close(api.StreamContext) error {
  54. return nil
  55. }