Generic codec for arbitrary data types. Uses field names as keys.
Moduletomland-1.3.3.3Haskell2010
Toml.Codec.Generic
SPDX-License-Identifier : MPL-2.0 Maintainer : Kowainik xrom.xkov@gmail.com Stability : Stable Portability : Portable
This module contains implementation of the Generic TOML codec. If your data types are big and nested, and you want to have codecs for them without writing a lot of boilerplate code, you can find this module helpful. Below you can find the detailed explanation on how the Generic codecs work.
Consider the following Haskell data types:
data User = User
{ age :: Int
, address :: Address
, socials :: [Social]
} deriving (Generic)
data Address = Address
{ street :: Text
, house :: Int
} deriving (Generic)
data Social = Social
{ name :: Text
, link :: Text
} deriving (Generic)
Value of the User type represents the following TOML:
age = 27
[address]
street = "Miami Beach"
house = 42
[[socials]]
name = "Twitter"
link = "https://twitter.com/foo"
[[socials]]
name = "GitHub"
link = "https://github.com/bar"
Normally you would write TomlCodec for this data type like this:
userCodec :: TomlCodec User
userCodec = User
<$> Toml.int "age" .= age
<*> Toml.table addressCodec "address" .= address
<*> Toml.list socialCodec "socials" .= socials
addressCodec :: TomlCodec Address
addressCodec = Address
<$> Toml.text "street" .= street
<*> Toml.int "house" .= house
socialCodec :: TomlCodec Social
socialCodec = Social
<$> Toml.text "name" .= name
<*> Toml.text "link" .= link
However, if you derive Generic instance for your data types (as we do in the example), you can write your codecs in a simpler way.
userCodec :: TomlCodec User
userCodec = genericCodec
instance HasCodec Address where
hasCodec = Toml.table genericCodec
instance HasItemCodec Social where
hasItemCodec = Right genericCodec
Several notes about the interface:
Your top-level data types are always implemented as genericCodec (or other generic codecs).
If you have a custom data type as a field of another type, you need to implement the instance of the HasCodec typeclass.
If the data type appears as an element of a list, you need to implement the instance of the HasItemCodec typeclass.
- 8 types
- 3 classes
- 5 values
- Packagetomland-1.3.3.3
- Exports16
- LanguageHaskell2010
- LicenceMPL-2.0
- SourceGeneric.hs
Generic codec with options for arbitrary data types.
Generic codec that uses stripTypeNameOptions.
Options
4 declarationsOptions to configure various parameters of generic encoding. Specifically:
tomlOptionsFieldModifier: how to translate field names to TOML keys?
Constructors
TomlOptionstomlOptionsFieldModifier :: Typeable a => Proxy a -> String -> String
Same as TomlOptions but with all data type information erased. This data type is used internally. Define your options using TomlOptions data type.
Constructors
Options that use stripTypeNamePrefix as tomlOptionsFieldModifier.
Strips name of the type name from field name prefix.
data UserData = UserData { userDataId :: Int, userDataShortInfo :: Text }stripTypeNamePrefix (Proxy @UserData) "userDataId""id"stripTypeNamePrefix (Proxy @UserData) "userDataShortInfo""shortInfo"stripTypeNamePrefix (Proxy @UserData) "udStats""stats"stripTypeNamePrefix (Proxy @UserData) "fooBar""bar"stripTypeNamePrefix (Proxy @UserData) "name""name"
Core generic typeclass
3 declarationsHelper typeclass for generic deriving. This instance tells how the data type should be coded if it's a field of another data type.
NOTE: If you implement TOML codecs for your data types manually, prefer more
explicit Toml.int or Toml.text instead of implicit Toml.hasCodec.
Implement instances of this typeclass only when using genericCodec and when
your custom data types are not covered here.
Instances35HasCodec, …
HasCodec IntSetDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec IntegerDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec NaturalDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec AllDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec AnyDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec Word8Defined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec BoolDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec DoubleDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec FloatDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec IntDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec WordDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec TextDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec TextDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec DayDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec LocalTimeDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec TimeOfDayDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec ZonedTimeDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec ByteStringAsBytesDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec ByteStringAsTextDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec LByteStringAsBytesDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec LByteStringAsTextDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec a => HasCodec (First a)Defined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec a => HasCodec (Last a)Defined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec a => HasCodec (Maybe a)Defined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec v => HasCodec (IntMap v)Defined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec a => HasCodec (NonEmpty a)Defined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec a => HasCodec [a]Defined in tomland-1.3.3.3 · Toml.Codec.Generic(Generic a, GenericCodec (Rep a)) => HasCodec (TomlTable a)Defined in tomland-1.3.3.3 · Toml.Codec.Generic(Generic a, GenericCodec (Rep a), Typeable a) => HasCodec (TomlTableStrip a)Defined in tomland-1.3.3.3 · Toml.Codec.Generic(Num a, HasCodec a) => HasCodec (Product a)Defined in tomland-1.3.3.3 · Toml.Codec.Generic(Num a, HasCodec a) => HasCodec (Sum a)Defined in tomland-1.3.3.3 · Toml.Codec.Generic(Ord a, HasItemCodec a) => HasCodec (Set a)Defined in tomland-1.3.3.3 · Toml.Codec.Generic(Hashable a, HasItemCodec a) => HasCodec (HashSet a)Defined in tomland-1.3.3.3 · Toml.Codec.Generic(Ord k, HasCodec k, HasCodec v) => HasCodec (Map k v)Defined in tomland-1.3.3.3 · Toml.Codec.Generic(Hashable k, HasCodec k, HasCodec v) => HasCodec (HashMap k v)Defined in tomland-1.3.3.3 · Toml.Codec.GenericEncodes HashMap as array of tables with the
keyandvalTOML key names for HashMap keys and values. E.g. if you have a typeHashMap Text Int, the HasCodec instance for Generic deriving will work with the following TOML representation:fieldName = [ { key = "foo", val = 15 } , { key = "bar", val = 7 } ]
This typeclass tells how the data type should be coded as an item of a list. Lists in TOML can have two types: primitive and table of arrays.
If hasItemCodec returns Left: primitive arrays codec is used.
If hasItemCodec returns 'Right:' table of arrays codec is used.
Methods
hasItemCodec :: Either (TomlBiMap a AnyValue) (TomlCodec a)
Instances22HasItemCodec, …
HasItemCodec IntSetDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec IntegerDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec NaturalDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec Word8Defined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec BoolDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec DoubleDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec FloatDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec IntDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec WordDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec TextDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec TextDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec DayDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec LocalTimeDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec TimeOfDayDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec ZonedTimeDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec ByteStringAsBytesDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec ByteStringAsTextDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec LByteStringAsBytesDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec LByteStringAsTextDefined in tomland-1.3.3.3 · Toml.Codec.Generic(Generic a, GenericCodec (Rep a)) => HasItemCodec (TomlTable a)Defined in tomland-1.3.3.3 · Toml.Codec.Generic(Generic a, GenericCodec (Rep a), Typeable a) => HasItemCodec (TomlTableStrip a)Defined in tomland-1.3.3.3 · Toml.Codec.Generic(HasItemCodec a, Typeable a) => HasItemCodec [a]Defined in tomland-1.3.3.3 · Toml.Codec.GenericIf data type
ais not primitive then this instance returns codec for list under key equal toatype name.
Helper class to derive TOML codecs generically.
Methods
genericTomlCodec :: GenericOptions -> TomlCodec (f p)
Instances5GenericCodec
TypeError GenericSumTomlNotSupported => GenericCodec (f :+: g)Defined in tomland-1.3.3.3 · Toml.Codec.GenericGenericCodec f => GenericCodec (C1 c f)Defined in tomland-1.3.3.3 · Toml.Codec.GenericGenericCodec f => GenericCodec (D1 d f)Defined in tomland-1.3.3.3 · Toml.Codec.Generic(Selector s, HasCodec a) => GenericCodec (S1 s (Rec0 a))Defined in tomland-1.3.3.3 · Toml.Codec.Generic(GenericCodec f, GenericCodec g) => GenericCodec (f :*: g)Defined in tomland-1.3.3.3 · Toml.Codec.Generic
ByteString newtypes
4 declarationsThere are two ways to encode ByteString in TOML:
Via text.
Via an array of integers (aka array of bytes).
To handle all these cases, tomland provides helpful newtypes, specifically:
As a bonus, on GHC >= 8.6 you can use these newtypes with the DerivingVia
extensions for your own ByteString types.
newtype MyByteString = MyByteString
{ unMyByteString :: ByteString
} deriving HasCodec via ByteStringAsBytes
Newtype wrapper over ByteString to be used for text values.
Constructors
Instances4Eq, Show, HasCodec, HasItemCodec
Eq ByteStringAsTextDefined in tomland-1.3.3.3 · Toml.Codec.GenericShow ByteStringAsTextDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec ByteStringAsTextDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec ByteStringAsTextDefined in tomland-1.3.3.3 · Toml.Codec.Generic
Newtype wrapper over ByteString to be used for array of integers representation.
Constructors
Instances4Eq, Show, HasCodec, HasItemCodec
Eq ByteStringAsBytesDefined in tomland-1.3.3.3 · Toml.Codec.GenericShow ByteStringAsBytesDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec ByteStringAsBytesDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec ByteStringAsBytesDefined in tomland-1.3.3.3 · Toml.Codec.Generic
Newtype wrapper over lazy ByteString to be used for text values.
Constructors
Instances4Eq, Show, HasCodec, HasItemCodec
Eq LByteStringAsTextDefined in tomland-1.3.3.3 · Toml.Codec.GenericShow LByteStringAsTextDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec LByteStringAsTextDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec LByteStringAsTextDefined in tomland-1.3.3.3 · Toml.Codec.Generic
Newtype wrapper over lazy ByteString to be used for array of integers representation.
Constructors
Instances4Eq, Show, HasCodec, HasItemCodec
Eq LByteStringAsBytesDefined in tomland-1.3.3.3 · Toml.Codec.GenericShow LByteStringAsBytesDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasCodec LByteStringAsBytesDefined in tomland-1.3.3.3 · Toml.Codec.GenericHasItemCodec LByteStringAsBytesDefined in tomland-1.3.3.3 · Toml.Codec.Generic
Deriving Via
2 declarationsnewtype for generic deriving of HasCodec typeclass for custom data
types that should we wrapped into separate table. Use it only for data types
that are fields of another data types.
data Person = Person
{ personName :: !Text
, personAddress :: !Address
} deriving (Generic)
data Address = Address
{ addressStreet :: !Text
, addressHouse :: !Int
} deriving (Generic)
deriving HasCodec via TomlTable Address
personCodec :: TomlCodec Person
personCodec = stripTypeNameCodec
personCodec corresponds to the TOML of the following structure:
name = "foo"
[address]
addressStreet = "Bar"
addressHouse = 42
Constructors
TomlTableunTomlTable :: a
Instances2HasCodec, HasItemCodec
(Generic a, GenericCodec (Rep a)) => HasCodec (TomlTable a)Defined in tomland-1.3.3.3 · Toml.Codec.Generic(Generic a, GenericCodec (Rep a)) => HasItemCodec (TomlTable a)Defined in tomland-1.3.3.3 · Toml.Codec.Generic
newtype for generic deriving of HasCodec typeclass for custom data
types that should be wrapped into a separate table.
Similar to TomlTable but also strips the data type name prefix from TOML keys.
personCodec from the TomlTable comment corresponds to the TOML of
the following structure:
name = "foo"
[address]
street = "Bar"
house = 42
Constructors
Instances2HasCodec, HasItemCodec
(Generic a, GenericCodec (Rep a), Typeable a) => HasCodec (TomlTableStrip a)Defined in tomland-1.3.3.3 · Toml.Codec.Generic(Generic a, GenericCodec (Rep a), Typeable a) => HasItemCodec (TomlTableStrip a)Defined in tomland-1.3.3.3 · Toml.Codec.Generic