Skip to content
Browse files

fixed: hash keys were always serialized as strings even if were symbols

now serialization only replaces "special values" (with sidekiq_serialize method) inside the original value (array, hash) and then converts the entire things to_yaml
  • Loading branch information...
1 parent 170fc54 commit a20fa9945516a3c850e77e3a7396130eb5bd0edc @elado committed Aug 20, 2012
Showing with 35 additions and 25 deletions.
  1. +1 −5 .travis.yml
  2. +25 −16 lib/sidekiq/extensions/args_serializer.rb
  3. +9 −4 test/test_args_serializer.rb
View
6 .travis.yml
@@ -2,14 +2,10 @@ language: ruby
rvm:
- 1.9.3
- jruby-19mode
-branches:
- only:
- - master
notifications:
- irc: "irc.freenode.org#sidekiq"
email:
recipients:
- - sidekiq@librelist.org
+ - elado7@gmail.com
matrix:
allow_failures:
- rvm: jruby-19mode
View
41 lib/sidekiq/extensions/args_serializer.rb
@@ -4,36 +4,45 @@ class ArgsSerializer
SIDEKIQ_CUSTOM_SERIALIZATION_FORMAT = /\ASIDEKIQ@(.+)/
def self.serialize(obj)
+ serialize_value(obj).to_yaml
+ end
+
+ def self.deserialize(str)
+ deserialize_value(YAML.load(str))
+ end
+
+ def self.serialize_message(target, method_name, *args)
+ [ serialize(target), method_name, serialize(args) ]
+ end
+
+ def self.deserialize_message(*msg)
+ [ deserialize(msg[0]), msg[1], deserialize(msg[2]) ]
+ end
+
+ private
+ def self.serialize_value(obj)
if obj.respond_to?(:sidekiq_serialize)
obj.sidekiq_serialize
else
case obj
- when Array then obj.map { |o| serialize(o) }
- when Hash then obj.inject({}) { |memo, (k, v)| memo[k] = serialize(v); memo }
- else obj.to_yaml
+ when Array then obj.map { |o| serialize_value(o) }
+ when Hash then obj.inject({}) { |memo, (k, v)| memo[k] = serialize_value(v); memo }
+ else obj
end
end
end
-
- def self.deserialize(obj)
+
+ def self.deserialize_value(obj)
case obj
when SIDEKIQ_CUSTOM_SERIALIZATION_FORMAT
klass_name, args = $1.split('@')
klass = klass_name.constantize
klass.respond_to?(:sidekiq_deserialize) && args ? klass.sidekiq_deserialize(args) : klass
- when Array then obj.map { |item| deserialize(item) }
- when Hash then obj.inject({}) { |memo, (k, v)| memo[k] = deserialize(v); memo }
- else YAML.load(obj)
+ when Array then obj.map { |item| deserialize_value(item) }
+ when Hash then obj.inject({}) { |memo, (k, v)| memo[k] = deserialize_value(v); memo }
+ else obj
end
end
-
- def self.serialize_message(target, method_name, *args)
- [ serialize(target), method_name, serialize(args) ]
- end
-
- def self.deserialize_message(*msg)
- [ deserialize(msg[0]), msg[1], deserialize(msg[2]) ]
- end
end
end
end
View
13 test/test_args_serializer.rb
@@ -20,17 +20,22 @@ def deser(s)
Sidekiq::Extensions::ArgsSerializer.deserialize(s)
end
+ it 'serializes a hash' do
+ hash = { sym: "value", "string" => "value 2", class: User }
+ assert_equal hash, deser(ser(hash))
+ end
+
class User < ActiveRecord::Base
end
it 'serializes active record class' do
- assert_equal "SIDEKIQ@TestArgsSerializer::User", ser(User)
+ assert_match /SIDEKIQ@TestArgsSerializer::User/, ser(User)
assert_equal TestArgsSerializer::User, deser(ser(User))
end
it 'serializes active record instance' do
user = User.create!
- assert_equal "SIDEKIQ@TestArgsSerializer::User@#{user.id}", ser(user)
+ assert_match /SIDEKIQ@TestArgsSerializer::User@#{user.id}/, ser(user)
assert_equal user, deser(ser(user))
end
@@ -41,12 +46,12 @@ module SomeModule
end
it 'serializes class' do
- assert_equal "SIDEKIQ@TestArgsSerializer::SomeClass", ser(SomeClass)
+ assert_match /SIDEKIQ@TestArgsSerializer::SomeClass/, ser(SomeClass)
assert_equal SomeClass, deser(ser(SomeClass))
end
it 'serializes module' do
- assert_equal "SIDEKIQ@TestArgsSerializer::SomeModule", ser(SomeModule)
+ assert_match /SIDEKIQ@TestArgsSerializer::SomeModule/, ser(SomeModule)
assert_equal SomeModule, deser(ser(SomeModule))
end

0 comments on commit a20fa99

Please sign in to comment.
Something went wrong with that request. Please try again.