deltas try to remember filename, lineno

main
Bel LaPointe 2025-04-03 11:54:29 -06:00
parent fb1ddc72c3
commit 1f9919a172
4 changed files with 47 additions and 9 deletions

View File

@ -14,29 +14,43 @@ type Delta struct {
Value float64
Currency Currency
Description string
isSet bool
Transaction string
Payee bool
isSet bool
fileName string
lineNo int
}
func newDelta(transaction string, payee bool, d, desc, name string, v float64, c string, isSet bool) Delta {
func newDelta(transaction string, payee bool, d, desc, name string, v float64, c string, isSet bool, fileName string, lineNo int) Delta {
return Delta{
Date: d,
Name: name,
Value: v,
Currency: Currency(c),
Description: desc,
isSet: isSet,
Transaction: transaction,
Payee: payee,
isSet: isSet,
fileName: fileName,
lineNo: lineNo,
}
}
func (delta Delta) equivalent(other Delta) bool {
delta.fileName = ""
delta.lineNo = 0
other.fileName = ""
other.lineNo = 0
return delta == other
}
func (delta Delta) Debug() string {
return fmt.Sprintf("{@%s %s(payee=%v):\"%s\" %s%.2f %s}", delta.Date, delta.Name, delta.Payee, delta.Description, func() string {
return fmt.Sprintf("{@%s %s(payee=%v):\"%s\" %s%.2f %s @%s#%d}", delta.Date, delta.Name, delta.Payee, delta.Description, func() string {
if !delta.isSet {
return ""
}
return "= "
}(), delta.Value, delta.Currency)
}(), delta.Value, delta.Currency, delta.fileName, delta.lineNo)
}

View File

@ -6,7 +6,7 @@ import (
func TestDelta(t *testing.T) {
d := "2099-08-07"
delta := newDelta("x", true, d, "", "name", 34.56, "$", false)
delta := newDelta("x", true, d, "", "name", 34.56, "$", false, "", 0)
if delta.Transaction != "x" {
t.Error(delta.Transaction)

View File

@ -145,7 +145,7 @@ func TestFileAmend(t *testing.T) {
} else if filtered := deltas.Like(func(d Delta) bool {
c.old.Transaction = d.Transaction
c.old.Payee = d.Payee
return d == c.old
return d.equivalent(c.old)
}); len(filtered) != 1 {
t.Fatalf("input \n\t%s \ndidnt include old \n\t%+v \nin \n\t%+v: \n\t%+v", c.from, c.old, deltas, filtered)
}
@ -373,6 +373,9 @@ func TestFileDeltas(t *testing.T) {
Currency: USD,
Description: "Electricity / Power Bill TG2PJ-2PLP5",
Payee: true,
fileName: "",
lineNo: 0,
},
{
Date: "2022-12-12",
@ -380,6 +383,9 @@ func TestFileDeltas(t *testing.T) {
Value: 97.92,
Currency: USD,
Description: "Electricity / Power Bill TG2PJ-2PLP5",
fileName: "",
lineNo: 0,
},
{
Date: "2022-12-12",
@ -388,6 +394,9 @@ func TestFileDeltas(t *testing.T) {
Currency: USD,
Description: "Test pay chase TG32S-BT2FF",
Payee: true,
fileName: "",
lineNo: 0,
},
{
Date: "2022-12-12",
@ -395,6 +404,9 @@ func TestFileDeltas(t *testing.T) {
Value: 1.00,
Currency: USD,
Description: "Test pay chase TG32S-BT2FF",
fileName: "",
lineNo: 0,
},
}
@ -407,7 +419,8 @@ func TestFileDeltas(t *testing.T) {
for name, d := range cases {
want := d
t.Run(name, func(t *testing.T) {
f, err := NewFiles("./testdata/" + name + ".dat")
fileName := "./testdata/" + name + ".dat"
f, err := NewFiles(fileName)
if err != nil {
t.Fatal(err)
}
@ -421,6 +434,8 @@ func TestFileDeltas(t *testing.T) {
t.Error(len(deltas))
}
for i := range want {
want[i].fileName = fileName
deltas[i].lineNo = 0
if i >= len(deltas) {
break
}

View File

@ -110,6 +110,9 @@ type transaction struct {
payee string
recipients []transactionRecipient
name string
fileName string
lineNo int
}
func (t transaction) empty() bool {
@ -126,7 +129,7 @@ type transactionRecipient struct {
func (t transaction) deltas() Deltas {
result := []Delta{}
sums := map[string]float64{}
for _, recipient := range t.recipients {
for i, recipient := range t.recipients {
sums[recipient.currency] += recipient.value
result = append(result, newDelta(
t.name,
@ -137,6 +140,8 @@ func (t transaction) deltas() Deltas {
recipient.value,
recipient.currency,
recipient.isSet,
t.fileName,
t.lineNo+i,
))
}
for currency, value := range sums {
@ -155,6 +160,8 @@ func (t transaction) deltas() Deltas {
-1.0*value,
currency,
false,
t.fileName,
t.lineNo,
))
}
}
@ -194,6 +201,8 @@ func (files Files) _transactions(file string) ([]transaction, error) {
name := fmt.Sprintf("%s/%d", file, len(result))
one, err := readTransaction(name, r)
if !one.empty() {
one.fileName = file
one.lineNo = len(result)
result = append(result, one)
}
if err == io.EOF {