deltas try to remember filename, lineno
parent
fb1ddc72c3
commit
1f9919a172
|
|
@ -14,29 +14,43 @@ type Delta struct {
|
||||||
Value float64
|
Value float64
|
||||||
Currency Currency
|
Currency Currency
|
||||||
Description string
|
Description string
|
||||||
isSet bool
|
|
||||||
Transaction string
|
Transaction string
|
||||||
Payee bool
|
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{
|
return Delta{
|
||||||
Date: d,
|
Date: d,
|
||||||
Name: name,
|
Name: name,
|
||||||
Value: v,
|
Value: v,
|
||||||
Currency: Currency(c),
|
Currency: Currency(c),
|
||||||
Description: desc,
|
Description: desc,
|
||||||
isSet: isSet,
|
|
||||||
Transaction: transaction,
|
Transaction: transaction,
|
||||||
Payee: payee,
|
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 {
|
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 {
|
if !delta.isSet {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return "= "
|
return "= "
|
||||||
}(), delta.Value, delta.Currency)
|
}(), delta.Value, delta.Currency, delta.fileName, delta.lineNo)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
func TestDelta(t *testing.T) {
|
func TestDelta(t *testing.T) {
|
||||||
d := "2099-08-07"
|
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" {
|
if delta.Transaction != "x" {
|
||||||
t.Error(delta.Transaction)
|
t.Error(delta.Transaction)
|
||||||
|
|
|
||||||
|
|
@ -145,7 +145,7 @@ func TestFileAmend(t *testing.T) {
|
||||||
} else if filtered := deltas.Like(func(d Delta) bool {
|
} else if filtered := deltas.Like(func(d Delta) bool {
|
||||||
c.old.Transaction = d.Transaction
|
c.old.Transaction = d.Transaction
|
||||||
c.old.Payee = d.Payee
|
c.old.Payee = d.Payee
|
||||||
return d == c.old
|
return d.equivalent(c.old)
|
||||||
}); len(filtered) != 1 {
|
}); 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)
|
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,
|
Currency: USD,
|
||||||
Description: "Electricity / Power Bill TG2PJ-2PLP5",
|
Description: "Electricity / Power Bill TG2PJ-2PLP5",
|
||||||
Payee: true,
|
Payee: true,
|
||||||
|
|
||||||
|
fileName: "",
|
||||||
|
lineNo: 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Date: "2022-12-12",
|
Date: "2022-12-12",
|
||||||
|
|
@ -380,6 +383,9 @@ func TestFileDeltas(t *testing.T) {
|
||||||
Value: 97.92,
|
Value: 97.92,
|
||||||
Currency: USD,
|
Currency: USD,
|
||||||
Description: "Electricity / Power Bill TG2PJ-2PLP5",
|
Description: "Electricity / Power Bill TG2PJ-2PLP5",
|
||||||
|
|
||||||
|
fileName: "",
|
||||||
|
lineNo: 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Date: "2022-12-12",
|
Date: "2022-12-12",
|
||||||
|
|
@ -388,6 +394,9 @@ func TestFileDeltas(t *testing.T) {
|
||||||
Currency: USD,
|
Currency: USD,
|
||||||
Description: "Test pay chase TG32S-BT2FF",
|
Description: "Test pay chase TG32S-BT2FF",
|
||||||
Payee: true,
|
Payee: true,
|
||||||
|
|
||||||
|
fileName: "",
|
||||||
|
lineNo: 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Date: "2022-12-12",
|
Date: "2022-12-12",
|
||||||
|
|
@ -395,6 +404,9 @@ func TestFileDeltas(t *testing.T) {
|
||||||
Value: 1.00,
|
Value: 1.00,
|
||||||
Currency: USD,
|
Currency: USD,
|
||||||
Description: "Test pay chase TG32S-BT2FF",
|
Description: "Test pay chase TG32S-BT2FF",
|
||||||
|
|
||||||
|
fileName: "",
|
||||||
|
lineNo: 0,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -407,7 +419,8 @@ 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 := NewFiles("./testdata/" + name + ".dat")
|
fileName := "./testdata/" + name + ".dat"
|
||||||
|
f, err := NewFiles(fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
@ -421,6 +434,8 @@ func TestFileDeltas(t *testing.T) {
|
||||||
t.Error(len(deltas))
|
t.Error(len(deltas))
|
||||||
}
|
}
|
||||||
for i := range want {
|
for i := range want {
|
||||||
|
want[i].fileName = fileName
|
||||||
|
deltas[i].lineNo = 0
|
||||||
if i >= len(deltas) {
|
if i >= len(deltas) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -110,6 +110,9 @@ type transaction struct {
|
||||||
payee string
|
payee string
|
||||||
recipients []transactionRecipient
|
recipients []transactionRecipient
|
||||||
name string
|
name string
|
||||||
|
|
||||||
|
fileName string
|
||||||
|
lineNo int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t transaction) empty() bool {
|
func (t transaction) empty() bool {
|
||||||
|
|
@ -126,7 +129,7 @@ type transactionRecipient struct {
|
||||||
func (t transaction) deltas() Deltas {
|
func (t transaction) deltas() Deltas {
|
||||||
result := []Delta{}
|
result := []Delta{}
|
||||||
sums := map[string]float64{}
|
sums := map[string]float64{}
|
||||||
for _, recipient := range t.recipients {
|
for i, recipient := range t.recipients {
|
||||||
sums[recipient.currency] += recipient.value
|
sums[recipient.currency] += recipient.value
|
||||||
result = append(result, newDelta(
|
result = append(result, newDelta(
|
||||||
t.name,
|
t.name,
|
||||||
|
|
@ -137,6 +140,8 @@ func (t transaction) deltas() Deltas {
|
||||||
recipient.value,
|
recipient.value,
|
||||||
recipient.currency,
|
recipient.currency,
|
||||||
recipient.isSet,
|
recipient.isSet,
|
||||||
|
t.fileName,
|
||||||
|
t.lineNo+i,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
for currency, value := range sums {
|
for currency, value := range sums {
|
||||||
|
|
@ -155,6 +160,8 @@ func (t transaction) deltas() Deltas {
|
||||||
-1.0*value,
|
-1.0*value,
|
||||||
currency,
|
currency,
|
||||||
false,
|
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))
|
name := fmt.Sprintf("%s/%d", file, len(result))
|
||||||
one, err := readTransaction(name, r)
|
one, err := readTransaction(name, r)
|
||||||
if !one.empty() {
|
if !one.empty() {
|
||||||
|
one.fileName = file
|
||||||
|
one.lineNo = len(result)
|
||||||
result = append(result, one)
|
result = append(result, one)
|
||||||
}
|
}
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue