0

I am trying to create a simple configuration file to read static data from a file . I used the examples from ConfigFile-1.1.4 to read the data from a file, although I didn't understand bits of the code I do have a it working. But I need to thread this config file through out my application ,and I understand i need to use the readerT monad for that ?

The working part of my code to read from the file is below

getConfig = do
  config <- readConfig "config.cfg"
  putStrLn $ "The path value is: " ++ (path config)
  putStrLn $ "The filename value is: " ++ (fileName config)



readConfig :: String -> IO ConfigInfo
readConfig f = do
    rv <-
    runErrorT $
     do
      cp <- join $ liftIO $ readfile emptyCP f
      let x = cp
      -- read out the attributes
      pv <- get x "DEFAULT" "path"
      fv <- get x "DEFAULT" "filename"
      -- build the config value
      return (ConfigInfo {path = pv, fileName = fv})
    either (\x -> error (snd x)) (\x -> return x) rv

I have a function ,

get :: ID -> Name -> IO Keys
get u n = do
    p <- readFile ("getthisvaluefromconfigfile" ++ "getfromConfigfile")
    .
    .
    .
    return (p, b)

From the examples I read I am able to understand that the ask function would return me the environment ,and then i can use the functions for the ConfigInfo to extract values .

a)I am just not able to understand how do load the values into the environment in the getConfig function ?

b)And how do I specify in the ask function (Get )which instance of the ConfigInfo to read from ?

I did try to research I found some examples which use the reader monad ,but none of them show how to load the env .

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
Tanmay Bhattacharya
  • 551
  • 1
  • 5
  • 16
  • 2
    The Reader monad is a very thin layer of syntactic sugar over function parameters. `runReader (... ask ...) x` is like `(\env -> ... env ...) x`. – melpomene Mar 04 '19 at 14:10
  • Here's some general reading on `Reader`: [one](https://stackoverflow.com/questions/40066909/readert-static-environment), [two](https://stackoverflow.com/questions/14178889/what-is-the-purpose-of-the-reader-monad). [This one](https://stackoverflow.com/questions/3451546/help-with-reader-monad) probably provides the clearest example, though. – Michail Mar 04 '19 at 18:36
  • Thanks that hellped ,I sort of have a working example , but each of the functions need to be called using the runreader ,passing the configuration object ,iis that the right way to go about things ,or is there a more elegant solution available ? – Tanmay Bhattacharya Mar 05 '19 at 09:37
  • You certainly can run every `Reader` action through `runReader`, and it's probably a reasonable thing to do if the calling function has no business being in `Reader` monad as well. On the other hand, you can combine individual `Reader`s into a single one, and run that. All subordinate readers will have access to the same environment as the top-level one. (btw, @mention me next time, please - I kinda forget about ongoing conversations sometimes). – Michail Mar 05 '19 at 18:23

0 Answers0