treble37

treble37

When "stringifying" keys in a map, would you want to convert struct keys to strings?

Just looking for a little feedback on a tiny helper library I built -

Sometimes I find the need to convert maps with atom keys to maps with string keys, so %{:foo => 2} becomes %{"foo" => 2}…Has anyone ever come across the need to “string-ify” date/datetime/naivedatetime/structs when they are map keys? (I actually have yet to see a usecase where a date/struct is a map key, so I’m curious what others’ needs are as I’m making a little package with some helpers)…

If so, what would your ideal “stringification” of a struct/datetime/date/naivedatetime look like?

For example a %User{} struct could be stringified to:
"%User{age: 27, name: \"john\"}"

Most Liked

michalmuskala

michalmuskala

You could always consider using the String.Chars protocol with the Kernel.to_string/1 function and let the implementor of the data type decide if it has a sensible “stringly” representation.

kylethebaker

kylethebaker

An issue I can see with the way you’ve shown is that %User{age: 27, name: "john"} and %User{name: "john", age: 27,} represent the same struct, but "%User{age: 27, name: \"john\"}" and "%User{name: \"john\", age: 27}" are different strings. In other words, when in string format the key order becomes relevant. This could be an issue if you’re trying to do lookups using the stringified key. You might be able to avoid it by sorting the keys in the map/struct when you convert it, so that you know age always appears before name in the string.

With structs you should only have atom keys, but there is also a situation where you have a regular map as a key. In this case you would probably approach it the same way as structs, but you would need to consider that this map can itself have maps as keys (which may have maps as keys (which may have maps as keys (which may have…)). So you will need to approach it recursively and could end up with things like: %{%{%{%{\"foo\" => \"bar\"} => \"bax\"} => \"baz\"} => \"biz\"}.

When stringifying the map keys you lose the ability to reliably reverse the map to its original state (was %{"foo" => 1, "bar" => 2} originally %{foo: 1, bar: 2} or %{foo: 1, "bar" => 2}?). This also means that there are several different maps that could stringify into the same value. A map that has all string keys could have originally been a map that contained all atom keys, or a map that contained all string keys and a single atom key, or half and half, etc. If you’re treating atoms and string keys as the same then this might not be an issue, but this leads to situations where two values will have the same string key but would fail an equality check: stringify(foo) == stringify(bar) but bar != foo.

Where Next?

Popular in Libraries Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
cjen07
parameterized pipe in elixir: |n> edit: negative index in |n> and mixed usage with |> are supported example: use ParamP...
New
tfwright
After working on it for a couple of months and using it in production for most of that time, today I’ve released LiveAdmin, a LiveView ba...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
tmbb
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
New
mtrudel
Bandit is an HTTP server for Plug and WebSock apps. Bandit is written entirely in Elixir and is built atop Thousand Island. It can serve...
New
gjaldon
As the title states, EctoEnum has just been updated after some time of hardly any activity in the repo. Here’s the latest release: https:...
New
anshuman23
Hello all, I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New

Sub Categories:

We're in Beta

About us Mission Statement