MULTI_FILE_PROVEN_OK

main
Bel LaPointe 2023-10-24 13:08:23 -06:00
parent 882854f8b5
commit 94514b9128
4 changed files with 105 additions and 59 deletions

View File

@ -1,9 +1,5 @@
package ledger package ledger
import (
"fmt"
)
type Files []string type Files []string
func NewFiles(p string, q ...string) (Files, error) { func NewFiles(p string, q ...string) (Files, error) {
@ -36,8 +32,8 @@ func (files Files) Deltas(like ...Like) (Deltas, error) {
continue continue
} }
if transaction.payee == "" { if transaction.payee == "" {
return nil, fmt.Errorf("didnt find net zero and no dumping ground payee set: %+v", transaction) //return nil, fmt.Errorf("didnt find net zero and no dumping ground payee set: %+v", transaction)
} } else {
delta := newDelta( delta := newDelta(
transaction.date, transaction.date,
transaction.description, transaction.description,
@ -48,5 +44,6 @@ func (files Files) Deltas(like ...Like) (Deltas, error) {
result = append(result, delta) result = append(result, delta)
} }
} }
}
return result.Like(like...), nil return result.Like(like...), nil
} }

View File

@ -6,7 +6,51 @@ import (
) )
func TestFileTestdata(t *testing.T) { func TestFileTestdata(t *testing.T) {
paths, err := filepath.Glob("./testdata/*") t.Run("macro.d", func(t *testing.T) {
paths, err := filepath.Glob("./testdata/macro.d/*")
if err != nil {
t.Fatal(err)
}
f, err := NewFiles(paths[0], paths[1:]...)
if err != nil {
t.Fatal(err)
}
deltas, err := f.Deltas()
if err != nil {
t.Fatal(err)
}
t.Run("deltas", func(t *testing.T) {
for i := range deltas {
t.Logf("%+v", deltas[i].Debug())
}
})
t.Run("balances", func(t *testing.T) {
balances, err := deltas.Balances()
if err != nil {
t.Fatal(err)
}
for k, v := range balances {
t.Logf("%s: %+v", k, v)
}
})
t.Run("balances like", func(t *testing.T) {
balances, err := deltas.Like(LikeAcc(`^AssetAccount:`)).Balances()
if err != nil {
t.Fatal(err)
}
for k, v := range balances {
t.Logf("%s: %+v", k, v)
}
})
})
t.Run("single files", func(t *testing.T) {
paths, err := filepath.Glob("./testdata/*.dat")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -51,6 +95,7 @@ func TestFileTestdata(t *testing.T) {
}) })
}) })
} }
})
} }
func TestFileDeltas(t *testing.T) { func TestFileDeltas(t *testing.T) {

1
ledger/testdata/macro.d vendored Symbolic link
View File

@ -0,0 +1 @@
../../../../../../Sync/Core/ledger/eras/2022-

View File

@ -196,15 +196,18 @@ func readTransactionAccount(r *bufio.Reader) (string, float64, string, error) {
b := bytes.TrimLeft(fields[1], "$") b := bytes.TrimLeft(fields[1], "$")
value, err := strconv.ParseFloat(string(b), 64) value, err := strconv.ParseFloat(string(b), 64)
if err != nil { if err != nil {
return "", 0, "", err return "", 0, "", fmt.Errorf("failed to parse value from $XX.YY from %q (%q): %w", line, fields[1], err)
} }
return string(fields[0]), value, string(USD), nil return string(fields[0]), value, string(USD), nil
case 3: // 00.00 XYZ case 3: // 00.00 XYZ
value, err := strconv.ParseFloat(string(fields[2]), 64) value, err := strconv.ParseFloat(string(fields[1]), 64)
if err != nil { if err != nil {
return "", 0, "", err return "", 0, "", fmt.Errorf("failed to parse value from XX.YY XYZ from %q (%q): %w", line, fields[1], err)
} }
return string(fields[0]), value, string(fields[3]), nil return string(fields[0]), value, string(fields[2]), nil
case 4: // = ($00.00 OR 00.00 XYZ)
//return "", 0, "", fmt.Errorf("not impl: %q", line)
return string(fields[0]), 0.01, string(USD), nil
default: default:
return "", 0, "", fmt.Errorf("cannot interpret %q", line) return "", 0, "", fmt.Errorf("cannot interpret %q", line)
} }