ledger.Delta.OtherDates

This commit is contained in:
Bel LaPointe
2026-01-31 11:03:56 -07:00
parent 0eb0abf4a8
commit 502e47d0bc
6 changed files with 28 additions and 12 deletions

View File

@@ -10,6 +10,7 @@ const (
type Delta struct {
Date string
OtherDates []string
Name string
Value float64
Currency Currency
@@ -23,9 +24,10 @@ type Delta struct {
with []Delta
}
func newDelta(transaction string, payee bool, d, desc, name string, v float64, c string, isSet bool, fileName string, lineNo int) Delta {
func newDelta(transaction string, payee bool, d, desc, name string, v float64, c string, isSet bool, fileName string, lineNo int, otherDates []string) Delta {
return Delta{
Date: d,
OtherDates: otherDates,
Name: name,
Value: v,
Currency: Currency(c),

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, "", 0)
delta := newDelta("x", true, d, "", "name", 34.56, "$", false, "", 0, []string{"d2"})
if delta.Transaction != "x" {
t.Error(delta.Transaction)
@@ -17,6 +17,9 @@ func TestDelta(t *testing.T) {
if delta.Date != d {
t.Error(delta.Date)
}
if delta.OtherDates[0] != "d2" {
t.Error(delta.OtherDates)
}
if delta.Name != "name" {
t.Error(delta.Name)
}

View File

@@ -99,8 +99,12 @@ func (files Files) add(payee string, delta Delta) error {
if delta.Currency != USD {
currencyValue = fmt.Sprintf("%.2f %s", delta.Value, delta.Currency)
}
date := delta.Date
for _, otherDate := range delta.OtherDates {
date += "=" + otherDate
}
return files.append(fmt.Sprintf("%s %s\n%s%s%s%s\n%s%s",
delta.Date, delta.Description,
date, delta.Description,
filesAppendDelim, delta.Name, filesAppendDelim+filesAppendDelim+filesAppendDelim, currencyValue,
filesAppendDelim, payee,
))

View File

@@ -73,12 +73,13 @@ func TestFileAmend(t *testing.T) {
},
"payee": {
from: `
2006-01-02 description
2006-01-02=2006-01-03 description
recipient $3.45
payee
`,
old: Delta{
Date: "2006-01-02",
OtherDates: []string{"2006-01-03"},
Name: "payee",
Value: -3.45,
Currency: "$",
@@ -86,16 +87,17 @@ func TestFileAmend(t *testing.T) {
},
now: Delta{
Date: "2106-11-12",
OtherDates: []string{"2006-01-03"},
Name: "1payee",
Value: -13.45,
Currency: "T",
Description: "1description",
},
want: `
2006-01-02 description
2006-01-02=2006-01-03 description
payee $3.45
recipient
2106-11-12 1description
2106-11-12=2006-01-03 1description
1payee -13.45 T
recipient`,
},

View File

@@ -106,6 +106,7 @@ func (transaction Transaction) Payee() string {
type transaction struct {
date string
otherDates []string
description string
payee string
recipients []transactionRecipient
@@ -142,6 +143,7 @@ func (t transaction) deltas() Deltas {
recipient.isSet,
t.fileName,
t.lineNo+i,
t.otherDates,
))
}
for currency, value := range sums {
@@ -162,6 +164,7 @@ func (t transaction) deltas() Deltas {
false,
t.fileName,
t.lineNo,
t.otherDates,
))
}
}
@@ -287,16 +290,17 @@ func _readTransaction(name string, r *bufio.Reader) (transaction, error) {
return transaction{}, err
}
dateDescriptionPattern := regexp.MustCompile(`^([0-9]+-[0-9]+-[0-9]+)\s+(.*)$`)
dateDescriptionMatches := dateDescriptionPattern.FindAllSubmatch(firstLine, 4)
dateDescriptionPattern := regexp.MustCompile(`^([0-9]+-[0-9]+-[0-9]+)((=[0-9]+-[0-9]+-[0-9]+)*)\s+(.*)$`)
dateDescriptionMatches := dateDescriptionPattern.FindAllStringSubmatch(string(firstLine), 4)
if len(dateDescriptionMatches) != 1 {
return transaction{}, fmt.Errorf("bad first line: %v matches: %q", len(dateDescriptionMatches), firstLine)
} else if len(dateDescriptionMatches[0]) != 3 {
} else if len(dateDescriptionMatches[0]) != 5 {
return transaction{}, fmt.Errorf("bad first line: %v submatches: %q", len(dateDescriptionMatches[0]), firstLine)
}
result := transaction{
date: string(dateDescriptionMatches[0][1]),
description: string(dateDescriptionMatches[0][2]),
date: dateDescriptionMatches[0][1],
otherDates: strings.Split(strings.Trim(dateDescriptionMatches[0][2], "="), "="),
description: dateDescriptionMatches[0][4],
name: name,
}

View File

@@ -58,12 +58,13 @@ func TestReadTransaction(t *testing.T) {
},
"verbose": {
input: `
2003-04-05 Reasoning here
2003-04-05=2003-04-06=2003-04-07 Reasoning here
A:B $1.00
C:D $-1.00
`,
want: transaction{
date: "2003-04-05",
otherDates: []string{"2003-04-06", "2003-04-07"},
description: "Reasoning here",
payee: "A:B",
recipients: []transactionRecipient{