2017-07-09 13:49:52 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:40:03 -04:00
|
|
|
|
2016-08-06 12:40:07 -04:00
|
|
|
require "active_support/core_ext/hash"
|
2014-12-21 06:57:46 -05:00
|
|
|
|
2014-05-22 13:35:45 -04:00
|
|
|
module ActiveJob
|
2018-02-12 14:16:41 -05:00
|
|
|
# Raised when an exception is raised during job arguments deserialization.
|
|
|
|
#
|
|
|
|
# Wraps the original exception raised as +cause+.
|
|
|
|
class DeserializationError < StandardError
|
|
|
|
def initialize #:nodoc:
|
|
|
|
super("Error while trying to deserialize arguments: #{$!.message}")
|
|
|
|
set_backtrace $!.backtrace
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Raised when an unsupported argument type is set as a job argument. We
|
2018-11-08 18:04:26 -05:00
|
|
|
# currently support String, Integer, Float, NilClass, TrueClass, FalseClass,
|
|
|
|
# BigDecimal, Symbol, Date, Time, DateTime, ActiveSupport::TimeWithZone,
|
|
|
|
# ActiveSupport::Duration, Hash, ActiveSupport::HashWithIndifferentAccess,
|
|
|
|
# Array or GlobalID::Identification instances, although this can be extended
|
|
|
|
# by adding custom serializers.
|
2018-02-12 14:16:41 -05:00
|
|
|
# Raised if you set the key for a Hash something else than a string or
|
|
|
|
# a symbol. Also raised when trying to serialize an object which can't be
|
2018-11-08 18:04:26 -05:00
|
|
|
# identified with a GlobalID - such as an unpersisted Active Record model.
|
2018-02-12 14:16:41 -05:00
|
|
|
class SerializationError < ArgumentError; end
|
|
|
|
|
2014-08-15 06:04:06 -04:00
|
|
|
module Arguments
|
|
|
|
extend self
|
2018-08-27 09:30:05 -04:00
|
|
|
# Serializes a set of arguments. Intrinsic types that can safely be
|
|
|
|
# serialized without mutation are returned as-is. Arrays/Hashes are
|
|
|
|
# serialized element by element. All other types are serialized using
|
|
|
|
# GlobalID.
|
2014-08-15 06:04:06 -04:00
|
|
|
def serialize(arguments)
|
2018-02-12 14:16:41 -05:00
|
|
|
arguments.map { |argument| serialize_argument(argument) }
|
2014-06-05 16:10:41 -04:00
|
|
|
end
|
|
|
|
|
2018-09-13 12:42:39 -04:00
|
|
|
# Deserializes a set of arguments. Intrinsic types that can safely be
|
2018-08-27 09:30:05 -04:00
|
|
|
# deserialized without mutation are returned as-is. Arrays/Hashes are
|
|
|
|
# deserialized element by element. All other types are deserialized using
|
|
|
|
# GlobalID.
|
2014-08-15 06:04:06 -04:00
|
|
|
def deserialize(arguments)
|
2018-02-12 14:16:41 -05:00
|
|
|
arguments.map { |argument| deserialize_argument(argument) }
|
2015-11-03 09:54:34 -05:00
|
|
|
rescue
|
|
|
|
raise DeserializationError
|
2014-06-05 16:10:41 -04:00
|
|
|
end
|
2018-02-12 14:16:41 -05:00
|
|
|
|
|
|
|
private
|
2018-11-12 14:44:59 -05:00
|
|
|
# :nodoc:
|
|
|
|
PERMITTED_TYPES = [ NilClass, String, Integer, Float, BigDecimal, TrueClass, FalseClass ]
|
2018-02-12 14:16:41 -05:00
|
|
|
# :nodoc:
|
2018-02-27 23:33:37 -05:00
|
|
|
GLOBALID_KEY = "_aj_globalid"
|
2018-02-12 14:16:41 -05:00
|
|
|
# :nodoc:
|
2018-02-27 23:33:37 -05:00
|
|
|
SYMBOL_KEYS_KEY = "_aj_symbol_keys"
|
2018-02-12 14:16:41 -05:00
|
|
|
# :nodoc:
|
2020-01-18 21:35:55 -05:00
|
|
|
RUBY2_KEYWORDS_KEY = "_aj_ruby2_keywords"
|
|
|
|
# :nodoc:
|
2018-02-27 23:33:37 -05:00
|
|
|
WITH_INDIFFERENT_ACCESS_KEY = "_aj_hash_with_indifferent_access"
|
2018-02-12 14:16:41 -05:00
|
|
|
# :nodoc:
|
|
|
|
OBJECT_SERIALIZER_KEY = "_aj_serialized"
|
|
|
|
|
|
|
|
# :nodoc:
|
|
|
|
RESERVED_KEYS = [
|
|
|
|
GLOBALID_KEY, GLOBALID_KEY.to_sym,
|
|
|
|
SYMBOL_KEYS_KEY, SYMBOL_KEYS_KEY.to_sym,
|
2020-01-18 21:35:55 -05:00
|
|
|
RUBY2_KEYWORDS_KEY, RUBY2_KEYWORDS_KEY.to_sym,
|
2018-02-12 14:16:41 -05:00
|
|
|
OBJECT_SERIALIZER_KEY, OBJECT_SERIALIZER_KEY.to_sym,
|
|
|
|
WITH_INDIFFERENT_ACCESS_KEY, WITH_INDIFFERENT_ACCESS_KEY.to_sym,
|
|
|
|
]
|
2020-01-18 21:35:55 -05:00
|
|
|
private_constant :PERMITTED_TYPES, :RESERVED_KEYS, :GLOBALID_KEY,
|
|
|
|
:SYMBOL_KEYS_KEY, :RUBY2_KEYWORDS_KEY, :WITH_INDIFFERENT_ACCESS_KEY
|
|
|
|
|
|
|
|
unless Hash.respond_to?(:ruby2_keywords_hash?) && Hash.respond_to?(:ruby2_keywords_hash)
|
|
|
|
using Module.new {
|
|
|
|
refine Hash do
|
|
|
|
class << Hash
|
|
|
|
if RUBY_VERSION >= "2.7"
|
|
|
|
def ruby2_keywords_hash?(hash)
|
|
|
|
!new(*[hash]).default.equal?(hash)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
def ruby2_keywords_hash?(hash)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ruby2_keywords_hash(hash)
|
|
|
|
_ruby2_keywords_hash(**hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
private def _ruby2_keywords_hash(*args)
|
|
|
|
args.last
|
|
|
|
end
|
|
|
|
ruby2_keywords(:_ruby2_keywords_hash) if respond_to?(:ruby2_keywords, true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2018-02-12 14:16:41 -05:00
|
|
|
|
|
|
|
def serialize_argument(argument)
|
|
|
|
case argument
|
2018-08-24 16:16:41 -04:00
|
|
|
when *PERMITTED_TYPES
|
2018-02-12 14:16:41 -05:00
|
|
|
argument
|
|
|
|
when GlobalID::Identification
|
|
|
|
convert_to_global_id_hash(argument)
|
|
|
|
when Array
|
|
|
|
argument.map { |arg| serialize_argument(arg) }
|
|
|
|
when ActiveSupport::HashWithIndifferentAccess
|
2018-10-05 03:26:02 -04:00
|
|
|
serialize_indifferent_hash(argument)
|
2018-02-12 14:16:41 -05:00
|
|
|
when Hash
|
2020-01-18 21:35:55 -05:00
|
|
|
symbol_keys = argument.each_key.grep(Symbol).map!(&:to_s)
|
|
|
|
aj_hash_key = if Hash.ruby2_keywords_hash?(argument)
|
|
|
|
RUBY2_KEYWORDS_KEY
|
|
|
|
else
|
|
|
|
SYMBOL_KEYS_KEY
|
|
|
|
end
|
2018-02-12 14:16:41 -05:00
|
|
|
result = serialize_hash(argument)
|
2020-01-18 21:35:55 -05:00
|
|
|
result[aj_hash_key] = symbol_keys
|
2018-02-12 14:16:41 -05:00
|
|
|
result
|
2018-10-05 03:26:02 -04:00
|
|
|
when -> (arg) { arg.respond_to?(:permitted?) }
|
|
|
|
serialize_indifferent_hash(argument.to_h)
|
2018-02-12 14:16:41 -05:00
|
|
|
else
|
|
|
|
Serializers.serialize(argument)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def deserialize_argument(argument)
|
|
|
|
case argument
|
|
|
|
when String
|
2018-09-05 17:38:09 -04:00
|
|
|
argument
|
2018-08-24 16:16:41 -04:00
|
|
|
when *PERMITTED_TYPES
|
2018-02-12 14:16:41 -05:00
|
|
|
argument
|
|
|
|
when Array
|
|
|
|
argument.map { |arg| deserialize_argument(arg) }
|
|
|
|
when Hash
|
|
|
|
if serialized_global_id?(argument)
|
|
|
|
deserialize_global_id argument
|
|
|
|
elsif custom_serialized?(argument)
|
|
|
|
Serializers.deserialize(argument)
|
|
|
|
else
|
|
|
|
deserialize_hash(argument)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise ArgumentError, "Can only deserialize primitive arguments: #{argument.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialized_global_id?(hash)
|
|
|
|
hash.size == 1 && hash.include?(GLOBALID_KEY)
|
|
|
|
end
|
|
|
|
|
|
|
|
def deserialize_global_id(hash)
|
|
|
|
GlobalID::Locator.locate hash[GLOBALID_KEY]
|
|
|
|
end
|
|
|
|
|
|
|
|
def custom_serialized?(hash)
|
|
|
|
hash.key?(OBJECT_SERIALIZER_KEY)
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialize_hash(argument)
|
|
|
|
argument.each_with_object({}) do |(key, value), hash|
|
|
|
|
hash[serialize_hash_key(key)] = serialize_argument(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def deserialize_hash(serialized_hash)
|
|
|
|
result = serialized_hash.transform_values { |v| deserialize_argument(v) }
|
|
|
|
if result.delete(WITH_INDIFFERENT_ACCESS_KEY)
|
|
|
|
result = result.with_indifferent_access
|
|
|
|
elsif symbol_keys = result.delete(SYMBOL_KEYS_KEY)
|
|
|
|
result = transform_symbol_keys(result, symbol_keys)
|
2020-01-18 21:35:55 -05:00
|
|
|
elsif symbol_keys = result.delete(RUBY2_KEYWORDS_KEY)
|
|
|
|
result = transform_symbol_keys(result, symbol_keys)
|
|
|
|
result = Hash.ruby2_keywords_hash(result)
|
2018-02-12 14:16:41 -05:00
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialize_hash_key(key)
|
|
|
|
case key
|
|
|
|
when *RESERVED_KEYS
|
|
|
|
raise SerializationError.new("Can't serialize a Hash with reserved key #{key.inspect}")
|
|
|
|
when String, Symbol
|
|
|
|
key.to_s
|
|
|
|
else
|
|
|
|
raise SerializationError.new("Only string and symbol hash keys may be serialized as job arguments, but #{key.inspect} is a #{key.class}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-05 03:26:02 -04:00
|
|
|
def serialize_indifferent_hash(indifferent_hash)
|
|
|
|
result = serialize_hash(indifferent_hash)
|
|
|
|
result[WITH_INDIFFERENT_ACCESS_KEY] = serialize_argument(true)
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2018-02-12 14:16:41 -05:00
|
|
|
def transform_symbol_keys(hash, symbol_keys)
|
2018-10-30 15:33:47 -04:00
|
|
|
# NOTE: HashWithIndifferentAccess#transform_keys always
|
|
|
|
# returns stringified keys with indifferent access
|
|
|
|
# so we call #to_h here to ensure keys are symbolized.
|
|
|
|
hash.to_h.transform_keys do |key|
|
2018-02-12 14:16:41 -05:00
|
|
|
if symbol_keys.include?(key)
|
|
|
|
key.to_sym
|
|
|
|
else
|
|
|
|
key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_to_global_id_hash(argument)
|
|
|
|
{ GLOBALID_KEY => argument.to_global_id.to_s }
|
|
|
|
rescue URI::GID::MissingModelIdError
|
|
|
|
raise SerializationError, "Unable to serialize #{argument.class} " \
|
|
|
|
"without an id. (Maybe you forgot to call save?)"
|
|
|
|
end
|
2014-05-22 13:35:45 -04:00
|
|
|
end
|
|
|
|
end
|