-
-
Save SigmaDee/f01f0a0fd484d212e9a3a0b7e5f43822 to your computer and use it in GitHub Desktop.
Data.Yaml.Combinators, part 3
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 #-} | |
{-# LANGUAGE LambdaCase #-} | |
module Contacts | |
( Contacts, | |
contactsParser | |
) where | |
import Data.Vector (Vector) | |
import Data.Text (Text) | |
import Data.Yaml.Combinators | |
import Data.Maybe (fromMaybe) | |
data Contacts = Contacts | |
(Vector Person) | |
(Vector Organization) | |
deriving Show | |
data Gender = Male | |
| Female | |
| Unassigned | |
deriving Show | |
data Address = Address | |
Text -- street | |
Int -- house_number | |
deriving Show | |
data Person = Person | |
Text -- name | |
Address | |
Gender | |
deriving Show | |
data Organization = Organization | |
Text -- name | |
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 | |
genderParser :: Parser Gender | |
genderParser = | |
fmap (\case "male" -> Male | |
"female" -> Female | |
_ -> error "gender: shall be either 'male' or 'female'.") string | |
personParser :: Parser Person | |
personParser = object $ Person | |
<$> field "name" string | |
<*> addressParser | |
<*> (fromMaybe Unassigned <$> optField "gender" genderParser) | |
organizationParser :: Parser Organization | |
organizationParser = object $ Organization | |
<$> field "name" string | |
<*> addressParser | |
addressParser :: FieldParser Address | |
addressParser = Address | |
<$> field "street" string | |
<*> field "house_number" integer |
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' | |
street: 'Havenhurst Drive' | |
house_number: 1612 | |
- name: 'Diane Selwyn' | |
street: 'Griffith Park Boulevard' | |
house_number: 2900 | |
gender: 'female' | |
- name: 'Adam Kersher' | |
street: 'Spring Street' | |
house_number: 634 | |
gender: 'male' | |
organizations: | |
- name: 'StudioCanal' | |
street: 'Place du Spectacle' | |
house_number: 1 | |
- name: 'ABC Studios' | |
street: 'West 66th Street' | |
house_number: 77 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment