convert: also require at least two fields in csv records, and don't error with empty csv file
This commit is contained in:
parent
b396e8e4a2
commit
98f1e2afdf
@ -10,7 +10,7 @@ import Ledger.Types (Ledger,AccountName,Transaction(..),Posting(..),PostingType(
|
|||||||
import Ledger.Utils (strip, spacenonewline, restofline)
|
import Ledger.Utils (strip, spacenonewline, restofline)
|
||||||
import Ledger.Parse (someamount, emptyCtx, ledgeraccountname)
|
import Ledger.Parse (someamount, emptyCtx, ledgeraccountname)
|
||||||
import Ledger.Amount (nullmixedamt)
|
import Ledger.Amount (nullmixedamt)
|
||||||
import Safe (atDef)
|
import Safe (atDef, maximumDef)
|
||||||
import System.IO (stderr)
|
import System.IO (stderr)
|
||||||
import Text.CSV (parseCSVFromFile, printCSV)
|
import Text.CSV (parseCSVFromFile, printCSV)
|
||||||
import Text.Printf (hPrintf)
|
import Text.Printf (hPrintf)
|
||||||
@ -48,19 +48,22 @@ convert opts args _ = do
|
|||||||
Left e -> error $ show e
|
Left e -> error $ show e
|
||||||
Right r -> r
|
Right r -> r
|
||||||
when debug $ hPrintf stderr "rules: %s\n" (show rules)
|
when debug $ hPrintf stderr "rules: %s\n" (show rules)
|
||||||
let maxfield = maxFieldIndex rules
|
let requiredfields = max 2 (maxFieldIndex rules + 1)
|
||||||
shortrecords = filter ((< maxfield).length) records
|
badrecords = take 1 $ filter ((< requiredfields).length) records
|
||||||
if null shortrecords
|
if null badrecords
|
||||||
then mapM_ (printTxn debug rules) records
|
then mapM_ (printTxn debug rules) records
|
||||||
else do
|
else do
|
||||||
hPrintf stderr (unlines [
|
hPrintf stderr (unlines [
|
||||||
"Warning, one or more CSV records do not contain field %d referenced by the"
|
"Warning, at least one CSV record does not contain a field referenced by the"
|
||||||
,"conversion rules file. Are you converting a valid CSV file ? First bad record:\n%s"
|
,"conversion rules file, or has less than two fields. Are you converting a"
|
||||||
]) maxfield (show $ head shortrecords)
|
,"valid CSV file ? First bad record:\n%s"
|
||||||
|
]) (show $ head badrecords)
|
||||||
exitFailure
|
exitFailure
|
||||||
|
|
||||||
|
-- | The highest (0-based) field index referenced in the field
|
||||||
|
-- definitions, or -1 if no fields are defined.
|
||||||
maxFieldIndex :: CsvRules -> Int
|
maxFieldIndex :: CsvRules -> Int
|
||||||
maxFieldIndex r = maximum $ catMaybes [
|
maxFieldIndex r = maximumDef (-1) $ catMaybes [
|
||||||
dateField r
|
dateField r
|
||||||
,statusField r
|
,statusField r
|
||||||
,codeField r
|
,codeField r
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user