counterparty was a structured field only nlb could fill honestly. revolut and traderepublic invented one by running an IBAN-shaped regex over the description they had just built, and the two spellings disagreed -- SI56 1234 5678 9012 345 against SI56123456789012345 -- so a literal rule pattern that worked on one account silently matched nothing on another. It is gone from the model, the index, the rule keys, ls --wide and the rules screen. nlb now appends its IBAN column to the end of the description, where the other two already keep theirs, so match = "*SI56*" works everywhere. That changes those descriptions and with them their fingerprints, so a statement overlapping an already-imported period will re-add rather than dedupe those rows until the index is rebuilt. An index built by an older binary drops the column when it is opened. The index itself moves from .money/index.db up to index.db beside rules.toml. Nothing looks in the old location, so an existing one has to be moved by hand -- otherwise the tool quietly starts a fresh index and the manual tags in the old file, the only thing statements cannot reproduce, stay behind in it. The csv and cmd parsers are gone along with the [csv] and [cmd] config they carried. cmd shelled out to the Python extractors, which were ported to Go and deleted, so it bridged to nothing; csv was a generic column-mapped fallback that no account used, and between them they were the largest configuration surface in the tool. A bank is now described in Go, where it can be tested. The importer tests register their own three-column parser rather than borrow a bank's, so they stay about the directory walk, dedupe and per-file error reporting. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
88 lines
2.6 KiB
Go
88 lines
2.6 KiB
Go
// Package parser turns a statement file into raw transactions.
|
|
//
|
|
// Every bank needs its own extraction logic, so parsers are looked up by name
|
|
// from a registry that each one joins from an init. A parser becomes usable by
|
|
// putting its name in an account.toml; there is no generic column-mapped
|
|
// parser, because a statement layout is better described in Go, where it can
|
|
// be tested, than in a table of column indexes.
|
|
package parser
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"sync"
|
|
|
|
"git.petrovv.com/nikola/money/internal/config"
|
|
)
|
|
|
|
// RawTxn is one line as it came out of a statement, before fingerprinting,
|
|
// deduplication or tagging.
|
|
type RawTxn struct {
|
|
Date string // YYYY-MM-DD
|
|
Description string
|
|
AmountMinor int64 // signed; negative is an outflow
|
|
|
|
// Type is the bank's own classification of the transaction. Optional.
|
|
Type string
|
|
// BalanceMinor is the running balance after this transaction, when the
|
|
// statement reports one. Optional; enables the balance-chain check.
|
|
BalanceMinor *int64
|
|
}
|
|
|
|
// Parser extracts transactions from a single statement file.
|
|
type Parser interface {
|
|
// Parse reads the statement at path. digits is the account's minor-unit
|
|
// scale, so parsers can convert decimal strings without guessing.
|
|
Parse(path string, acc *config.Account) ([]RawTxn, error)
|
|
}
|
|
|
|
// Warner is an optional interface for parsers that legitimately drop rows --
|
|
// pending transactions, other currencies -- and want to say so. The importer
|
|
// collects the warnings from the most recent Parse call and reports them.
|
|
type Warner interface {
|
|
Warnings() []string
|
|
}
|
|
|
|
// Factory builds a parser from an account's config, validating it up front so
|
|
// a bad account.toml fails before any file is read.
|
|
type Factory func(acc *config.Account) (Parser, error)
|
|
|
|
var (
|
|
mu sync.RWMutex
|
|
registry = map[string]Factory{}
|
|
)
|
|
|
|
// Register adds a named parser. It panics on a duplicate name, since that can
|
|
// only be a programming error at init time.
|
|
func Register(name string, f Factory) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if _, dup := registry[name]; dup {
|
|
panic("parser: duplicate registration of " + name)
|
|
}
|
|
registry[name] = f
|
|
}
|
|
|
|
// For builds the parser named by acc.Parser.
|
|
func For(acc *config.Account) (Parser, error) {
|
|
mu.RLock()
|
|
f, ok := registry[acc.Parser]
|
|
mu.RUnlock()
|
|
if !ok {
|
|
return nil, fmt.Errorf("account %s: unknown parser %q (available: %v)", acc.Slug, acc.Parser, Names())
|
|
}
|
|
return f(acc)
|
|
}
|
|
|
|
// Names lists the registered parsers, for error messages and `money parsers`.
|
|
func Names() []string {
|
|
mu.RLock()
|
|
defer mu.RUnlock()
|
|
out := make([]string, 0, len(registry))
|
|
for name := range registry {
|
|
out = append(out, name)
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|