Created
October 24, 2021 00:37
-
-
Save alassek/4e80c33faa71cd8f3ab19e03362db752 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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