lib: implement skip end in csv reader

This commit is contained in:
Dmitry Astapov 2019-10-14 21:46:35 +01:00
parent b2ba1086b6
commit 38db7eb24d
3 changed files with 10 additions and 4 deletions

View File

@ -225,6 +225,7 @@ validateCsv rules numhdrlines (Right rs) = validate $ applyConditionalSkips $ dr
case getEffectiveAssignment rules r "skip" of
Nothing -> Nothing
Just "" -> Just 1
Just "end" -> Just maxBound
Just x -> Just (read x)
applyConditionalSkips [] = []
applyConditionalSkips (r:rest) =

View File

@ -178,12 +178,12 @@ Note, interpolation strips any outer whitespace, so a CSV value like
## conditional block
`if` *`PATTERN`*\
    *`FIELDASSIGNMENTS or skip N`*...
    *`FIELDASSIGNMENTS` or `skip N` or `skip end`*...
`if`\
*`PATTERN`*\
*`PATTERN`*...\
    *`FIELDASSIGNMENTS or skip N`*...
    *`FIELDASSIGNMENTS` or `skip N` or `skip end`*...
This applies one or more field assignments, only to those CSV records matched by one of the PATTERNs.
The patterns are case-insensitive regular expressions which match anywhere
@ -192,7 +192,7 @@ specific field). When there are multiple patterns they can be written
on separate lines, unindented.
The field assignments are on separate lines indented by at least one space.
Instead of field assignments you can specify `skip N` to skip the next N records (including the one that matchied).
Instead of field assignments you can specify `skip N` to skip the next N records (including the one that matchied). Special form `skip end` will cause the rest of the file to be skipped.
Examples:
```rules

View File

@ -359,6 +359,8 @@ AND THIS
AND THIS ONE
10/2009/09,Flubber Co,50
*** END OF FILE ***
More lines of the trailer here
They all should be ignored
RULES
fields date, description, amount
date-format %d/%Y/%m
@ -366,9 +368,12 @@ currency $
account1 assets:myacct
if HEADER
END OF FILE
skip
if
END OF FILE
skip end
if MIDDLE
skip 3