Files
spellbook/internal/adapters/exporter/exporter.go
T

44 lines
780 B
Go

package exporter
import (
"context"
"fmt"
"github.com/abelith/spellbook/internal/models"
"io"
"sync"
)
type Exporter interface {
Export(ctx context.Context, ch <-chan models.Spell, w io.Writer) error
}
var (
exporters = make(map[string]Exporter)
mu sync.Mutex
)
func Register(name string, exp Exporter) {
mu.Lock()
defer mu.Unlock()
if exp == nil {
panic("exporter: Exporter cannot be nil")
}
if _, ok := exporters[name]; ok {
panic("exporter: Register called twice for " + name)
}
exporters[name] = exp
}
func Export(ctx context.Context, name string, ch <-chan models.Spell, w io.Writer) error {
mu.Lock()
exp, ok := exporters[name]
mu.Unlock()
if !ok {
return fmt.Errorf("exporter %s not found", name)
}
return exp.Export(ctx, ch, w)
}