Skip to content

Instantly share code, notes, and snippets.

@alassek
Created October 24, 2021 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alassek/4e80c33faa71cd8f3ab19e03362db752 to your computer and use it in GitHub Desktop.
Save alassek/4e80c33faa71cd8f3ab19e03362db752 to your computer and use it in GitHub Desktop.
require "global_id"
module Sidekiq
module GlobalIDMiddleware
class Processor
def call(_worker_class, job, *)
job["args"].map!(&method(:process))
yield
end
def process(argument)
argument
end
end
class Deserializer < Processor
def process(argument)
if argument.to_s.start_with?("gid://")
GlobalID::Locator.locate(argument)
else
argument
end
end
end
class Serializer < Processor
def process(argument)
if argument.respond_to?(:to_global_id)
argument.to_global_id.to_s
else
argument
end
end
end
end
server_middleware do |chain|
chain.prepend GlobalIDMiddleware::Deserializer
end
client_middleware do |chain|
chain.prepend GlobalIDMiddleware::Serializer
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment