Copyright | (c) Matthew Peddie 2014 |
---|---|
License | BSD3 |
Maintainer | mpeddie@gmail.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | None |
Language | Haskell2010 |
Optics for the libconfig types in Language.Libconfig.Types.
- settingValue :: Lens' Setting Value
- settingName :: Lens' Setting Name
- _nameText :: Prism' Text Name
- _Scalar :: Prism' Value Scalar
- _Array :: Prism' Value Array
- _List :: Prism' Value List
- _Group :: Prism' Value Group
- _Boolean :: Prism' Scalar Bool
- _Integer :: Prism' Scalar Int32
- _Integer64 :: Prism' Scalar Int64
- _Hex :: Prism' Scalar Word32
- _Hex64 :: Prism' Scalar Word64
- _Float :: Prism' Scalar Double
- _String :: Prism' Scalar Text
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 Lens
es 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 Prism
s 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 Traversal
s 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 Traversal
s 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)