libconfig-0.3.0.0: Haskell bindings to libconfig

Copyright(c) Matthew Peddie 2014
LicenseBSD3
Maintainermpeddie@gmail.com
Stabilityexperimental
PortabilityGHC
Safe HaskellNone
LanguageHaskell2010

Language.Libconfig.Optics

Contents

Description

Optics for the libconfig types in Language.Libconfig.Types.

Synopsis

Documentation

 

In order to run the usage examples in ghci, some setup is required:

>>> :set -XOverloadedStrings
>>> import Control.Lens
>>> :set -XCPP
>>> :set -DDEFINE_PRISMS
>>> let Just asset = textToName "asset"
>>> let Just price = textToName "price"

Setting

These Lenses are first-class references into the parts of a Setting.

settingValue :: Lens' Setting Value Source

>>> (asset := Scalar (String "butts")) ^. settingValue
Scalar (String "butts")
>>> (asset := Scalar (String "butts")) & settingValue .~ Scalar (Float 22.2)
"asset" := Scalar (Float 22.2)

settingName :: Lens' Setting Name Source

>>> (asset := Scalar (String "butts")) ^. settingName
"asset"
>>> let Just shake = textToName "shake"
>>> (asset := Scalar (String "butts")) & settingName .~ shake
"shake" := Scalar (String "butts")

_nameText :: Prism' Text Name Source

Here is a Prism' for accessing the string value of a Name.

>>> _nameText # asset
"asset"
>>> "butts" ^? _nameText
Just "butts"
>>> :t ("butts" :: Text) ^? _nameText
("butts" :: Text) ^? _nameText :: Maybe Name

N.B.: _nameText is partial in the opposite direction to the usual Prisms for sum types (e.g. _Left, _Just). This makes it a bit puzzling to compose. We use re _nameText with view:

>>> (asset := Scalar (String "butts")) ^. settingName . re _nameText
"asset"

I don't know how to get it to compose properly for setting.

Value

These Prisms provide Traversals for tweaking the relevant part of a Value. They can also be turned around to obtain the embedding into the relevant constructor.

_Scalar :: Prism' Value Scalar Source

>>> Scalar (String "butts") ^? _Scalar
Just (String "butts")
>>> (asset := Scalar (String "butts")) & settingValue . _Scalar . _String .~ "money"
"asset" := Scalar (String "money")
>>> _Scalar # String "butts"
Scalar (String "butts")

_Array :: Prism' Value Array Source

>>> Array [String "butts"] ^? _Array
Just [String "butts"]
>>> (asset := Array [String "butts"]) & settingValue . _Array . traverse . _String .~ "money"
"asset" := Array [String "money"]

_List :: Prism' Value List Source

>>> List [Scalar (String "butts"), Scalar (Float 22.2)] ^? _List . ix 0
Just (Scalar (String "butts"))
>>> List [Scalar (String "butts"), Scalar (Float 22.2)] & _List . traverse . _Scalar . _Float %~ (*2)
List [Scalar (String "butts"),Scalar (Float 44.4)]

_Group :: Prism' Value Group Source

>>> Group [asset := Scalar (String "butts"), price := Scalar (Float 22.2)] ^? _Group . ix 0
Just ("asset" := Scalar (String "butts"))
>>> Group [asset := Scalar (String "butts"), price := Scalar (Float 22.2)] & _Group . traverse . settingValue . _Scalar . _Float %~ (*2)
Group ["asset" := Scalar (String "butts"),"price" := Scalar (Float 44.4)]

Scalar

These Prisms provide Traversals for tweaking the relevant part of a Scalar. They can also be turned around to obtain the embedding into the relevant constructor.

_Boolean :: Prism' Scalar Bool Source

>>> Boolean False ^? _Boolean
Just False
>>> Scalar (Boolean False) & _Scalar . _Boolean %~ not
Scalar (Boolean True)

_Integer :: Prism' Scalar Int32 Source

>>> Integer 22 ^? _Integer
Just 22
>>> Scalar (Integer 22) & _Scalar . _Integer %~ (*2)
Scalar (Integer 44)

_Integer64 :: Prism' Scalar Int64 Source

>>> Integer64 2222222222 ^? _Integer64
Just 2222222222
>>> Scalar (Integer64 2222222222) & _Scalar . _Integer64 %~ (*2)
Scalar (Integer64 4444444444)

_Hex :: Prism' Scalar Word32 Source

>>> Hex 22 ^? _Hex
Just 22
>>> Scalar (Hex 22) & _Scalar . _Hex %~ (*2)
Scalar (Hex 44)

_Hex64 :: Prism' Scalar Word64 Source

>>> Hex64 2222222222 ^? _Hex64
Just 2222222222
>>> Scalar (Hex64 2222222222) & _Scalar . _Hex64 %~ (*2)
Scalar (Hex64 4444444444)

_Float :: Prism' Scalar Double Source

>>> Float 22.22 ^? _Float
Just 22.22
>>> Scalar (Float 22.22) & _Scalar . _Float %~ (*2)
Scalar (Float 44.44)

_String :: Prism' Scalar Text Source

>>> String "butts" ^? _String
Just "butts"
>>> Float 22.22 ^? _String
Nothing
>>> import Data.Monoid ((<>))
>>> Scalar (String "butts") & _Scalar . _String %~ ("hello " <>)
Scalar (String "hello butts")