bglusman

bglusman

So I’m looking for reactions and alternatives, and I know a lot of them will be negative/some of mine are too…

I just threw together this code in an hour or two after frustration with a bug in my project where one dev used a string-keyed map in some business logic where some existing code assumed it could use an atom… and we have a number of similar situations elsewhere, where we fail to do anything to the params at the Router or Controller level and they leak, unchanged, into deeper business logic modules.

Arguably that’s the problem itself, and I know a lot of you (probably correctly) will think this is a terrible idea. I think it might be a terrible idea. But I also wanted to see how hard it was to do and have a discussion here over alternatives, tradeoffs, and whether it’s fundamentally bad in any important way or just non-idiomatic, and if, as I’m sure many will think, it’s non-idiomatic, what’s the right alternative that tolerates mistakes and oversights and doesn’t have a lot of ceremony or boilerplate?

One obvious issue with this is the overhead… another is that Map’s are enumerable and if you use this on a Map you enumerate over, well, things will be a bit weird by default. What other issues come to mind? Anyone willing to say they like it/try it out in a personal project and see how they feel after using it a tiny bit/pointing to specific places it works well or badly, aside from the general concerns over being idiomatic?

I lean towards being pluralist and even if I don’t use this myself going forward, I wanted to see what it looked like when done/how hard it was, and the only comparable libraries I found were GitHub - vic/indifferent: Elixir Indifferent access on maps/lists/tuples with custom key transforms. · GitHub and GitHub - philosodad/morphix: Tiny library of functions to perform transforms on maps and arrays. · GitHub and neither seemed to do quite as simply what I had in mind… and it was easy enough I thought i should share the result and see if I learn anything, or if people surprise me and actually like it :upside_down_face:

Anyway here’s the github GitHub - bglusman/indifferent_access: [ALPHA] - Elixir Plug/Utility doing questionable things with maps/params · GitHub and the hex package indifferent_access | Hex

Showing Posts 1 to 10

NobbZ

NobbZ

I’ve only skimmed the implementation, but it seems as if it builds on a one-time creation of stringified/atomized counterkey with same value.

This seems to make things even worse…

One part of code updates atom key, another reads string key.

And even if you remember to re-key… Which should win if both exist?

The only solution are proper conversion to structs. No more arguing about string vs. atoms, just structs. Part of the contract, checked by dialyzer.

bglusman

bglusman OP

Yeah, to be fair this is an attempt to make the behavior backwards compatible to allow to gradually ensure the logic works, but if it were to go forward I imagine it would be a good/simple option to have to allow for replacing string keys that exist as atoms with atom keys pointing at same/indifferentized value… but I didn’t want to go too far down any path without talking and thinking about implications and tradeoffs.

As for re-keying, not sure if I understand your question/point, but it used Map.put_new so if there’s an existing atom key it never overwrites that, it only adds an atom key in addition. But maybe you meant something else?

I also had a version that left string keys with their unchanged values and only updated the value recursively on the atom key, but then thought if supporting both maybe best to do so consistently in both “paths” but, that would also make for a reasonable config/option if extending.

I still haven’t gotten a chance to really use Dialyzer since the Dialyxir updates I’ve heard have made it a lot more usable… maybe I’ll try it out on this as a simple little toy project without too much surface area, dunno… in any case, appreciate your feedback.

dimitarvp

dimitarvp

You will only introduce confusion by using this IMO.

What kind of a legacy problem are you trying to solve? Indeed, as @NobbZ suggested, convert the incoming controller parameters to a struct as quickly as you can so the compiler can cover part of the possible set of problems, and to be able to use Ecto as a validator as an added bonus.

What you suggest doing with this library is not just non-idiomatic; it’s terrible no matter what language you are doing it in. It increases the possible bug surface.

NobbZ

NobbZ

Let’s take a simple map, %{a: 1}, which you indifferentize. %{"a" => 1, a: 1} and then update "a", %{"a" => 2, a: 1}, now you have 2 conflicting values for the “same” key.

Your solution is not a solution, it’s changing pest for Cholera.

Always normalize the input at the edges of your system. In the system there is rarely a use case to assume non normalized data, and normalized data is nearly always a struct.

bglusman

bglusman OP

I agree I want to normalize at the edge of the system, but that’s not what I have in an existing substantial code base. I have a number of roadmap items to change some key params into an embedded_schema, which I see as helping to benefit from Ecto changeset and cast/dump behavior centralized well, but I’m not totally sold and/or educated/adapted to the idea that all params should immediately be changed into a Struct, and if I were, I don’t know what Structs those would be or how they’d change over time/which controllers would share them vs every controller having a slightly different one… (though to be fair, the bug that inspired this was a map pulled out of a param and passed on, and I suppose the same concern applies there even if the entire map isn’t treated this way, any value in it might be a map and get the same issues)… also though the point of using this would basically be that you’d only intend to work with the atom keys going forward, the string keys are there (for now) as a legacy/gradual adoption feature so its possible to drop this into existing code and not break things that rely on string keys until you’ve had a chance to move them over… but perhaps it’s better that they break loudly right away to make fixing them to use atoms a straightforward path of fixing failing tests and errors… again, I’m not at all sold on this general idea or this specific implementation, but these are good concerns to discuss for sure for anyone who does consider using it.

I also don’t think though that controller params, which is all this is intended to be used with, should really ever be updated in that sense… i think things are pulled out of them, perhaps with default values, but I don’t beleive the params map should be used wholesale as a map for any other purpose, and if only used there and never updated directly as you suggest, the “rekeying” problem is not much of a problem I think… not to say the other points aren’t valid, but, since I put this up for discussion, I’d like to make sure we’re actually discussing the same use case and reasonable concerns, not purely theoretical ones…

mhanberg

mhanberg

Expert LSP Core Team

I was sort of surprised at the API here.

I expected to see a IndifferentMap.get function that checks for atom and string keys, while using the normal map functions for putting and updating.

bglusman

bglusman OP

Yeah the other libraries I linked to may be more like that IIRC but I wanted to try something that “just worked” transparently via a plug for some or all pipelines in an app and was easy to try out and see without littering code with references to or dependencies on it… That said, obviously people (myself included) have some legitimate qualms with this approach but I threw together quickly and may never touch again…

bglusman

bglusman OP

Oh! But maybe there’s a fun idea there for implementing instead a struct named like you suggest that implements access behavior and does that… Then this plug could initialize that struct with params map… Maybe I’ll try that as an alternate iteration! Thanks!

Schultzer

Schultzer

Have you tried to pattern match?

def has_my_atom_key(%{mykey: _} = params) do
  params
end
def has_my_atom_key(params) do
  raise Brah, "We got a problem"
end

I find thats a trivial way to catch bugs, it’s not as good as a strong type system, but it’s the best we got.

bglusman

bglusman OP

I do pattern match quite a lot! Not entirely sure how/if something like that would scale to every usage and access to a different key in a different params map across dozens of controllers, and the purpose/scope of this library/discussion is sort of looking for patterns that are a tiny bit more “automatic” and or hard to mess up/forget than something like that, but… No doubt if we’re in Elixir pattern matching probably is/could be part of the answer!

Where Next? Top

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New
kip
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
New

Other Trending Topics Top

akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
bartblast
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
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews