treble37
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\"}"
Trending in Announcing
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
I released Doggo, a collection of unstyled Phoenix components.
https://github.com/woylie/doggo
Features
Unstyled Phoenix components....
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hello
Published a new library - ProcessHub!
ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
It’s not that it’s vocabulary is too advanced. It’s something worse.
I get lost trying to follow even a paragraph written by Claude. It’...
New
This showed up on my feed.. anyone heard of it? Just hype?
Ox Alpha is a reasoning model designed for coding, sustained ag...
New
Hey folks,
I just published a post about Hologram’s funding and where the project goes next - the short version:
Curiosum as Main Spons...
New
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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 knowagealways appears beforenamein 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)butbar != foo.treble37
Thank you for the feedback @kylethebaker. Right now, the helper package does handle nested maps.
But I didn’t think about the use case of having a map in a “mixed key type” state (e.g., it contains both strings and atoms) and not beibng able to go back to the original state. All my use cases have been either I need a map with all atom type keys or all string type keys.
And I see your point about structs and key lookups.
Perhaps I shall start with the simplest use case (which is what I’m doing currently) - handle atoms/strings (in nested and non-nested maps) and leave structs and struct-like objects alone…I have to chew on this some more and perhaps others will chime in on their use cases.
kylethebaker
There are a few other types to consider as well, namely PIDs, lists, tuples, and functions. I’m not sure how often these would come up in practice. As a shortcut to stringify any of them you can pass the value to
inspect/1and you will get some sort of string representation. I don’t know how useful this string representation would actually be though, I think it depends on the use case.One thing I noticed in your
stringifyandatomizefunctions, you are using the enumerable property of maps to apply the transformation and then casting it back into a map. You can instead useMap.new/2as a shortcut to do this all at once. For example:Can be simply:
michalmuskala
You could always consider using the
String.Charsprotocol with theKernel.to_string/1function and let the implementor of the data type decide if it has a sensible “stringly” representation.