Files
spellbook/internal/adapters/importer/importer.go
T
2026-08-21 15:27:37 +05:00

44 lines
812 B
Go

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