-
-
Save SigmaDee/012308f9c62d29203beec7be4bd7b7a7 to your computer and use it in GitHub Desktop.
Data.Yaml.Combinators part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Contacts | |
( Contacts, | |
contactsParser | |
) where | |
import Data.Vector (Vector) | |
import Data.Text (Text) | |
import Data.Yaml.Combinators | |
data Contacts = Contacts | |
(Vector Person) | |
(Vector Organization) | |
deriving Show | |
data Person = Person | |
Text -- name | |
Text -- address | |
deriving Show | |
data Organization = Organization | |
Text -- name | |
Text -- address | |
deriving Show | |
contactsParser :: Parser Contacts | |
contactsParser = object $ Contacts | |
<$> field "persons" personListParser | |
<*> field "organizations" organizationListParser | |
personListParser :: Parser (Vector Person) | |
personListParser = array personParser | |
organizationListParser :: Parser (Vector Organization) | |
organizationListParser = array organizationParser | |
personParser :: Parser Person | |
personParser = object $ Person | |
<$> field "name" string | |
<*> field "address" string | |
organizationParser :: Parser Organization | |
organizationParser = object $ Organization | |
<$> field "name" string | |
<*> field "address" string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
persons: | |
- name: 'Coco Lenoix' | |
address: '1612 Havenhurst Drive' | |
- name: 'Diane Selwyn' | |
address: '2900 Griffith Park Boulevard' | |
organizations: | |
- name: 'StudioCanal' | |
address: '1 Place du Spectacle' | |
- name: 'ABC Studios' | |
address: '77 West 66th Street' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import qualified Data.ByteString.Char8 as BS | |
import qualified Data.Yaml.Combinators as Y | |
import System.Environment (getArgs) | |
import System.IO | |
import Contacts | |
main :: IO () | |
main = do | |
args <- getArgs | |
bs <- BS.readFile $ head args | |
let parsedContent = Y.parse contactsParser bs :: Either String Contacts | |
case parsedContent of | |
(Left e) -> error e | |
(Right ts) -> putStrLn $ show ts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment