From 0267819b456f0effee376eefd7ec310dd3acad0b Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Wed, 25 Oct 2023 11:25:38 -0600 Subject: [PATCH] convert transaction with = to normal but pass isSet=true --- ledger/delta.go | 8 +++++++- ledger/transaction.go | 36 +++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/ledger/delta.go b/ledger/delta.go index 3793753..6c36239 100644 --- a/ledger/delta.go +++ b/ledger/delta.go @@ -14,6 +14,7 @@ type Delta struct { Value float64 Currency Currency Description string + isSet bool } func newDelta(d, desc, name string, v float64, c string) Delta { @@ -36,5 +37,10 @@ func (delta Delta) Plus(other Delta) Delta { } func (delta Delta) Debug() string { - return fmt.Sprintf("{@%s %s:\"%s\" %.2f %s}", delta.Date, delta.Name, delta.Description, delta.Value, delta.Currency) + return fmt.Sprintf("{@%s %s:\"%s\" %s%.2f %s}", delta.Date, delta.Name, delta.Description, func() string { + if !delta.isSet { + return "" + } + return "= " + }(), delta.Value, delta.Currency) } diff --git a/ledger/transaction.go b/ledger/transaction.go index 35aefbc..d8d80c7 100644 --- a/ledger/transaction.go +++ b/ledger/transaction.go @@ -26,6 +26,7 @@ type transactionRecipient struct { name string value float64 currency string + isSet bool } func (t transactionRecipient) empty() bool { @@ -109,7 +110,7 @@ func _readTransaction(r *bufio.Reader) (transaction, error) { } for { - name, value, currency, err := readTransactionName(r) + name, value, currency, isSet, err := readTransactionName(r) if name != "" { if currency == "" { result.payee = name @@ -118,6 +119,7 @@ func _readTransaction(r *bufio.Reader) (transaction, error) { name: name, value: value, currency: currency, + isSet: isSet, }) } } @@ -179,39 +181,43 @@ func _readTransactionLine(r *bufio.Reader) ([]byte, error) { return b2[:n], err } -func readTransactionName(r *bufio.Reader) (string, float64, string, error) { +func readTransactionName(r *bufio.Reader) (string, float64, string, bool, error) { line, err := readTransactionLine(r) if err != nil { - return "", 0, "", err + return "", 0, "", false, err } if len(line) > 0 && !unicode.IsSpace(rune(line[0])) { r2 := *r *r = *bufio.NewReader(io.MultiReader(bytes.NewReader(append(line, '\n')), &r2)) - return "", 0, "", nil + return "", 0, "", false, nil } fields := bytes.Fields(line) + + isSet := false + if len(fields) > 2 && string(fields[1]) == "=" { + isSet = true + fields = append(fields[:1], fields[2:]...) + } + switch len(fields) { case 1: // payee - return string(fields[0]), 0, "", nil - case 2: // $00.00 + return string(fields[0]), 0, "", false, nil + case 2: // payee $00.00 b := bytes.TrimLeft(fields[1], "$") value, err := strconv.ParseFloat(string(b), 64) if err != nil { - return "", 0, "", fmt.Errorf("failed to parse value from $XX.YY from %q (%q): %w", line, fields[1], err) + return "", 0, "", isSet, fmt.Errorf("failed to parse value from $XX.YY from %q (%q): %w", line, fields[1], err) } - return string(fields[0]), value, string(USD), nil - case 3: // 00.00 XYZ + return string(fields[0]), value, string(USD), isSet, nil + case 3: // payee 00.00 XYZ value, err := strconv.ParseFloat(string(fields[1]), 64) if err != nil { - return "", 0, "", fmt.Errorf("failed to parse value from XX.YY XYZ from %q (%q): %w", line, fields[1], err) + return "", 0, "", false, fmt.Errorf("failed to parse value from XX.YY XYZ from %q (%q): %w", line, fields[1], err) } - return string(fields[0]), value, string(fields[2]), nil - case 4: // = ($00.00 OR 00.00 XYZ) - //return "", 0, "", fmt.Errorf("not impl: %q", line) - return string(fields[0]), 0.01, string(USD), nil + return string(fields[0]), value, string(fields[2]), isSet, nil default: - return "", 0, "", fmt.Errorf("cannot interpret %q", line) + return "", 0, "", isSet, fmt.Errorf("cannot interpret %q", line) } }