messing about with decoding/encoding

This commit is contained in:
tsmethurst 2021-09-07 11:43:47 +02:00
commit 3641d47e7e
22 changed files with 2276 additions and 32 deletions

View file

@ -19,7 +19,6 @@
package trans
import (
"bufio"
"context"
"encoding/json"
"fmt"
@ -35,26 +34,25 @@ func (e *exporter) ExportMinimal(ctx context.Context, path string) error {
return err
}
w := bufio.NewWriter(f)
encoder := json.NewEncoder(w)
encoder := json.NewEncoder(f)
accounts := []*transmodel.Account{}
if err := e.db.GetWhere(ctx, []db.Where{{Key: "domain", Value: nil}}, &accounts); err != nil {
return fmt.Errorf("error selecting accounts: %s", err)
return fmt.Errorf("ExportMinimal: error selecting accounts: %s", err)
}
for _, a := range accounts {
encoder.Encode(a)
a.Type = transmodel.TransAccount
if err := encoder.Encode(a); err != nil {
return fmt.Errorf("ExportMinimal: error encoding account: %s", err)
}
e.log.Infof("ExportMinimal: exported account %s to %s", a.ID, path)
}
return neatClose(w, f)
return neatClose(f)
}
func neatClose(w *bufio.Writer, f *os.File) error {
if err := w.Flush(); err != nil {
return fmt.Errorf("error flushing writer: %s", err)
}
func neatClose(f *os.File) error {
if err := f.Close(); err != nil {
return fmt.Errorf("error closing file: %s", err)
}