yaml-light-lens-0.3.2: Lens interface to yaml-light.

Safe HaskellNone
LanguageHaskell2010

Data.Yaml.YamlLight.Lens

Contents

Description

Lenses for working with YAML structures.

Synopsis

Traversals

nth :: Int -> Traversal' YamlLight YamlLight Source

Lens into a sequence.

>>> YSeq [YStr "a", YStr "b", YStr "c"] ^? nth 1
Just (YStr "b")
>>> YSeq [YStr "a", YStr "b", YStr "c"] & nth 1 .~ YStr "B"
YSeq [YStr "a",YStr "B",YStr "c"]
>>> YSeq [YStr "a", YStr "b", YStr "c"] ^? nth 2 . _Yaml :: Maybe String
Just "c"

key :: ByteString -> Traversal' YamlLight YamlLight Source

Lens into a mapping. ByteStrings are used as keys directly. If you wish to use a complex mapping key, see key'.

>>> let m = YMap $ Map.fromList [(YStr "name", YStr "Tony Stark"), (YStr "sequels", YStr "2")]
>>> m & key "sequels" . _Yaml +~ 1
YMap (fromList [(YStr "name",YStr "Tony Stark"),(YStr "sequels",YStr "3")])

key' :: YamlLight -> Traversal' YamlLight YamlLight Source

Lens into a mapping using a complex key.

Yaml parsing prism

_Yaml :: AsYaml a => Prism' YamlLight a Source

Convert between YAML values and corresponding common Haskell values.

>>> YStr "-2.3" ^? _Yaml :: Maybe Double
Just (-2.3)
>>> YStr "7b.3" ^? _Yaml :: Maybe Double
Nothing
>>> YStr "-23" ^? _Yaml :: Maybe Int
Just (-23)
>>> YStr "Help, I'm trapped in a haddock factory!" ^? _Yaml :: Maybe String
Just "Help, I'm trapped in a haddock factory!"
>>> YStr "An integer" ^? _Yaml :: Maybe Integer
Nothing

If we just want to pull out those values that were successfully parsed,

>>> let nums = YSeq [YStr "3", YStr "2a", YStr "1"]
>>> nums ^.. each._Yaml :: [Int]
[3,1]

Alternately, we may want to fail the entire parse if any element fails to parse.

>>> sequenceA $ map (preview _Yaml) (nums ^.. each) :: Maybe [Int]
Nothing
>>> let nums' = YSeq [YStr "3", YStr "2", YStr "1"]
>>> sequenceA $ map (preview _Yaml) (nums' ^.. each) :: Maybe [Int]
Just [3,2,1]

class AsYaml a where Source

Convert between YAML values and common types of Haskell values.

Numeric parsers

yamlInt :: Integral b => YamlLight -> Maybe b Source

Try to parse an Integral value from a YamlLight.

yamlReal :: Fractional b => YamlLight -> Maybe b Source

Try to parse a Fractional value from a YamlLight.