ENH skip lines without all fields
This commit is contained in:
parent
4835ab15ca
commit
8e2019ac5b
|
@ -351,10 +351,34 @@ let TxOpts_ =
|
||||||
Data.Time.Format.formattime Haskell function.
|
Data.Time.Format.formattime Haskell function.
|
||||||
-}
|
-}
|
||||||
Text
|
Text
|
||||||
, toSkipBlankDate : Bool
|
, toSkipBlankDate :
|
||||||
, toSkipBlankAmount : Bool
|
{-
|
||||||
, toSkipBlankDescription : Bool
|
Skip line if date field is a blank
|
||||||
, toSkipBlankOther : List Text
|
-}
|
||||||
|
Bool
|
||||||
|
, toSkipBlankAmount :
|
||||||
|
{-
|
||||||
|
Skip line if amount field(s) is(are) a blank
|
||||||
|
-}
|
||||||
|
Bool
|
||||||
|
, toSkipBlankDescription :
|
||||||
|
{-
|
||||||
|
Skip line if description field is a blank
|
||||||
|
-}
|
||||||
|
Bool
|
||||||
|
, toSkipBlankOther :
|
||||||
|
{-
|
||||||
|
Skip line if any arbitrary fields are blank (these fields must also
|
||||||
|
be listed in 'toOther' to be considered)
|
||||||
|
-}
|
||||||
|
List Text
|
||||||
|
, toSkipMissingFields :
|
||||||
|
{-
|
||||||
|
Skip line if any fields are missing (this is different from blank;
|
||||||
|
'missing' means there is no field with name 'X', 'blank' means that
|
||||||
|
there is a field 'X' and its value is an empty string)
|
||||||
|
-}
|
||||||
|
Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
let TxAmountSpec = TxAmountSpec_ Text
|
let TxAmountSpec = TxAmountSpec_ Text
|
||||||
|
@ -371,6 +395,7 @@ let TxOpts =
|
||||||
, toSkipBlankAmount = False
|
, toSkipBlankAmount = False
|
||||||
, toSkipBlankDescription = False
|
, toSkipBlankDescription = False
|
||||||
, toSkipBlankOther = [] : List Text
|
, toSkipBlankOther = [] : List Text
|
||||||
|
, toSkipMissingFields = False
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -124,31 +124,58 @@ parseTxRecord
|
||||||
, toSkipBlankAmount
|
, toSkipBlankAmount
|
||||||
, toSkipBlankDescription
|
, toSkipBlankDescription
|
||||||
, toSkipBlankOther
|
, toSkipBlankOther
|
||||||
|
, toSkipMissingFields
|
||||||
}
|
}
|
||||||
r = do
|
r =
|
||||||
d <- r .: T.encodeUtf8 toDate
|
do
|
||||||
e <- r .: T.encodeUtf8 toDesc
|
-- TODO this is confusing as hell
|
||||||
os <- M.fromList <$> mapM (\n -> (n,) <$> r .: T.encodeUtf8 n) toOther
|
--
|
||||||
|
-- try and parse all fields; if a parse fails, either trip an error
|
||||||
|
-- or return a Nothing if we want to deliberately skip missing fields
|
||||||
|
d <- getField toDate
|
||||||
|
e <- getField toDesc
|
||||||
|
os <-
|
||||||
|
fmap M.fromList . sequence
|
||||||
|
<$> mapM (\n -> fmap (n,) <$> getField n) toOther
|
||||||
(af, ax) <- case toAmount of
|
(af, ax) <- case toAmount of
|
||||||
|
-- the amount column is extra confusing because it can either be one
|
||||||
|
-- or two columns, so keep track of this with a maybe
|
||||||
AmountSingle TxAmount1 {a1Column, a1Fmt} -> do
|
AmountSingle TxAmount1 {a1Column, a1Fmt} -> do
|
||||||
f <- r .: T.encodeUtf8 a1Column
|
f <- getField a1Column
|
||||||
return (a1Fmt, Right f)
|
return (a1Fmt, Right <$> f)
|
||||||
AmountDual TxAmount2 {a2Positive, a2Negative, a2Fmt} -> do
|
AmountDual TxAmount2 {a2Positive, a2Negative, a2Fmt} -> do
|
||||||
f1 <- r .: T.encodeUtf8 a2Positive
|
f1 <- getField a2Positive
|
||||||
f2 <- r .: T.encodeUtf8 a2Negative
|
f2 <- getField a2Negative
|
||||||
return (a2Fmt, Left (f1, f2))
|
return $ (a2Fmt,) $ case (f1, f2) of
|
||||||
if (toSkipBlankDate && d == "")
|
(Just a, Just b) -> Just $ Left (a, b)
|
||||||
|| (toSkipBlankDescription && e == "")
|
_ -> Nothing
|
||||||
|| (toSkipBlankAmount && (ax == Right "" || ax == Left ("", "")))
|
case (d, e, os, ax) of
|
||||||
|| elem "" (mapMaybe (`M.lookup` os) toSkipBlankOther)
|
-- If all lookups were successful, check that none of the fields are
|
||||||
|
-- blank, and if they are return nothing to skip this line
|
||||||
|
(Just d', Just e', Just os', Just ax') ->
|
||||||
|
if (toSkipBlankDate && d' == "")
|
||||||
|
|| (toSkipBlankDescription && e' == "")
|
||||||
|
|| (toSkipBlankAmount && (ax' == Right "" || ax' == Left ("", "")))
|
||||||
|
|| elem "" (mapMaybe (`M.lookup` os') toSkipBlankOther)
|
||||||
then return Nothing
|
then return Nothing
|
||||||
else do
|
else -- if we are skipping nothing, proceed to parse the date and amount
|
||||||
a' <- case ax of
|
-- columns
|
||||||
|
do
|
||||||
|
a <- case ax' of
|
||||||
Right a -> parseDecimal True af a
|
Right a -> parseDecimal True af a
|
||||||
Left ("", a) -> ((-1) *) <$> parseDecimal False af a
|
Left ("", a) -> ((-1) *) <$> parseDecimal False af a
|
||||||
Left (a, _) -> parseDecimal False af a
|
Left (a, _) -> parseDecimal False af a
|
||||||
d' <- parseTimeM True defaultTimeLocale (T.unpack toDateFmt) d
|
d'' <- parseTimeM True defaultTimeLocale (T.unpack toDateFmt) d'
|
||||||
return $ Just $ TxRecord d' a' e os p
|
return $ Just $ TxRecord d'' a e' os' p
|
||||||
|
-- if no lookups succeeded, return nothing to skip this line. Note that
|
||||||
|
-- a parse fail will trigger a failure error further up, so that case
|
||||||
|
-- is already dealt with implicitly
|
||||||
|
_ -> return Nothing
|
||||||
|
where
|
||||||
|
getField :: FromField a => T.Text -> Parser (Maybe a)
|
||||||
|
getField f = case runParser $ r .: T.encodeUtf8 f of
|
||||||
|
Left err -> if toSkipMissingFields then return Nothing else fail err
|
||||||
|
Right x -> return $ Just x
|
||||||
|
|
||||||
matchRecords :: MonadFinance m => [StatementParserRe] -> [TxRecord] -> AppExceptT m [Tx ()]
|
matchRecords :: MonadFinance m => [StatementParserRe] -> [TxRecord] -> AppExceptT m [Tx ()]
|
||||||
matchRecords ms rs = do
|
matchRecords ms rs = do
|
||||||
|
|
|
@ -532,6 +532,7 @@ data TxOpts re = TxOpts
|
||||||
, toSkipBlankAmount :: !Bool
|
, toSkipBlankAmount :: !Bool
|
||||||
, toSkipBlankDescription :: !Bool
|
, toSkipBlankDescription :: !Bool
|
||||||
, toSkipBlankOther :: ![Text]
|
, toSkipBlankOther :: ![Text]
|
||||||
|
, toSkipMissingFields :: !Bool
|
||||||
}
|
}
|
||||||
deriving (Functor, Foldable, Traversable)
|
deriving (Functor, Foldable, Traversable)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue