Skip to content

Commit

Permalink
fix:csv: fix tag: queries on CSV data (#2114)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichael committed Nov 21, 2023
1 parent 4febb74 commit 2b18715
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions hledger-lib/Hledger/Read/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ module Hledger.Read.Common (
emptyorcommentlinep,
followingcommentp,
transactioncommentp,
commenttagsp,
postingcommentp,

-- ** bracketed dates
Expand Down
7 changes: 6 additions & 1 deletion hledger-lib/Hledger/Read/RulesReader.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ import Text.Printf (printf)

import Hledger.Data
import Hledger.Utils
import Hledger.Read.Common (aliasesFromOpts, Reader(..), InputOpts(..), amountp, statusp, journalFinalise, accountnamep )
import Hledger.Read.Common (aliasesFromOpts, Reader(..), InputOpts(..), amountp, statusp, journalFinalise, accountnamep, commenttagsp )
import Hledger.Read.CsvUtils
import System.Directory (doesFileExist, getHomeDirectory)
import Data.Either (fromRight)

--- ** doctest setup
-- $setup
Expand Down Expand Up @@ -1067,6 +1068,7 @@ transactionFromCsvRecord timesarezoned mtzin tzout sourcepos rules record = t
code = maybe "" singleline' $ fieldval "code"
description = maybe "" singleline' $ fieldval "description"
comment = maybe "" unescapeNewlines $ fieldval "comment"
ttags = fromRight [] $ rtp commenttagsp comment
precomment = maybe "" unescapeNewlines $ fieldval "precomment"

singleline' = T.unwords . filter (not . T.null) . map T.strip . T.lines
Expand All @@ -1079,6 +1081,7 @@ transactionFromCsvRecord timesarezoned mtzin tzout sourcepos rules record = t
p1IsVirtual = (accountNamePostingType <$> fieldval "account1") == Just VirtualPosting
ps = [p | n <- [1..maxpostings]
,let cmt = maybe "" unescapeNewlines $ fieldval ("comment"<> T.pack (show n))
,let ptags = fromRight [] $ rtp commenttagsp cmt
,let currency = fromMaybe "" (fieldval ("currency"<> T.pack (show n)) <|> fieldval "currency")
,let mamount = getAmount rules record currency p1IsVirtual n
,let mbalance = getBalance rules record currency n
Expand All @@ -1091,6 +1094,7 @@ transactionFromCsvRecord timesarezoned mtzin tzout sourcepos rules record = t
,ptransaction = Just t
,pbalanceassertion = mkBalanceAssertion rules record <$> mbalance
,pcomment = cmt
,ptags = ptags
,ptype = accountNamePostingType acct
}
]
Expand All @@ -1106,6 +1110,7 @@ transactionFromCsvRecord timesarezoned mtzin tzout sourcepos rules record = t
,tcode = code
,tdescription = description
,tcomment = comment
,ttags = ttags
,tprecedingcomment = precomment
,tpostings = ps
}
Expand Down
27 changes: 25 additions & 2 deletions hledger/test/csv.test
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ $ ./csvtest.sh

>=0

# ** 27. match a specific field
# ** 27. query by description
<
2020-01-01, 1
2020-01-01, 2
Expand Down Expand Up @@ -1094,7 +1094,30 @@ $ ./csvtest.sh
>2 /unexpected space/
>=1

## .
# ** 56. match transaction by posting tag (#2114)
<
2020-01-01, 1
RULES
fields date, desc
if %desc 1
comment ttag:tval
comment1 ptag:pval
account1 a

$ ./csvtest.sh tag:ptag
2020-01-01 ; ttag:tval
a ; ptag:pval

>=

# ** 57. match transaction by transaction tag (#2114)
$ ./csvtest.sh tag:ttag
2020-01-01 ; ttag:tval
a ; ptag:pval

>=

# ** .
#<
#$ ./csvtest.sh
#>=0
Expand Down

0 comments on commit 2b18715

Please sign in to comment.