Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict variable expansion #13

Open
zudov opened this issue Aug 6, 2018 · 1 comment
Open

Strict variable expansion #13

zudov opened this issue Aug 6, 2018 · 1 comment

Comments

@zudov
Copy link

zudov commented Aug 6, 2018

We've run into a bit of confusion (here KSF-Media/tuttifrutti#12) with identifier parsing in our log module when using template to support variable interpolation.

While the actual issue comes from the lack of documentation in our library. I think we could eliminate such confusion by adding a "strict" mode in which, $foo won't be allowed, only ${foo}. It will also eliminate the need to escape $ with $$.

Such functionality could be added in backwards compatible way by introducing a templateStrict :: Text -> Template that has such behavior.

@trskop
Copy link
Collaborator

trskop commented Aug 6, 2018

I was thinking about this for a while. My idea was to create new module Data.Text.Template.Parse that would expose following:

data ParseOptions = ParseOptions
    { bracketsRequired :: Bool
    , escapeChar :: Char
    }

data ParseError = ParseError
    { sourceLocation :: Maybe SrcLoc
    -- ^ Location of template parsing function in Haskell source code, if supported.
    , templateLocation :: (Int, Int)
    -- ^ Location in the template where parsing error was encountered.
    }

parse :: HasCallStack => ParseOptions -> Text -> Either ParseError Template
parse = parseImpl

Would something like this work for you as well?

@trskop trskop mentioned this issue Aug 6, 2018
trskop added a commit to trskop/template that referenced this issue Aug 6, 2018
* Alternative API for parsing `Text` into `Template` which allows users to
  specify if they want brackets to be mandatory. Instead of simple
  location this API returns `ParseError` on failure.

* Function `template` throws `ParseError`, instead of calling `error`.
  No other function in existing public API has (visibly) changed.

Addresses issues tibbe#13 and tibbe#14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants