Skip to content

web: dev: ReportPage.Option: provide a single source of truth for report options - #2807

Closed
thielema wants to merge 1 commit into
hledgerorg:mainfrom
thielema:web-option-single-truth
Closed

thielema wants to merge 1 commit into
hledgerorg:mainfrom
thielema:web-option-single-truth

Conversation

@thielema

@thielema thielema commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

accumulationOption, calcOption: demonstration of collecting all required information in one place

I added several ToDo comments. I have not yet tried to remove duplicate parameter name literals like "calc". I defer that one to the next refactoring iteration.

I moved Widgets.Common.calcLinks to ReportPage and think all link generators should be moved to ReportPage. I also suggest to replace the long import lists in Handler.BalanceR by qualified imports from ReportPage.

@thielema
thielema force-pushed the web-option-single-truth branch from 34865a7 to d89d08d Compare October 24, 2026 19:11
@simonmichael

Copy link
Copy Markdown
Member

Thanks @thielema. This relates to

It's aiming to solve this problem, which is what #2798 was talking about:

In hledger-web, each multiple-choice URL parameter (accum, calc, list, sort, value...) is described in several separate places:

  • a parser from URL word to mode (parseAccum in ReportPage.hs);
  • an encoder from mode to URL word (accumWord in Widget/Common.hs);
  • a links row with each mode's label, tooltip and link (accumulationLinks);
  • the parameter name as a string literal, which appears about 29 times across the handlers.

Nothing forces these to agree. If you rename a value, or add a mode, and miss one place, links can produce URLs the parser rejects, or a mode gets no link.

By gathering all the info relating to each parameter into a table. It defines a new Option type inspired by Yesod's Option (used in web forms).

My impression is that this draft is adding more new machinery than is needed. Here's a simpler idea (deriving the parser from the encoders) - what do you think of it ?

A simpler fix

The duplication that causes the bugs is narrower than the full option table: it is the URL words (written once in the parser, again in the encoder) and the parameter name (repeated as a string literal in about 29 places). Labels and tooltips are used only by the links rows, so they are already in one place.

So how about keeping the encoders we already have (accumWord, calcWord, listWord, valueWord) and deriving each parser by inverting its encoder:

-- | A multiple-choice URL parameter: its name, its modes in display order,
-- and each mode's URL word.
data Param a = Param { paramName :: Text, paramModes :: [a], paramWord :: a -> Text }

accumParam :: Param BalanceAccumulation
accumParam = Param "accum" [PerPeriod, Historical, Cumulative] accumWord

-- | Read a parameter's value; empty or absent means the page's default.
parseParam :: Param a -> Maybe Text -> Either ReportParamError (Maybe a)
parseParam p = \case
  Nothing -> Right Nothing
  Just "" -> Right Nothing
  Just w  -> maybe (Left $ BadParamValue (paramName p) w) (Right . Just) $
               find ((== w) . paramWord p) (paramModes p)

-- | The parameter setting the given mode.
paramSetting :: Param a -> a -> (Text, Text)
paramSetting p a = (paramName p, paramWord p a)

That's about 10 lines of shared code, plus about one line per parameter. With it:

  • Each URL word is written once, in the encoder. The encoder is a total case, so adding a mode gives a missing-case warning.
  • The parameter name is written once, which fixes the repeated literals.
  • BadAccum and the hand-written parsers go away.
  • The display order is explicit in paramModes, rather than depending on the order the constructors are declared in.
  • Labels and tooltips stay in the links rows, the only place that uses them.
  • Alternative words such as calc=change can be a small case before the lookup, or an optional extra field if several parameters need them.

It also fits #2788: each field of a URL state record would be read and written through its Param.

@simonmichael simonmichael added the web The hledger-web tool. label Oct 24, 2026
@thielema

Copy link
Copy Markdown
Contributor Author

I thought I needed two option parsers, because parseAccum returns Maybe BalanceAccumulation, where Nothing refers to a "page's default" according to the comment, whereas parseCalc returns BalanceCalculation, and its default is CalcChange.
If you leave link generation and parser separated you still have to repeat the connection between BalanceCalculation and other values, right? However, if you manage to re-use the parsers from ReportOptions this would be great, because it would ensure coherence between CLI and URL arguments.
I used the approach of a case on all BalanceAccumulation values in order to check that all possible values are supported. However, I found that calcLinks does not support all values of BalanceCalculation.

@simonmichael

Copy link
Copy Markdown
Member

It might be great to unify all the way to the CLI, but only if it doesn't create more headaches. And it doesn't sound urgent.

@thielema
thielema force-pushed the web-option-single-truth branch from d89d08d to a20c968 Compare October 26, 2026 12:06
@thielema

Copy link
Copy Markdown
Contributor Author

If we have no immediate consensus on the single source of truth we can also first examine the ToDos I added to the draft.

@simonmichael

Copy link
Copy Markdown
Member

