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

Can't encode Maps with record keys, nor Tuples #513

Open
ryanprior opened this issue Nov 13, 2021 · 2 comments
Open

Can't encode Maps with record keys, nor Tuples #513

ryanprior opened this issue Nov 13, 2021 · 2 comments
Assignees
Labels
enhancement New feature or request feature request

Comments

@ryanprior
Copy link
Contributor

You can encode a record, and you can encode a map, but you can't encode a map with record keys. You also can't encode Tuples, which seemed to me like it would be the best workaround.

Here's a sandbox illustrating the issue: https://sandbox.mint-lang.com/sandboxes/Cvi4ReudJVxywQ

Workarounds

I tried to work around this by first transforming the map using Map.entries. This didn't work for me because Tuple types also can't be encoded.

What did work was ultimately to write my own inline JS like so:

json =
  data
  |> Map.entries
  |> Array.map((data : Tuple(Room, String)) { `{room: {id: #{data[0].id}, name: #{data[0].name}}, when: #{data[1]}}` })
  |> Object.Encode.array
  |> Json.stringify
@gdotdesign gdotdesign added enhancement New feature or request feature request labels Nov 13, 2021
@gdotdesign
Copy link
Member

We can do automatic tuple decoding / encoding, no problem 💪

Supporting Map with any keys is more problematic, we have two options here:

  1. Map(a, b) would be encoded as [[a, b]] which is basically its internal representation but with the keys and values encoded. This would be a breaking change because now they are encoded as objects {a: b}.
  2. We would do as in the first option but encode / decode Map(String, a) as before so no breaking change since we only allowed String keys before, but this would mean different behavior with String keys and the rest.

I'm wondering what you folks think 💡 🤔

@gdotdesign gdotdesign self-assigned this Nov 17, 2021
@ryanprior
Copy link
Contributor Author

I'd like encode on a Map(a, b) to give a normal JS object if a is String, otherwise a JS Map. This would not work with Mint's Json.stringify but it could work for other serialization modules that could handle maps with non-string keys, such as if we introduce an EDN module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature request
Development

No branches or pull requests

2 participants