From b81ce853200a05cc8d72bcd1e372acd20ab681d2 Mon Sep 17 00:00:00 2001 From: Giorgos Komninos Date: Fri, 8 Sep 2023 20:54:12 +0300 Subject: [PATCH] adds jsonwriter --- adapters/writers/jsonwriter/jsonwriter.go | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 adapters/writers/jsonwriter/jsonwriter.go diff --git a/adapters/writers/jsonwriter/jsonwriter.go b/adapters/writers/jsonwriter/jsonwriter.go new file mode 100644 index 0000000..307dfa5 --- /dev/null +++ b/adapters/writers/jsonwriter/jsonwriter.go @@ -0,0 +1,47 @@ +package jsonwriter + +import ( + "context" + "encoding/json" + "io" + + "github.com/gosom/scrapemate" +) + +var _ scrapemate.ResultWriter = (*jsonWriter)(nil) + +type jsonWriter struct { + enc *json.Encoder +} + +func NewJSONWriter(w io.Writer) scrapemate.ResultWriter { + enc := json.NewEncoder(w) + return &jsonWriter{enc: enc} +} + +func (c *jsonWriter) Run(_ context.Context, in <-chan scrapemate.Result) error { + for result := range in { + items := asSlice(result.Data) + + for i := range items { + if err := c.enc.Encode(items[i]); err != nil { + return err + } + } + } + + return nil +} + +func asSlice(t any) []any { + isSlice, ok := t.([]any) + if ok { + return isSlice + } + + var elements []any + + elements = append(elements, t) + + return elements +}