jhthorsen / mojo-mysql

Mojolicious and Async MySQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Results class - object, objects

srchulo opened this issue · comments

I was wondering what you would think for the Mojo::mysql::Results to have an object and objects methods that correspond to hash and hashes, but instead returned objects with the keys as attributes. I was thinking this could be useful so that you don't need to worry about typos in hash keys if performance isn't a concern.

This should be easy to do with a role, which you could apply by setting database_class in Mojo::mysql to a Mojo::mysql::Database subclass that overrides results_class.

Yeah, that's a good idea. I can do that, but I wanted to see if this would be generally useful to include in Mojo::mysql::Results.

Put together one possibility at https://github.com/Grinnz/Mojo-DB-Results-Role-Struct. One to return Mojo::Base objects would be doable too. Still working on the mechanism to apply it to the Mojo::mysql object.

I'm giving this feature request a 👎 because I would rather encourage the programmer to write tests to prevent having/introducing typos.

Adding an custom result_class with this kind of functionality sounds like a better option to me.

@Grinnz that looks like a good solution!

@jhthorsen I agree that testing is important and should catch these typos, but I think the benefit of the method call is a very loud error that the programmer/application user can't miss vs. a potentially silent failure that returns undef for an invalid key. Not saying it should be included in this module (definitely fine with your decision), but for quickly written scripts or tests that aren't perfect, I think that a struct solution like mentioned above could be more robust.