-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array support for params (or better error message...) #109
Comments
I've got a similar issue using an array value as the param:
The job: class SomeJob < Mosquito::QueuedJob
param keys : Array(String)
...
end My solution in the meantime is to serialize to JSON. |
The mosquito serializer logic is certainly primitive. For this particular case, it's kind of straightforward to emit an error suggesting the developer do something else: elsif type.id =~ /Array|Hash/
raise "Mosquito Job: Unable to build serialization for #{type}. Have you considered serializing using json?" That looks like this:
There's certainly a future in better serialization logic, but it's a lot of lift and I'm not sure the payoff is worth it at this time. I looked for a bit but I didn't see any better way in crystal macros to inspect the type to see if it's an array or a hash -- this problem would probably also rear if it were specified as an Enum or a Tuple. The logic for any of these things is trivial for the single case but significantly harder to generalize -- Avram has a lot of infrastructure around making this work. In the mean time, is it reasonable to add an error message like that? Should it also regex on Tuple, Enum, or anything else? |
I think it's reasonable to just add the error message for now with a suggestion. Avram actually just checks What would be nice is something like Ruby's Marshal 😂 though, I guess JSON works here too? Is that what Sidekiq is doing? |
A better error is definitely an improvement. As @jwoertink says, opening the door for That said, what about supporting simple serialization? Something along the lines of: class SomeJob < Mosquito::QueuedJob
param keys : Array(String), as: :json
end Or perhaps: class SomeJob < Mosquito::QueuedJob
json_param keys : Array(String)
end In the background that would call class SomeJob < Mosquito::QueuedJob
json_param data : SomeData
struct SomeData
include JSON::Serializable
...
end
end I'm not sure, but maybe JSON serialization like that can be made to work on the current It's just an idea, I'm not trying to get you to work or anything 😊️. |
Yes, I think if I was going to support serialization it'd need to have some sort of interface module similar to what avram does. I toyed around with like: module Mosqito::Serializer(T)
abstract serialize(input : T) : String
abstract deserialize(input : String) : T
end
class KlassSerializer
include Mosquito::Serializer(Klass)
# ...
end
class Job < QueuedJob
param data : Klass, serialize_with: KlassSerializer
end It's a great idea and I'd like to implement it...but for now I think I'll just use Jeremy's suggestion to check for Array or maybe Generic so the error is less opaque and let it be for now. |
I'm not sure if arrays can be supported, so if not, then I'm cool with just a better error message too..
Current, if you try to add an array type, you'll get the error
This comes from this line
mosquito/src/mosquito/queued_job.cr
Line 49 in d0e40c3
because
simplified_type
is defaulted tonil
and only set if the type declaration typeis_a? Path
, but it's not... it's aGeneric
.The code ends up trying to make
@ids : nil?
which is a syntax error.Which means this block is skipped
mosquito/src/mosquito/queued_job.cr
Lines 36 to 40 in d0e40c3
An easy solution could be just adding the
else
condition to raise an error saying the type isn't supportedCrystal version: 1.8.1
Mosquito Shard version:
1.0.0.rc3+git.commit.d0245eba8514e238c253267b0bdf7663c6b01590
The text was updated successfully, but these errors were encountered: