Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
a sample of reading a yaml file by Haskell
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
import Data.Yaml
import Control.Applicative
data Config = Config { app :: AppConfig, db :: DbConfig } deriving (Show, Generic)
data AppConfig = AppConfig { port :: Int } deriving (Show)
data DbConfig = DbConfig { user :: String, password :: String, database :: String} deriving (Show, Generic)
-- instance FromJSON Config where
-- parseJSON (Object o) = Config <$>
-- o .: "app" <*>
-- o .: "db"
-- parseJSON x = fail $ show x
instance FromJSON Config
instance FromJSON AppConfig where
parseJSON (Object m) = AppConfig <$> m .:? "port" .!= 8081 -- default
instance FromJSON DbConfig
readConfig :: IO Config
readConfig =
either (error . show) id <$>
decodeFileEither "./conf/conf.yaml"
main :: IO ()
main = do
conf <- readConfig
putStrLn $ show conf
return ()
--
-- app :
-- port :
-- db :
-- user : "admin"
-- password : "pass"
-- database : "mydb"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment