Commit 44c3fb6e authored by Ahmad Sherif's avatar Ahmad Sherif

Add an attributes bag class as a GitalyClient helper

parent f31fb4cd
module Gitlab
module GitalyClient
# This module expects an `ATTRS` const to be defined on the subclass
# See GitalyClient::WikiFile for an example
module AttributesBag
extend ActiveSupport::Concern
included do
attr_accessor(*const_get(:ATTRS))
end
def initialize(params)
params = params.with_indifferent_access
attributes.each do |attr|
instance_variable_set("@#{attr}", params[attr])
end
end
def ==(other)
attributes.all? do |field|
instance_variable_get("@#{field}") == other.instance_variable_get("@#{field}")
end
end
def attributes
self.class.const_get(:ATTRS)
end
end
end
end
module Gitlab module Gitlab
module GitalyClient module GitalyClient
class Diff class Diff
FIELDS = %i(from_path to_path old_mode new_mode from_id to_id patch overflow_marker collapsed).freeze ATTRS = %i(from_path to_path old_mode new_mode from_id to_id patch overflow_marker collapsed).freeze
attr_accessor(*FIELDS) include AttributesBag
def initialize(params)
params.each do |key, val|
public_send(:"#{key}=", val) # rubocop:disable GitlabSecurity/PublicSend
end
end
def ==(other)
FIELDS.all? do |field|
public_send(field) == other.public_send(field) # rubocop:disable GitlabSecurity/PublicSend
end
end
end end
end end
end end
...@@ -12,7 +12,7 @@ module Gitlab ...@@ -12,7 +12,7 @@ module Gitlab
@rpc_response.each do |diff_msg| @rpc_response.each do |diff_msg|
if current_diff.nil? if current_diff.nil?
diff_params = diff_msg.to_h.slice(*GitalyClient::Diff::FIELDS) diff_params = diff_msg.to_h.slice(*GitalyClient::Diff::ATTRS)
# gRPC uses frozen strings by default, and we need to have an unfrozen string as it # gRPC uses frozen strings by default, and we need to have an unfrozen string as it
# gets processed further down the line. So we unfreeze the first chunk of the patch # gets processed further down the line. So we unfreeze the first chunk of the patch
# in case it's the only chunk we receive for this diff. # in case it's the only chunk we receive for this diff.
......
module Gitlab module Gitlab
module GitalyClient module GitalyClient
class WikiFile class WikiFile
FIELDS = %i(name mime_type path raw_data).freeze ATTRS = %i(name mime_type path raw_data).freeze
attr_accessor(*FIELDS) include AttributesBag
def initialize(params)
params = params.with_indifferent_access
FIELDS.each do |field|
instance_variable_set("@#{field}", params[field])
end
end
end end
end end
end end
module Gitlab module Gitlab
module GitalyClient module GitalyClient
class WikiPage class WikiPage
FIELDS = %i(title format url_path path name historical raw_data).freeze ATTRS = %i(title format url_path path name historical raw_data).freeze
attr_accessor(*FIELDS) include AttributesBag
def initialize(params) def initialize(params)
params = params.with_indifferent_access super
FIELDS.each do |field|
instance_variable_set("@#{field}", params[field])
end
# All gRPC strings in a response are frozen, so we get an unfrozen # All gRPC strings in a response are frozen, so we get an unfrozen
# version here so appending to `raw_data` doesn't blow up. # version here so appending to `raw_data` doesn't blow up.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment