to multifile always

main
Bel LaPointe 2023-10-24 12:54:11 -06:00
parent 24d0e829b3
commit 480d358f5a
3 changed files with 34 additions and 16 deletions

View File

@ -4,20 +4,15 @@ import (
"fmt" "fmt"
) )
type File string type Files []string
func NewFile(p string) (File, error) { func NewFiles(p string, q ...string) (Files, error) {
f := File(p) f := Files(append([]string{p}, q...))
_, err := f.Deltas() _, err := f.Deltas()
return f, err return f, err
} }
func (file File) Balances(like ...Like) (map[string]map[Currency]float64, error) { func Balances(deltas []Delta) (map[string]map[Currency]float64, error) {
deltas, err := file.Deltas(like...)
if err != nil {
return nil, err
}
result := make(map[string]map[Currency]float64) result := make(map[string]map[Currency]float64)
for _, delta := range deltas { for _, delta := range deltas {
if _, ok := result[delta.Account]; !ok { if _, ok := result[delta.Account]; !ok {
@ -32,8 +27,8 @@ func (file File) Balances(like ...Like) (map[string]map[Currency]float64, error)
return result, nil return result, nil
} }
func (file File) Deltas(like ...Like) ([]Delta, error) { func (files Files) Deltas(like ...Like) ([]Delta, error) {
transactions, err := file.transactions() transactions, err := files.transactions()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -14,7 +14,7 @@ func TestFileTestdata(t *testing.T) {
for _, pathd := range paths { for _, pathd := range paths {
path := pathd path := pathd
t.Run(path, func(t *testing.T) { t.Run(path, func(t *testing.T) {
f, err := NewFile(path) f, err := NewFiles(path)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -24,13 +24,19 @@ func TestFileTestdata(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
for i := range deltas { for i := range deltas {
t.Logf("%+v", deltas[i].Debug()) t.Logf("%+v", deltas[i].Debug())
} }
}) })
t.Run("balances", func(t *testing.T) { t.Run("balances", func(t *testing.T) {
balances, err := f.Balances() deltas, err := f.Deltas()
if err != nil {
t.Fatal(err)
}
balances, err := Balances(deltas)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -40,7 +46,12 @@ func TestFileTestdata(t *testing.T) {
}) })
t.Run("balances like", func(t *testing.T) { t.Run("balances like", func(t *testing.T) {
balances, err := f.Balances(LikeAcc(`AssetAccount:Cash:Fidelity76`)) deltas, err := f.Deltas(LikeAcc(`AssetAccount:Cash:Fidelity76`))
if err != nil {
t.Fatal(err)
}
balances, err := Balances(deltas)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -93,7 +104,7 @@ func TestFileDeltas(t *testing.T) {
for name, d := range cases { for name, d := range cases {
want := d want := d
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
f, err := NewFile("./testdata/" + name + ".dat") f, err := NewFiles("./testdata/" + name + ".dat")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -32,7 +32,19 @@ func (t transactionRecipient) empty() bool {
return t == (transactionRecipient{}) return t == (transactionRecipient{})
} }
func (file File) transactions() ([]transaction, error) { func (files Files) transactions() ([]transaction, error) {
result := make([]transaction, 0)
for i := range files {
some, err := files._transactions(files[i])
if err != nil {
return nil, err
}
result = append(result, some...)
}
return result, nil
}
func (files Files) _transactions(file string) ([]transaction, error) {
f, err := os.Open(string(file)) f, err := os.Open(string(file))
if err != nil { if err != nil {
return nil, err return nil, err