sink.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2021 INTECH Process Automation 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 shared
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. )
  20. type sink struct {
  21. id string
  22. ch *channels
  23. }
  24. func (s *sink) Open(ctx api.StreamContext) error {
  25. return nil
  26. }
  27. func (s *sink) Configure(props map[string]interface{}) error {
  28. return nil
  29. }
  30. func (s *sink) Collect(ctx api.StreamContext, data interface{}) error {
  31. if b, casted := data.([]byte); casted {
  32. d, err := toMap(b)
  33. if err != nil {
  34. return err
  35. }
  36. for _, el := range d {
  37. for _, c := range s.ch.consumers {
  38. c <- el
  39. }
  40. }
  41. return nil
  42. }
  43. return fmt.Errorf("unrecognized format of %s", data)
  44. }
  45. func (s *sink) Close(ctx api.StreamContext) error {
  46. return closeSink(s.id)
  47. }
  48. func toMap(data []byte) ([]map[string]interface{}, error) {
  49. res := make([]map[string]interface{}, 0)
  50. err := json.Unmarshal(data, &res)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return res, nil
  55. }