This
TomlCodec helps you to convert TOML key-value pairs
directly to
Map using TOML keys as
Map keys. It can be
convenient if your
Map keys are types like
Text or
Int and you want to work with raw TOML keys directly.
For example, if you have TOML like this:
[colours]
yellow = "#FFFF00"
red = { red = 255, green = 0, blue = 0 }
pink = "#FFC0CB"
You want to convert such TOML configuration into the following Haskell
types:
data Rgb = Rgb
{ rgbRed :: Int
, rgbGreen :: Int
, rgbBlue :: Int
}
data Colour
= Hex Text
| RGB Rgb
colourCodec :: TomlCodec Colour
colourCodec = ...
data ColourConfig = ColourConfig
{ configColours :: Map Text Colour
}
And you want in the result to have a
Map like this:
fromList
[ "yellow" -> Hex "#FFFF00"
, "pink" -> Hex "#FFC0CB"
, "red" -> Rgb 255 0 0
]
You can use
tableMap to define
TomlCodec in the
following way:
colourConfigCodec :: TomlCodec ColourConfig
colourConfigCodec = ColourConfig
<$> Toml.tableMap Toml._KeyText colourCodec "colours" .= configColours
Hint: You can use
_KeyText or
_KeyString to
convert betwen TOML keys and
Map keys (or you can write your
custom
TomlBiMap).
NOTE: Unlike the
map codec, this codec is less flexible
(i.e. it doesn't allow to have arbitrary structures as
Keys, it
works only for text-like keys), but can be helpful if you want to save
a few keystrokes during TOML configuration. A similar TOML
configuration, but suitable for the
map codec will look like
this:
colours =
[ { key = "yellow", hex = "#FFFF00" }
, { key = "pink", hex = "#FFC0CB" }
, { key = "red", rgb = { red = 255, green = 0, blue = 0 } }
]