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 { type Delta struct {
Date string Date string
OtherDates []string
Name string Name string
Value float64 Value float64
Currency Currency Currency Currency
@@ -23,9 +24,10 @@ type Delta struct {
with []Delta 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{ return Delta{
Date: d, Date: d,
OtherDates: otherDates,
Name: name, Name: name,
Value: v, Value: v,
Currency: Currency(c), Currency: Currency(c),

View File

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

View File

@@ -99,8 +99,12 @@ func (files Files) add(payee string, delta Delta) error {
if delta.Currency != USD { if delta.Currency != USD {
currencyValue = fmt.Sprintf("%.2f %s", delta.Value, delta.Currency) 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", 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, delta.Name, filesAppendDelim+filesAppendDelim+filesAppendDelim, currencyValue,
filesAppendDelim, payee, filesAppendDelim, payee,
)) ))

View File

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

View File

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

View File

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