Skip to content

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

@ryanprior

Description

@ryanprior

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

Metadata

Metadata

Assignees

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions