Skip to content

Commit

Permalink
print error message on parse failure
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran committed Sep 30, 2023
1 parent 71f7f30 commit faa8c79
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
18 changes: 8 additions & 10 deletions internal/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func parseLedgerPrices(output string, defaultCurrency string) ([]price.Price, er

func parseHLedgerPrices(output string, defaultCurrency string) ([]price.Price, error) {
var prices []price.Price
re := regexp.MustCompile(`P (\d{4}-\d{2}-\d{2}) ([^\s\d.-]+) (.+)\n`)
re := regexp.MustCompile(`P (\d{4}-\d{2}-\d{2}) ([^\s\d.-]+|"[^"]+") (.+)\n`)
matches := re.FindAllStringSubmatch(output, -1)

for _, match := range matches {
Expand All @@ -240,20 +240,18 @@ func parseHLedgerPrices(output string, defaultCurrency string) ([]price.Price, e
}

func parseAmount(amount string) (string, decimal.Decimal, error) {
match := regexp.MustCompile(`^(-?[0-9.,]+)([^\d,.-]+)$|([^\d,.-]+)(-?[0-9.,]+)$|(-?[0-9.,]+)\s*("[^"]+")$`).FindStringSubmatch(amount)
match := regexp.MustCompile(`^(-?[0-9.,]+)([^\d,.-]+|\s*"[^"]+")$|([^\d,.-]+|\s*"[^"]+"\s*)(-?[0-9.,]+)$`).FindStringSubmatch(amount)
if len(match) == 0 {
log.Fatal("Could not parse amount: " + amount)
}

if match[1] != "" {
value, err := decimal.NewFromString(strings.ReplaceAll(match[1], ",", ""))
return utils.UnQuote(strings.Trim(match[2], " ")), value, err

}

if match[3] != "" {
value, err := decimal.NewFromString(strings.ReplaceAll(match[4], ",", ""))
return utils.UnQuote(strings.Trim(match[3], " ")), value, err
}

value, err := decimal.NewFromString(strings.ReplaceAll(match[5], ",", ""))
return utils.UnQuote(strings.Trim(match[6], " ")), value, err
value, err := decimal.NewFromString(strings.ReplaceAll(match[4], ",", ""))
return utils.UnQuote(strings.Trim(match[3], " ")), value, err

}

Expand Down
7 changes: 7 additions & 0 deletions internal/ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func TestParseHLegerPrices(t *testing.T) {
parsedPrices, _ = parseHLedgerPrices("P 2023-05-01 EUR 1.1$\n", "$")
assertPriceEqual(t, parsedPrices[0], "2023/05/01", "EUR", 1.1)

parsedPrices, _ = parseHLedgerPrices("P 2023-05-01 \"AAPL0\" \"USD0\" 45.5\n", "USD0")
assertPriceEqual(t, parsedPrices[0], "2023/05/01", "AAPL0", 45.5)

parsedPrices, _ = parseHLedgerPrices("P 2023-05-01 USD 0.9 EUR\n", "INR")
assert.Len(t, parsedPrices, 0)
parsedPrices, _ = parseHLedgerPrices("P 2023-05-01 USD $0.9\n", "INR")
Expand Down Expand Up @@ -79,4 +82,8 @@ func TestParseAmount(t *testing.T) {
commodity, amount, _ = parseAmount("-100,000.00 \"EUR0-0\"")
assert.Equal(t, "EUR0-0", commodity)
assert.Equal(t, -100000.0, amount.InexactFloat64())

commodity, amount, _ = parseAmount("\"EUR0-0\" -100,000.00")
assert.Equal(t, "EUR0-0", commodity)
assert.Equal(t, -100000.0, amount.InexactFloat64())
}

0 comments on commit faa8c79

Please sign in to comment.