convert transaction with = to normal but pass isSet=true

main
Bel LaPointe 2023-10-25 11:25:38 -06:00
parent a4bf52d0e9
commit 0267819b45
2 changed files with 28 additions and 16 deletions

View File

@ -14,6 +14,7 @@ type Delta struct {
Value float64 Value float64
Currency Currency Currency Currency
Description string Description string
isSet bool
} }
func newDelta(d, desc, name string, v float64, c string) Delta { 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 { 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)
} }

View File

@ -26,6 +26,7 @@ type transactionRecipient struct {
name string name string
value float64 value float64
currency string currency string
isSet bool
} }
func (t transactionRecipient) empty() bool { func (t transactionRecipient) empty() bool {
@ -109,7 +110,7 @@ func _readTransaction(r *bufio.Reader) (transaction, error) {
} }
for { for {
name, value, currency, err := readTransactionName(r) name, value, currency, isSet, err := readTransactionName(r)
if name != "" { if name != "" {
if currency == "" { if currency == "" {
result.payee = name result.payee = name
@ -118,6 +119,7 @@ func _readTransaction(r *bufio.Reader) (transaction, error) {
name: name, name: name,
value: value, value: value,
currency: currency, currency: currency,
isSet: isSet,
}) })
} }
} }
@ -179,39 +181,43 @@ func _readTransactionLine(r *bufio.Reader) ([]byte, error) {
return b2[:n], err 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) line, err := readTransactionLine(r)
if err != nil { if err != nil {
return "", 0, "", err return "", 0, "", false, err
} }
if len(line) > 0 && !unicode.IsSpace(rune(line[0])) { if len(line) > 0 && !unicode.IsSpace(rune(line[0])) {
r2 := *r r2 := *r
*r = *bufio.NewReader(io.MultiReader(bytes.NewReader(append(line, '\n')), &r2)) *r = *bufio.NewReader(io.MultiReader(bytes.NewReader(append(line, '\n')), &r2))
return "", 0, "", nil return "", 0, "", false, nil
} }
fields := bytes.Fields(line) fields := bytes.Fields(line)
isSet := false
if len(fields) > 2 && string(fields[1]) == "=" {
isSet = true
fields = append(fields[:1], fields[2:]...)
}
switch len(fields) { switch len(fields) {
case 1: // payee case 1: // payee
return string(fields[0]), 0, "", nil return string(fields[0]), 0, "", false, nil
case 2: // $00.00 case 2: // payee $00.00
b := bytes.TrimLeft(fields[1], "$") b := bytes.TrimLeft(fields[1], "$")
value, err := strconv.ParseFloat(string(b), 64) value, err := strconv.ParseFloat(string(b), 64)
if err != nil { 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 return string(fields[0]), value, string(USD), isSet, nil
case 3: // 00.00 XYZ case 3: // payee 00.00 XYZ
value, err := strconv.ParseFloat(string(fields[1]), 64) value, err := strconv.ParseFloat(string(fields[1]), 64)
if err != nil { 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 return string(fields[0]), value, string(fields[2]), isSet, 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
default: default:
return "", 0, "", fmt.Errorf("cannot interpret %q", line) return "", 0, "", isSet, fmt.Errorf("cannot interpret %q", line)
} }
} }