Skip to content

Instantly share code, notes, and snippets.

@SigmaDee
Last active April 3, 2020 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SigmaDee/6142125d5f0c80afaf7f6ed3c3d1d3e0 to your computer and use it in GitHub Desktop.
Save SigmaDee/6142125d5f0c80afaf7f6ed3c3d1d3e0 to your computer and use it in GitHub Desktop.
Data.Yaml.Combinators part 2
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
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 Gender = Male
| Female
| Unassigned
deriving Show
data Person = Person
Text -- name
Text -- address
Gender
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
<*> field "gender" genderParser
genderParser :: Parser Gender
genderParser =
fmap (\case "male" -> Male
"female" -> Female
_ -> error "gender: shall be either 'male' or 'female'.") string
organizationParser :: Parser Organization
organizationParser = object $ Organization
<$> field "name" string
<*> field "address" string
persons:
- name: 'Coco Lenoix'
address: '1612 Havenhurst Drive'
gender: 'female'
- name: 'Diane Selwyn'
address: '2900 Griffith Park Boulevard'
gender: 'female'
organizations:
- name: 'StudioCanal'
address: '1 Place du Spectacle'
- name: 'ABC Studios'
address: '77 West 66th Street'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment