This commit is contained in:
2026-06-05 17:22:38 +02:00
parent cf9dbf26f3
commit 88623f55df
21 changed files with 2123 additions and 50 deletions
+32 -9
View File
@@ -36,29 +36,52 @@ type Exporter struct {
IncludeAppFields bool
}
// iterator streams QSOs through fn. The three concrete sources (all, filtered,
// by-ids) all match this shape so the document writer stays source-agnostic.
type iterator func(ctx context.Context, fn func(qso.QSO) error) error
// ExportFile creates path (overwriting if it exists) and writes every QSO.
func (e *Exporter) ExportFile(ctx context.Context, path string) (ExportResult, error) {
return e.exportFileWith(ctx, path, e.Repo.IterateAll)
}
// ExportFileFiltered writes only the QSOs matching f (no row limit).
func (e *Exporter) ExportFileFiltered(ctx context.Context, path string, f qso.QueryFilter) (ExportResult, error) {
return e.exportFileWith(ctx, path, func(ctx context.Context, fn func(qso.QSO) error) error {
return e.Repo.IterateFiltered(ctx, f, fn)
})
}
// ExportFileByIDs writes only the QSOs with the given ids.
func (e *Exporter) ExportFileByIDs(ctx context.Context, path string, ids []int64) (ExportResult, error) {
return e.exportFileWith(ctx, path, func(ctx context.Context, fn func(qso.QSO) error) error {
return e.Repo.IterateByIDs(ctx, ids, fn)
})
}
func (e *Exporter) exportFileWith(ctx context.Context, path string, iter iterator) (ExportResult, error) {
f, err := os.Create(path)
if err != nil {
return ExportResult{}, fmt.Errorf("create %s: %w", path, err)
}
defer f.Close()
count, err := e.Export(ctx, f)
count, err := e.writeDoc(ctx, f, iter)
if err != nil {
return ExportResult{Path: path, Count: count}, err
}
info, _ := f.Stat()
return ExportResult{
Path: path,
Count: count,
SizeKB: info.Size() / 1024,
}, nil
return ExportResult{Path: path, Count: count, SizeKB: info.Size() / 1024}, nil
}
// Export writes a complete ADIF document (header + records + EOF) to w.
// Returns the number of QSOs successfully written.
// Export writes a complete ADIF document (header + records + EOF) to w for
// every QSO. Returns the number of QSOs written.
func (e *Exporter) Export(ctx context.Context, w io.Writer) (int, error) {
return e.writeDoc(ctx, w, e.Repo.IterateAll)
}
// writeDoc writes the ADIF header then streams every QSO from iter.
func (e *Exporter) writeDoc(ctx context.Context, w io.Writer, iter iterator) (int, error) {
bw := bufio.NewWriterSize(w, 64*1024)
defer bw.Flush()
@@ -76,7 +99,7 @@ func (e *Exporter) Export(ctx context.Context, w io.Writer) (int, error) {
fmt.Fprintf(bw, " <CREATED_TIMESTAMP:15>%s <EOH>\n\n", now)
count := 0
err := e.Repo.IterateAll(ctx, func(q qso.QSO) error {
err := iter(ctx, func(q qso.QSO) error {
writeRecord(bw, q, e.IncludeAppFields)
count++
return nil