HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduletomland-1.3.3.3Haskell2010

Toml.Codec.Combinator.Map

SPDX-License-Identifier : MPL-2.0 Maintainer : Kowainik xrom.xkov@gmail.com Stability : Stable Portability : Portable

TOML-specific combinators for converting between TOML and Haskell Map-like data types.

There are two way to represent map-like structures with the tomland library.

  • Map structure with the key and value represented as key-value pairs:

    foo =
        [ {myKey = "name", myVal = 42}
        , {myKey = "otherName", myVal = 100}
        ]
    
  • Map structure as a table with the TOML key as the map key:

    [foo]
        name = 42
        otherName = 100
    

You can find both types of the codecs in this module for different map-like structures. See the following table for the heads up:

Haskell Type

TOML

TomlCodec

Map Int Text

x = [{k = 42, v = "foo"}]

map (Toml.int "k") (Toml.text "v") "x"

Map Text Int

x = {a = 42, b = 11}

tableMap Toml._KeyText Toml.int "x"

HashMap Int Text

x = [{k = 42, v = "foo"}]

hashMap (Toml.int "k") (Toml.text "v") "x"

HashMap Text Int

x = {a = 42, b = 11}

tableHashMap Toml._KeyText Toml.int "x"

IntMap Text

x = [{k = 42, v = "foo"}]

intMap (Toml.int "k") (Toml.text "v") "x"

IntMap Text

x = {1 = "one", 2 = "two"}

tableIntMap Toml._KeyInt Toml.text "x"

Note: in case of the missing key on the TOML side an empty map structure is returned.

  • 6 values
  • Packagetomland-1.3.3.3
  • Exports6
  • LanguageHaskell2010
  • LicenceMPL-2.0
  • SourceMap.hs

Map codecs

2 declarations
valuemap
  1. :: Ord k
  2. => TomlCodec k

    Codec for Map keys

  3. -> TomlCodec v

    Codec for Map values

  4. -> Key

    TOML key where Map is stored

  5. -> TomlCodec (Map k v)

    Codec for the Map

#

Bidirectional codec for Map. It takes birectional converter for keys and values and produces bidirectional codec for Map. Currently it works only with array of tables, so you need to specify Maps in TOML files like this:

myMap =
    [ { name = "foo", payload = 42 }
    , { name = "bar", payload = 69 }
    ]

TomlCodec for such TOML field can look like this:

Toml.map (Toml.text "name") (Toml.int "payload") "myMap"

If there's no key with the name "myMap" then empty Map is returned.

valuetableMap
  1. :: Ord k
  2. => TomlBiMap Key k

    Bidirectional converter between TOML Keys and Map keys

  3. -> (Key -> TomlCodec v)

    Codec for Map values for the corresponding Key

  4. -> Key

    Table name for Map

  5. -> TomlCodec (Map k v)
#

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 Toml.Codec.BiMap._KeyText or Toml.Codec.BiMap._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 } }
    ]

HashMap codecs

2 declarations
valuehashMap
  1. :: Hashable k
  2. => TomlCodec k

    Codec for HashMap keys

  3. -> TomlCodec v

    Codec for HashMap values

  4. -> Key

    TOML key where HashMap is stored

  5. -> TomlCodec (HashMap k v)

    Codec for the HashMap

#

Bidirectional codec for HashMap. It takes birectional converter for keys and values and produces bidirectional codec for HashMap. It works with array of tables, so you need to specify HashMaps in TOML files like this:

myHashMap =
    [ { name = "foo", payload = 42 }
    , { name = "bar", payload = 69 }
    ]

TomlCodec for such TOML field can look like this:

Toml.hashMap (Toml.text "name") (Toml.int "payload") "myHashMap"

If there's no key with the name "myHashMap" then empty HashMap is returned.

valuetableHashMap
  1. :: Hashable k
  2. => TomlBiMap Key k

    Bidirectional converter between TOML Keys and HashMap keys

  3. -> (Key -> TomlCodec v)

    Codec for HashMap values for the corresponding Key

  4. -> Key

    Table name for HashMap

  5. -> TomlCodec (HashMap k v)
#

This TomlCodec helps to convert TOML key-value pairs directly to HashMap using TOML keys as HashMap keys. It can be convenient if your HashMap keys are types like Text or Int and you want to work with raw TOML keys directly.

For example, if you can write your HashMap in TOML like this:

[myHashMap]
key1 = "value1"
key2 = "value2"

IntMap codecs

2 declarations
valueintMap
  1. :: TomlCodec Int

    Codec for IntMap keys

  2. -> TomlCodec v

    Codec for IntMap values

  3. -> Key

    TOML key where IntMap is stored

  4. -> TomlCodec (IntMap v)

    Codec for the IntMap

#

Bidirectional codec for IntMap. It takes birectional converter for keys and values and produces bidirectional codec for IntMap. It works with array of tables, so you need to specify IntMaps in TOML files like this:

myIntMap =
    [ { name = "foo", payload = 42 }
    , { name = "bar", payload = 69 }
    ]

TomlCodec for such TOML field can look like this:

Toml.intMap (Toml.text "name") (Toml.int "payload") "myIntMap"

If there's no key with the name "myIntMap" then empty IntMap is returned.