Skip to content

Add ability to convert between 'Toml' and user custom data types #25

@chshersh

Description

@chshersh

What we want is to have some way to describe correspondence between user data types and TOML representation. Consider this Haskell data type:

data Node = Node { root :: Bool, labels :: [Int] }

Would be great to have an ability to specify correspondence in some way like this:

nodeToml :: TomlM Node
nodeToml = do
    root   <- bool "root"
    labels <- array @Int "labels"
    pure Node{..}

And then we can have two functions:

decode :: Toml -> TomlM a -> Either DecodeError a
encode :: a -> TomlM a -> Toml

Difficult questions

  1. How this data type should be encoded in TOML?

Like this:

node.root = true
node.labels = [ 1, 2, 3 ]

or like this:

[node]
root = true
labels = [ 1, 2, 3 ]

I think we should give an ability to specify which way user want.

  1. How to encode sum types?

I propose the following strategy: if you have foo :: Either Int String then it should be either

foo.Left = 3

or

foo.Right = "bar"

If you have both keys, this should be a type error. And, again, give ability to specify foo as table. And this scheme easily can be extended if constructors have multiple fields.

  1. What to do with arrays of different values?

In TOML it's possible to have:

foo = [ [1, 2, 3], [true, false] ]

But Haskell doesn't really have a standard data structure for such case... So should we care about supporting this case or not?..

Metadata

Metadata

Assignees

Labels

astType of TOMLcodecConversion between TOML and custom user data types

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions