Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tableMap codec with list value is irreversible #335

Closed
samhh opened this issue Aug 28, 2020 · 6 comments · Fixed by #337
Closed

tableMap codec with list value is irreversible #335

samhh opened this issue Aug 28, 2020 · 6 comments · Fixed by #337
Assignees
Labels
bug Something isn't working

Comments

@samhh
Copy link

samhh commented Aug 28, 2020

Me again! 👋

It's possible I'm misusing the codecs. I've tried and failed to simplify the repro beyond this point, hopefully this helps.

import qualified Data.Map as M
import qualified Toml as Toml
import Toml ((.=))

data Inner = Inner { y :: Text } deriving (Show)
innerCodec = Inner <$> Toml.text "y" .= y

data Thing = Thing { xs :: Map Text [Inner] } deriving (Show)
thingCodec = Thing <$> Toml.tableMap Toml._KeyText (Toml.list innerCodec) "xs" .= xs

thing = Thing $ M.fromList [("k", [Inner "a", Inner "b"])]

reversed = Toml.decode thingCodec $ Toml.encode thingCodec thing
-- Right (Thing {xs = fromList []})

The encoded value looks like this written to a file with Toml.encodeToFile:

[xs]
  [[xs.k]]
    y = "a"

  [[xs.k]]
    y = "b"
@chshersh chshersh added the question Further information is requested label Aug 28, 2020
@chshersh
Copy link
Contributor

Hi @samhh, can you also tell what output you expect from the codec? It's not clear what's the problem.
Also, generally, specifying top-level signatures of all codecs can help with resolving issues, because due to type inference the inferred types of codecs might not be the same you expect.

@samhh
Copy link
Author

samhh commented Aug 28, 2020

Hey @chshersh,

In my real codebase I do have explicit type signatures, just tried to write a pared down repro. 🙂

What's confusing me is that the encoded value cannot be decoded - is this expected behaviour? I'd generally expect decode (encode x) == x to hold.

@chshersh
Copy link
Contributor

@samhh Thanks for more details! From the implementation of the tableMap codec I can see that it doesn't handle array of tables as an argument codec to tableMap at the moment. I guess, it should be possible to patch tableMap and take this into consideration.
Just wonder, what is your real use case and why you want to have such nested configurations? I'm curious about different use-cases for TOML and how it can be used in various scenarios 🙂

@samhh
Copy link
Author

samhh commented Aug 28, 2020

My use case is something I've just started working on.

I'm using TOML for user configuration and, until I'm given a compelling reason not to, also for data caching. The structure of said data is currently like this:

data Cache = Cache
  { timestamp :: LocalTime,
    feeds :: Map PodcastId (Map EpisodeId Episode)
  }

I'm contemplating changing that to this, which prompted this ticket:

data Cache = Cache
  { timestamp :: LocalTime,
    feeds :: Map PodcastId [Episode]
  }

@chshersh chshersh added bug Something isn't working and removed question Further information is requested labels Aug 30, 2020
@vrom911 vrom911 self-assigned this Aug 30, 2020
vrom911 added a commit that referenced this issue Aug 30, 2020
@vrom911
Copy link
Member

vrom911 commented Aug 30, 2020

@samhh we prepared a fix in #337 . Let us know if it doesn't work for your use-case 🙂

Current known limitation of the solution – it doesn't preserve empty lists in values of the map.

chshersh pushed a commit that referenced this issue Aug 30, 2020
* [#335] tableMap codec with list value is irreversible

Resolves #335

* Fix HLint suggestions
@samhh
Copy link
Author

samhh commented Aug 30, 2020

That's working perfectly, many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants