Remove the transfer flag
A transfer was a second verdict carried alongside the tag: a boolean set by transfer = true in a rule or by x in the TUI, kept in its own pair of rule_ and manual_ columns, whose one real effect was to hold the row out of the report. The rest of it was display -- a T column in the transaction list, in the rules screen and in money ls. Money moved between your own accounts is now tagged like anything else and counts like anything else. The leg leaving checking is an outflow and the leg arriving in savings is an inflow, so a report over the whole data root roughly nets out while one scoped to a single account or month does not. That is the price of one verdict per transaction instead of two. A rule now needs a tag, and one that set only transfer = true is refused by number on load. A leftover transfer key beside a tag is ignored, as unknown TOML keys always were, and an index built by an older binary drops both columns when it is opened. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+11
-13
@@ -1,9 +1,8 @@
|
||||
// Package rules applies the ordered glob rules from rules.toml to
|
||||
// transactions, deciding their automatic tag and whether they are a transfer
|
||||
// between the user's own accounts.
|
||||
// transactions, deciding their automatic tag.
|
||||
//
|
||||
// Only the rule_* columns are ever written. Manual edits made in the TUI live
|
||||
// in separate columns and survive any number of re-runs.
|
||||
// Only rule_tag is ever written. A tag set by hand in the TUI lives in its own
|
||||
// column and survives any number of re-runs.
|
||||
package rules
|
||||
|
||||
import (
|
||||
@@ -76,17 +75,16 @@ func (e *Engine) Usage(txns []model.Transaction) []int {
|
||||
return counts
|
||||
}
|
||||
|
||||
// ApplyTxn returns the tag and transfer flag for a transaction. An unmatched
|
||||
// transaction gets an empty tag and is not a transfer.
|
||||
func (e *Engine) ApplyTxn(accountSlug string, t model.Transaction) (tag string, transfer bool) {
|
||||
// ApplyTxn returns the tag for a transaction, empty if no rule matches.
|
||||
func (e *Engine) ApplyTxn(accountSlug string, t model.Transaction) string {
|
||||
if r := e.Match(accountSlug, t); r != nil {
|
||||
return r.Tag, r.Transfer
|
||||
return r.Tag
|
||||
}
|
||||
return "", false
|
||||
return ""
|
||||
}
|
||||
|
||||
// Apply is the description-only shorthand, for callers that have nothing else.
|
||||
func (e *Engine) Apply(accountSlug, description string) (tag string, transfer bool) {
|
||||
func (e *Engine) Apply(accountSlug, description string) string {
|
||||
return e.ApplyTxn(accountSlug, model.Transaction{Description: description})
|
||||
}
|
||||
|
||||
@@ -99,11 +97,11 @@ func (e *Engine) Retag(db *store.DB) (int, error) {
|
||||
}
|
||||
var changed []store.RuleAssignment
|
||||
for _, t := range txns {
|
||||
tag, transfer := e.ApplyTxn(t.AccountSlug, t)
|
||||
if tag == t.RuleTag && transfer == t.RuleTransfer {
|
||||
tag := e.ApplyTxn(t.AccountSlug, t)
|
||||
if tag == t.RuleTag {
|
||||
continue
|
||||
}
|
||||
changed = append(changed, store.RuleAssignment{ID: t.ID, Tag: tag, Transfer: transfer})
|
||||
changed = append(changed, store.RuleAssignment{ID: t.ID, Tag: tag})
|
||||
}
|
||||
if len(changed) == 0 {
|
||||
return 0, nil
|
||||
|
||||
@@ -128,13 +128,13 @@ func TestFirstMatchWins(t *testing.T) {
|
||||
{Match: "*LIDL EXPRESS*", Tag: "snacks"},
|
||||
{Match: "*LIDL*", Tag: "groceries"},
|
||||
}})
|
||||
if tag, _ := engine.Apply("checking", "CARD LIDL EXPRESS 12"); tag != "snacks" {
|
||||
if tag := engine.Apply("checking", "CARD LIDL EXPRESS 12"); tag != "snacks" {
|
||||
t.Errorf("tag = %q, want snacks (earlier rule must win)", tag)
|
||||
}
|
||||
if tag, _ := engine.Apply("checking", "CARD LIDL 12"); tag != "groceries" {
|
||||
if tag := engine.Apply("checking", "CARD LIDL 12"); tag != "groceries" {
|
||||
t.Errorf("tag = %q, want groceries", tag)
|
||||
}
|
||||
if tag, _ := engine.Apply("checking", "SOMETHING ELSE"); tag != "" {
|
||||
if tag := engine.Apply("checking", "SOMETHING ELSE"); tag != "" {
|
||||
t.Errorf("tag = %q, want empty for an unmatched description", tag)
|
||||
}
|
||||
}
|
||||
@@ -187,17 +187,15 @@ func TestMatchIndex(t *testing.T) {
|
||||
|
||||
func TestAccountScopedRule(t *testing.T) {
|
||||
engine := New(&config.Rules{Rule: []config.Rule{
|
||||
{Match: "*TRANSFER*", Tag: "transfer", Transfer: true, Account: "savings"},
|
||||
{Match: "*TRANSFER*", Tag: "transfer", Account: "savings"},
|
||||
{Match: "*TRANSFER*", Tag: "misc"},
|
||||
}})
|
||||
|
||||
tag, transfer := engine.Apply("savings", "TRANSFER FROM CHECKING")
|
||||
if tag != "transfer" || !transfer {
|
||||
t.Errorf("savings: tag=%q transfer=%v, want transfer/true", tag, transfer)
|
||||
if tag := engine.Apply("savings", "TRANSFER FROM CHECKING"); tag != "transfer" {
|
||||
t.Errorf("savings: tag = %q, want transfer", tag)
|
||||
}
|
||||
tag, transfer = engine.Apply("checking", "TRANSFER FROM CHECKING")
|
||||
if tag != "misc" || transfer {
|
||||
t.Errorf("checking: tag=%q transfer=%v, want misc/false", tag, transfer)
|
||||
if tag := engine.Apply("checking", "TRANSFER FROM CHECKING"); tag != "misc" {
|
||||
t.Errorf("checking: tag = %q, want misc", tag)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,13 +208,13 @@ func TestTypeRules(t *testing.T) {
|
||||
}})
|
||||
|
||||
// A rule setting several patterns requires all of them to match.
|
||||
tag, _ := engine.ApplyTxn("checking", model.Transaction{
|
||||
tag := engine.ApplyTxn("checking", model.Transaction{
|
||||
Description: "LIDL SOFIA", Type: "CARD_PAYMENT",
|
||||
})
|
||||
if tag != "groceries" {
|
||||
t.Errorf("tag = %q, want groceries", tag)
|
||||
}
|
||||
tag, _ = engine.ApplyTxn("checking", model.Transaction{
|
||||
tag = engine.ApplyTxn("checking", model.Transaction{
|
||||
Description: "LIDL SOFIA", Type: "TRANSFER",
|
||||
})
|
||||
if tag != "" {
|
||||
@@ -224,68 +222,9 @@ func TestTypeRules(t *testing.T) {
|
||||
}
|
||||
|
||||
// Type alone works too.
|
||||
if tag, _ := engine.ApplyTxn("checking", model.Transaction{
|
||||
if tag := engine.ApplyTxn("checking", model.Transaction{
|
||||
Description: "WITHDRAWAL", Type: "ATM",
|
||||
}); tag != "cash" {
|
||||
t.Errorf("tag = %q, want cash", tag)
|
||||
}
|
||||
}
|
||||
|
||||
// Transfers can also be overridden by hand, independently of the tag.
|
||||
func TestManualTransferOverride(t *testing.T) {
|
||||
db := testDB(t)
|
||||
seed(t, db, "TRANSFER TO SAVINGS")
|
||||
|
||||
engine := New(&config.Rules{Rule: []config.Rule{
|
||||
{Match: "*TO SAVINGS*", Tag: "transfer", Transfer: true},
|
||||
}})
|
||||
if _, err := engine.Retag(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
txn := byDescription(t, db, "TRANSFER TO SAVINGS")
|
||||
if !txn.IsTransfer() {
|
||||
t.Fatal("expected the rule to mark this as a transfer")
|
||||
}
|
||||
|
||||
no := false
|
||||
if err := db.SetManualTransfer(txn.ID, &no); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := engine.Retag(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if byDescription(t, db, "TRANSFER TO SAVINGS").IsTransfer() {
|
||||
t.Error("manual transfer override was lost on retag")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUntaggedFilter(t *testing.T) {
|
||||
db := testDB(t)
|
||||
seed(t, db, "LIDL SOFIA", "SOME RANDOM SHOP")
|
||||
|
||||
engine := New(&config.Rules{Rule: []config.Rule{{Match: "*LIDL*", Tag: "groceries"}}})
|
||||
if _, err := engine.Retag(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
untagged, err := db.Transactions(store.Filter{Untagged: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(untagged) != 1 || untagged[0].Description != "SOME RANDOM SHOP" {
|
||||
t.Fatalf("untagged = %+v, want only SOME RANDOM SHOP", untagged)
|
||||
}
|
||||
|
||||
// A manual tag must remove the row from the untagged view.
|
||||
if err := db.SetManualTag(untagged[0].ID, "hobby"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
untagged, err = db.Transactions(store.Filter{Untagged: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(untagged) != 0 {
|
||||
t.Errorf("untagged = %+v, want none", untagged)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user