35 lines
586 B
Go
35 lines
586 B
Go
package json
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"github.com/abelith/spellbook/internal/adapters/exporter"
|
|
"github.com/abelith/spellbook/internal/models"
|
|
"io"
|
|
)
|
|
|
|
func init() {
|
|
exporter.Register("jsonl", &JsonlExporter{})
|
|
}
|
|
|
|
type JsonlExporter struct{}
|
|
|
|
func (je *JsonlExporter) Export(ctx context.Context, ch <-chan models.Spell, w io.Writer) error {
|
|
encoder := json.NewEncoder(w)
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case spell, ok := <-ch:
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
if err := encoder.Encode(&spell); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|