convert transaction with = to normal but pass isSet=true

This commit is contained in:
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

@@ -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)
}
}