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 Announcing Top

danschultzer
In short Plug n’ play OAuth 2.0 provider library. Just set up a resource owner schema with Ecto (your user schema), install the dependen...
New
mischov
import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do...
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
riverrun
I’ve just released version 3 of Comeonin, a password hashing library. The following small changes have been made: changes to the NIF c...
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
cjen07
parameterized pipe in elixir: |n> edit: negative index in |n> and mixed usage with |> are supported example: use ParamPipe ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
Jskalc
Hi! Today, after a couple weeks of development I’ve released v0.1 of LiveVue. It’s a seamless integration of Vue and Phoenix LiveView, i...
New
dominicletz
Hi, I thought I had posted my library before but seems I hadn’t. The project is still in early stages but it’s growing and so I think it...
New
trisolaran
Hi! :waving_hand: I would like to present LiveSelect, a little library that I wrote to easily add a dynamic selection input to your LV f...
198 10978 107
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48475 226
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement