-
-
Save SigmaDee/6142125d5f0c80afaf7f6ed3c3d1d3e0 to your computer and use it in GitHub Desktop.
Data.Yaml.Combinators part 2
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 | |
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 |
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' | |
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