Skip to content

Commit

Permalink
fix:check tags: _price-matched -> _cost-matched, and whitelisted [#2247]
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichael committed Oct 15, 2024
1 parent b8497c8 commit 71a7879
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion hledger-lib/Hledger/Data/Balancing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ transactionCheckBalanced BalancingOpts{commodity_styles_} t = errs
-- convert this posting's amount to cost,
-- without getting confused by redundant costs/equity postings
postingBalancingAmount p
| "_price-matched" `elem` map fst (ptags p) = mixedAmountStripCosts $ pamount p
| "_cost-matched" `elem` map fst (ptags p) = mixedAmountStripCosts $ pamount p
| otherwise = mixedAmountCost $ pamount p

-- transaction balancedness is checked at each commodity's display precision
Expand Down
3 changes: 2 additions & 1 deletion hledger-lib/Hledger/Data/JournalChecks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ builtinTags = [
,"_generated-transaction" -- always exists on generated periodic txns
,"_generated-posting" -- always exists on generated auto postings
,"_modified" -- always exists on txns which have had auto postings added
,"_conversion-matched" -- exists on postings which have been matched with a nearby @/@@ cost notation
,"_conversion-matched" -- marks postings with a cost which have been matched with a nearby pair of equity conversion postings
,"_cost-matched" -- marks equity conversion postings which have been matched with a nearby posting with a cost
]

-- | In each tranaction, check that any conversion postings occur in adjacent pairs.
Expand Down
4 changes: 2 additions & 2 deletions hledger-lib/Hledger/Data/Posting.hs
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,13 @@ postingToCost ToCost p
-- Make sure not to duplicate them when matching ones exist already.
postingAddInferredEquityPostings :: Bool -> Text -> Posting -> [Posting]
postingAddInferredEquityPostings verbosetags equityAcct p
| "_price-matched" `elem` map fst (ptags p) = [p]
| "_cost-matched" `elem` map fst (ptags p) = [p]
| otherwise = taggedPosting : concatMap conversionPostings costs
where
costs = filter (isJust . acost) . amountsRaw $ pamount p
taggedPosting
| null costs = p
| otherwise = p{ ptags = ("_price-matched","") : ptags p }
| otherwise = p{ ptags = ("_cost-matched","") : ptags p }
conversionPostings amt = case acost amt of
Nothing -> []
Just _ -> [ cp{ paccount = accountPrefix <> amtCommodity
Expand Down
6 changes: 3 additions & 3 deletions hledger-lib/Hledger/Data/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ transactionInferCostsFromEquity dryrun conversionaccts t = first (annotateErrorW
-- Approximately: given a pair of conversion postings to match,
-- and lists of the remaining unmatched costful and costless other postings,
-- 1. find (and consume) two other postings which match the two conversion postings
-- 2. add identifying tags to the four postings
-- 2. add identifying (hidden) tags to the four postings
-- 3. add an explicit cost, if missing, to one of the matched other postings
-- 4. or if there is a problem, raise an informative error or do nothing as appropriate.
-- Or, if the first argument is true:
Expand All @@ -326,8 +326,8 @@ transactionInferCostsFromEquity dryrun conversionaccts t = first (annotateErrorW

-- A function that adds a cost and/or tag to a numbered posting if appropriate.
postingAddCostAndOrTag np costp (n,p) =
(n, if | n == np -> costp `postingAddTags` [("_price-matched","")]
| n == n1 || n == n2 -> p `postingAddTags` [("_conversion-matched","")]
(n, if | n == np -> costp `postingAddTags` [("_cost-matched","")] -- add this tag to the posting with a cost
| n == n1 || n == n2 -> p `postingAddTags` [("_conversion-matched","")] -- add this tag to the two equity conversion postings
| otherwise -> p)

-- Annotate any errors with the conversion posting pair
Expand Down
8 changes: 6 additions & 2 deletions hledger/hledger.m4.md
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,7 @@ Tags you can set to influence hledger's behaviour:
date2 -- overrides a posting's secondary date
type -- declares an account's type
```

Tags hledger adds to indicate generated data:
```
t -- appears on postings generated by timedot letters
Expand All @@ -1814,14 +1815,17 @@ Tags hledger adds to indicate generated data:
generated-transaction -- appears on generated periodic txns (with --verbose-tags)
generated-posting -- appears on generated auto postings (with --verbose-tags)
modified -- appears on txns which have had auto postings added (with --verbose-tags)
Not displayed, but queryable:
_generated-transaction -- exists on generated periodic txns (always)
_generated-posting -- exists on generated auto postings (always)
_modified -- exists on txns which have had auto postings added (always)
```
Tags hledger uses internally:

Other tags hledger uses internally:
```
_conversion-matched -- exists on postings which have been matched with a nearby @/@@ cost annotation
_cost-matched -- marks postings with a cost which have been matched with a nearby pair of equity conversion postings
_conversion-matched -- marks equity conversion postings which have been matched with a nearby posting with a cost
```

### Tag values
Expand Down
2 changes: 2 additions & 0 deletions hledger/test/check-tags.test
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ $ hledger -f - check tags
; _generated-posting:
; _modified:
; _conversion-matched:
; _cost-matched:
$ hledger -f - check tags

# ** 7. Declaring the built-in special tags is harmless.
Expand All @@ -77,4 +78,5 @@ tag _generated-transaction ; always exists on generated periodic txns
tag _generated-posting ; always exists on generated auto postings
tag _modified ; always exists on txns which have had auto postings added
tag _conversion-matched ; exists on postings which have been matched with a nearby @/@@ cost notation
tag _cost-matched ; exists on postings which have been matched with a nearby @/@@ cost notation
$ hledger -f - check tags

0 comments on commit 71a7879

Please sign in to comment.