Can you clarify @thielema - do you agree with that "simpler fix" approach above ?

@simonmichael

Copy link
Copy Markdown
Member

we can also first examine the ToDos I added to the draft.

I looked at your new push to this PR but don't see what you mean - where are those ?

@thielema

thielema commented Oct 27, 2026 via email •

Copy link
Copy Markdown
Contributor Author

@thielema

thielema commented Oct 27, 2026 via email

Copy link
Copy Markdown
Contributor Author

@thielema
thielema force-pushed the web-option-single-truth branch from a20c968 to f942994 Compare October 27, 2026 21:14
…ort options

accumulationOption, calcOption: demonstration of collecting all required information in one place
@simonmichael

Copy link
Copy Markdown
Member

Thanks @thielema. Continuing the discussion from here and #2821:

We seem to agree on:

  • Each parameter should have a single source of truth, including its links row.
  • BadAccum can go; a generic BadParamValue is enough for hand-edited URLs.
  • All the *Links functions should live in one module.

I do not see how it simpler.

It's about 20 shared lines, versus about 65 in the draft, which looked boilerplatey to me.

But your per-mode case is the right core, I think, and my sketch has moved closer to it (see below).

You somehow have to do a JOIN between option parser/generator and link list generator, and this is a source of potential inconsistency.

I take your point. That earlier sketch kept the URL words consistent, but a links row could still omit a mode, add one, or order them differently. We could prevent that by putting the labels into Param and generating the links row from it:

-- | A multiple-choice URL parameter: its name, its modes in display order,
-- the mode used when it's absent (if any), and each mode's URL word,
-- label and tooltip.
data Param a = Param
  { paramName    :: Text
  , paramModes   :: [a]
  , paramDefault :: Maybe a
  , paramWord    :: a -> Text
  , paramLabel   :: Translations -> a -> (Text, Text)
  }

-- | Read a parameter's value; empty or absent gives the default.
parseParam :: Param a -> Maybe Text -> Either ReportParamError (Maybe a)

-- | A links row choosing one of the parameter's modes.
-- The default mode's link omits the parameter.
paramLinks :: Eq a => Param a -> Translations -> Text -> r -> [(Text, Text)] -> a -> HtmlUrl r
paramLinks p trs heading route kept current =
  linksRowClass (paramName p) heading
    [ (lbl, tip, (route, kept ++ [paramSetting p a | Just a /= paramDefault p]), a == current)
    | a <- paramModes p, let (lbl, tip) = paramLabel p trs a ]

With paramWord and paramLabel written as total cases, a mode added to the type gives a missing-case warning in both places. The mode list, words, labels and parameter name are each written once, and the parser, the links row and the handlers all read them from the same Param, which also removes the repeated "calc"-style literals from #2798 (your draft leaves those for later).

An explicit mode list also gives us more freedom in the UI, to show a subset of the constructors (like calc), or a chosen order, independent of the type declarations.

Once the parameter handling is settled, we can clarify the modules. Eg Param definitions and paramLinks could go together in one module (name to taste), which ReportPage and the handlers import.

Would you like to try adapting #2807 along these lines ? Or if you prefer, I could try it.

@simonmichael

Copy link
Copy Markdown
Member

Any other opinions welcome too (@acinader).

@acinader

Copy link
Copy Markdown
Contributor

🍿

@thielema

Copy link
Copy Markdown
Contributor Author

Sounds all reasonable to me. Since you have a clear idea how to reorganize the code I'd ask you and your LLM to prepare a new pull request.

@simonmichael

Copy link
Copy Markdown
Member

Will do. I made a start, and it's (of course) more complicated than it seems. If I get time to work it out and do sufficient testing, it might still get done before 1.99.6, scheduled for 11/1; if not I'll pick it up again after that.

simonmichael added a commit that referenced this pull request Nov 15, 2026
…r; calc uses it

A multiple-choice URL parameter such as calc was described in several
places that nothing kept in agreement: its parser, its encoder, its row
of links, the options summary's names for it, and its name as a string
literal across the handlers (#2798). Now a Param value holds its name,
its modes in display order, each mode's URL word, and its labels, and
the parser (parseParam, readParam, lookupParam), the row of links
(paramLinks), the URL settings (paramSetting, paramSettings) and the
options summary's names (paramPhrase) are made from it.

The new Hledger.Web.Param module also takes the generic links row
renderer (linksRow, linksRowClass, linksRowGroups) from Widget.Common,
which now imports it, avoiding an import cycle.

calc is the first parameter converted, replacing parseCalc, calcWord and
calcLinks. Its labels and translator comments are unchanged, so its
translations are too. The journal and register pages now carry only a
valid calc value on their links, rather than any value given.

Supersedes #2807 and #2821, following the design discussed in #2807.

AI usage: Claude Fable 5.1/Opus 5.5, ~51k output tokens
@simonmichael

Copy link
Copy Markdown
Member

Closed in favour of #2851.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

web The hledger-web tool.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants