feat: import interface
This commit is contained in:
1 parent
f97e9b0aa9
commit
69af7eaee3
3 files changed
+66
No files matched your search
@@ -0,0 +1,43 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package application
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/abelith/spellbook/internal/adapters/exporter"
|
"github.com/abelith/spellbook/internal/adapters/exporter"
|
||||||
|
"github.com/abelith/spellbook/internal/adapters/importer"
|
||||||
"github.com/abelith/spellbook/internal/models"
|
"github.com/abelith/spellbook/internal/models"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
@@ -93,3 +94,24 @@ func (a *Application) ExportSpells(
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Application) ImportSpells(
|
||||||
|
ctx context.Context,
|
||||||
|
name string,
|
||||||
|
r io.Reader,
|
||||||
|
) error {
|
||||||
|
ch, errCh, err := importer.Import(ctx, name, r)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := a.spellCmd.Import(ctx, ch); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := <-errCh; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@ type Application interface {
|
|||||||
DeleteSpell(ctx context.Context, spellName string) error
|
DeleteSpell(ctx context.Context, spellName string) error
|
||||||
CastSpell(ctx context.Context, spellCast models.SpellCast) (models.CastResult, error)
|
CastSpell(ctx context.Context, spellCast models.SpellCast) (models.CastResult, error)
|
||||||
ExportSpells(ctx context.Context, exp string, w io.Writer) error
|
ExportSpells(ctx context.Context, exp string, w io.Writer) error
|
||||||
|
ImportSpells(ctx context.Context, exp string, r io.Reader) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type CLI struct {
|
type CLI struct {
|
||||||
|
|||||||
Reference in new issue
Block a user