func.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2021 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. // CollectorFunc
  27. type FuncCollector struct {
  28. f CollectorFunc
  29. }
  30. // Func creates a new value *FuncCollector that
  31. // will use the specified function parameter to
  32. // collect streaming data.
  33. func Func(f CollectorFunc) *FuncCollector {
  34. return &FuncCollector{f: f}
  35. }
  36. func (c *FuncCollector) Configure(props map[string]interface{}) error {
  37. //do nothing
  38. return nil
  39. }
  40. // Open is the starting point that starts the collector
  41. func (c *FuncCollector) Open(ctx api.StreamContext) error {
  42. log := ctx.GetLogger()
  43. log.Infoln("Opening func collector")
  44. if c.f == nil {
  45. return errors.New("func collector missing function")
  46. }
  47. return nil
  48. }
  49. func (c *FuncCollector) Collect(ctx api.StreamContext, item interface{}) error {
  50. return c.f(ctx, item)
  51. }
  52. func (c *FuncCollector) Close(api.StreamContext) error {
  53. return nil
  54. }