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

Question: New Semantics for Reader / Default #18

Closed
StevenXL opened this issue Jul 18, 2020 · 5 comments
Closed

Question: New Semantics for Reader / Default #18

StevenXL opened this issue Jul 18, 2020 · 5 comments

Comments

@StevenXL
Copy link

I was wondering if it was possible to have a Reader e a with the following behavior:

  1. If the key is not set, then return Nothing
  2. If the key is set, but not parseable into the domain type, return UnreadError s
  3. If the key is set, and parsable into the domain type, return Just <CustomType>

For help with this problem, I have a full Haskell script which does not have the desire behavior. The focal point of the script should be declaration of readCustom, which is my custom Reader.

The current behavior of the script is that it correctly implements behavior 2 and 3 in the list above, but not behavior 1. If I add def Nothing to the third argument of var, then behavior 1 and 3 are correctly implemented, but not behavior 2. (In the case of behavior 2, the value is Nothing).

#!/usr/bin/env stack
-- stack --resolver lts-14.7 script --package envparse

import           Control.Monad (unless)
import           Env
import           Text.Read

newtype Custom = Custom { unCustom :: Int } deriving (Show, Eq)

data Environment = Environment { envInt :: Maybe Int, envString :: Maybe String, envCustom :: Maybe Custom } deriving (Show, Eq)

environmentParser :: Parser Error Environment
environmentParser =
  Environment <$> optional (var auto "INT"  (help "The environment integer"))
                        <*> optional (var str "STRING"  (help "The environment string"))
                        <*> var readCustom "CUSTOM"  (help "The environment custom")
  where readCustom :: Reader Error (Maybe Custom)
        readCustom rawStr = case readMaybe rawStr of
                               Nothing -> Left $ UnreadError rawStr
                               Just i  -> Right $ Just $ Custom i

hello :: IO Environment
hello = parse (header "envparse example") (prefixed "ENV_" environmentParser)

main :: IO ()
main = parse (header "envparse example") (prefixed "ENV_" environmentParser) >>= print
@supki
Copy link
Owner

supki commented Jul 22, 2020

The underlying problem is that default values overrode parse errors before 28bf50b.
Adding def Nothing should work as expected in HEAD.

Hopefully, I'll get around to releasing the fix to Hackage in a couple of days, thanks for reminding me!

supki added a commit that referenced this issue Jul 22, 2020
@StevenXL
Copy link
Author

The underlying problem is that default values overrode parse errors before 28bf50b.
Adding def Nothing should work as expected in HEAD.

Hopefully, I'll get around to releasing the fix to Hackage in a couple of days, thanks for reminding me!

Thank you for the library!

@NorfairKing
Copy link

@supki It looks like this release hasn't happened yet. Gentle ping about that.

@ocharles
Copy link

ocharles commented Aug 4, 2021

Another nudge from me to cut a release - I'd love to see this!

@NorfairKing
Copy link

Another ping to the maintainer, the same is asked in #20

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

4 participants