imp:csv:if: go back to accepting unknown csv field names [#2289]

It makes life easier when reusing common rules with different CSVs.
This commit is contained in:
Simon Michael 2024-12-04 17:45:28 -10:00
parent 99fc4cd61f
commit 29349458b3
2 changed files with 8 additions and 6 deletions

View File

@ -808,8 +808,10 @@ isBlockActive rules record CB{..} = any (all matcherMatches) $ groupedMatchers c
matcherMatches = \case
RecordMatcher prefix pat -> maybeNegate prefix $ match pat $ T.intercalate "," record
FieldMatcher prefix csvfieldref pat -> maybeNegate prefix $ match pat $
fromMaybe (warn msg "") $ replaceCsvFieldReference rules record csvfieldref
where msg = "if "<>T.unpack csvfieldref<>": this should be a name declared with 'fields', or %NUM"
fromMaybe "" $ replaceCsvFieldReference rules record csvfieldref
-- (warn msg "") where msg = "if "<>T.unpack csvfieldref<>": this should be a name declared with 'fields', or %NUM"
-- #2289: we'd like to warn the user when an unknown CSV field is being referenced,
-- but it's useful to ignore it for easier reuse of rules files.
where match p v = regexMatchText (dbg7 "regex" p) (dbg7 "value" v)
-- | Group matchers into associative pairs based on prefix, e.g.:

View File

@ -3673,10 +3673,10 @@ When using these, there's two things to be aware of:
Eg when reading an SSV record like: `2023-01-01 ; "Acme, Inc. " ; 1,000`\
the whole record matcher sees instead: `2023-01-01,Acme, Inc. ,1,000`
2. In field matchers you must use either a CSV field number,
or a [CSV field name](#field-names) which has been set by a [`fields` list](#fields-list).
Anything else will print a warning, to avoid [confusion](https://github.com/simonmichael/hledger/issues/2289);
if you see it, you should adjust your matchers. This might become an error in future.
2. Field matchers expect either a CSV field number, or a [CSV field name](#field-names) declared with [`fields`](#fields-list).
(Don't use a hledger field name here, unless it is also a CSV field name.)
A non-CSV field name will cause the matcher to match against `""` (the empty string),
and does not raise an error, allowing easier reuse of common rules with different CSV files.
You can also prefix a matcher with `!` (and optional space) to negate it.
Eg `! whole foods`, `! %3 whole foods`, `!%description whole foods` will match if "whole foods" is NOT present.