From 29349458b32fb1b8dc889a3735f9ebd74ae7b8ee Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 4 Dec 2024 17:45:28 -1000 Subject: [PATCH] imp:csv:if: go back to accepting unknown csv field names [#2289] It makes life easier when reusing common rules with different CSVs. --- hledger-lib/Hledger/Read/RulesReader.hs | 6 ++++-- hledger/hledger.m4.md | 8 ++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/hledger-lib/Hledger/Read/RulesReader.hs b/hledger-lib/Hledger/Read/RulesReader.hs index d7bd83d7f..cc34e947e 100644 --- a/hledger-lib/Hledger/Read/RulesReader.hs +++ b/hledger-lib/Hledger/Read/RulesReader.hs @@ -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.: diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index b7fae7d8e..1a007ef5d 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -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.