From ea3b00dd91f458d7b1b0903a291406e283d54520 Mon Sep 17 00:00:00 2001 From: fabio-gallonetto <55149722+fabio-gallonetto@users.noreply.github.com> Date: Wed, 17 Jun 2020 15:50:50 +0100 Subject: [PATCH 1/5] Add support for nested arrays on ObjectifiedHash At the moment ObjectifiedHash is missing out on hierarchies of objects where there are hashes contained within an array. Which is annoying as it happens in a lot of API calls. This just adds an extra step to the initialiser of ObjectifiedHash so that arrays of hashes are handled properly --- lib/gitlab/objectified_hash.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/gitlab/objectified_hash.rb b/lib/gitlab/objectified_hash.rb index 993fea8e5..bee5f0801 100644 --- a/lib/gitlab/objectified_hash.rb +++ b/lib/gitlab/objectified_hash.rb @@ -8,6 +8,7 @@ def initialize(hash) @hash = hash @data = hash.each_with_object({}) do |(key, value), data| value = self.class.new(value) if value.is_a? Hash + value = value.map { |v| self.class.new(v) } if value.is_a? Array data[key.to_s] = value end end From 2d6edb5db7e9edbd36c5e3ce20ce190f8807b1f1 Mon Sep 17 00:00:00 2001 From: Fabio Gallonetto Date: Thu, 25 Jun 2020 17:38:20 +0100 Subject: [PATCH 2/5] Add tests --- lib/gitlab/objectified_hash.rb | 2 +- spec/gitlab/objectified_hash_spec.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/objectified_hash.rb b/lib/gitlab/objectified_hash.rb index bee5f0801..873df55eb 100644 --- a/lib/gitlab/objectified_hash.rb +++ b/lib/gitlab/objectified_hash.rb @@ -8,7 +8,7 @@ def initialize(hash) @hash = hash @data = hash.each_with_object({}) do |(key, value), data| value = self.class.new(value) if value.is_a? Hash - value = value.map { |v| self.class.new(v) } if value.is_a? Array + value = value.map { |v| v.is_a?(Hash) ? self.class.new(v) : v} if value.is_a? Array data[key.to_s] = value end end diff --git a/spec/gitlab/objectified_hash_spec.rb b/spec/gitlab/objectified_hash_spec.rb index 1808a4f2f..dd1d91206 100644 --- a/spec/gitlab/objectified_hash_spec.rb +++ b/spec/gitlab/objectified_hash_spec.rb @@ -4,7 +4,7 @@ describe Gitlab::ObjectifiedHash do before do - @hash = { a: 1, b: 2, 'string' => 'string', symbol: :symbol } + @hash = { a: 1, b: 2, 'string' => 'string', symbol: :symbol, array: ["string", {:a => 1, :b => 2}] } @oh = described_class.new @hash end @@ -13,6 +13,12 @@ expect(@oh.b).to eq(@hash[:b]) end + it 'objectifies a hash contained in an array' do + expect(@oh.array[1].a).to eq(@hash[:array][1][:a]) + expect(@oh.array[1].b).to eq(@hash[:array][1][:b]) + expect(@oh.array[0]).to eq(@hash[:array][0]) + end + describe '#to_hash' do it 'returns an original hash' do expect(@oh.to_hash).to eq(@hash) From 4e26f462af20a2a00d5faf594f4351f4fea990b4 Mon Sep 17 00:00:00 2001 From: Fabio Gallonetto Date: Tue, 30 Jun 2020 10:32:24 +0100 Subject: [PATCH 3/5] Add support for old and new style sub-hash --- lib/gitlab/objectified_hash.rb | 4 ++++ spec/gitlab/objectified_hash_spec.rb | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/gitlab/objectified_hash.rb b/lib/gitlab/objectified_hash.rb index 873df55eb..ae0fbec4d 100644 --- a/lib/gitlab/objectified_hash.rb +++ b/lib/gitlab/objectified_hash.rb @@ -24,6 +24,10 @@ def inspect "#<#{self.class}:#{object_id} {hash: #{hash.inspect}}" end + def [](key) + @data[key] + end + private attr_reader :hash, :data diff --git a/spec/gitlab/objectified_hash_spec.rb b/spec/gitlab/objectified_hash_spec.rb index dd1d91206..425e575b8 100644 --- a/spec/gitlab/objectified_hash_spec.rb +++ b/spec/gitlab/objectified_hash_spec.rb @@ -4,7 +4,7 @@ describe Gitlab::ObjectifiedHash do before do - @hash = { a: 1, b: 2, 'string' => 'string', symbol: :symbol, array: ["string", {:a => 1, :b => 2}] } + @hash = { a: 1, b: 2, 'string' => 'string', symbol: :symbol, array: [ "string", {:a => 1, :b => 2} ] } @oh = described_class.new @hash end @@ -19,6 +19,11 @@ expect(@oh.array[0]).to eq(@hash[:array][0]) end + it 'supports legacy addressing mode' do + expect(@oh['a']).to eq(@hash[:a]) + expect(@oh['b']).to eq(@hash[:b]) + end + describe '#to_hash' do it 'returns an original hash' do expect(@oh.to_hash).to eq(@hash) From 4d5cd591088ae6249ec3a40be32e5c53edb84254 Mon Sep 17 00:00:00 2001 From: Fabio Gallonetto Date: Tue, 30 Jun 2020 10:42:53 +0100 Subject: [PATCH 4/5] Fix linting errors --- lib/gitlab/objectified_hash.rb | 2 +- spec/gitlab/objectified_hash_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/objectified_hash.rb b/lib/gitlab/objectified_hash.rb index ae0fbec4d..e2aed31fb 100644 --- a/lib/gitlab/objectified_hash.rb +++ b/lib/gitlab/objectified_hash.rb @@ -8,7 +8,7 @@ def initialize(hash) @hash = hash @data = hash.each_with_object({}) do |(key, value), data| value = self.class.new(value) if value.is_a? Hash - value = value.map { |v| v.is_a?(Hash) ? self.class.new(v) : v} if value.is_a? Array + value = value.map { |v| v.is_a?(Hash) ? self.class.new(v) : v } if value.is_a? Array data[key.to_s] = value end end diff --git a/spec/gitlab/objectified_hash_spec.rb b/spec/gitlab/objectified_hash_spec.rb index 425e575b8..6b6d19d7c 100644 --- a/spec/gitlab/objectified_hash_spec.rb +++ b/spec/gitlab/objectified_hash_spec.rb @@ -4,7 +4,7 @@ describe Gitlab::ObjectifiedHash do before do - @hash = { a: 1, b: 2, 'string' => 'string', symbol: :symbol, array: [ "string", {:a => 1, :b => 2} ] } + @hash = { a: 1, b: 2, 'string' => 'string', symbol: :symbol, array: ['string', { a: 1, b: 2 }] } @oh = described_class.new @hash end From 231985c687230f64a3934d0daaa6b6063e3f4602 Mon Sep 17 00:00:00 2001 From: fabio-gallonetto <55149722+fabio-gallonetto@users.noreply.github.com> Date: Wed, 1 Jul 2020 09:37:02 +0100 Subject: [PATCH 5/5] Accepting PR suggestion Co-authored-by: Nihad Abbasov --- lib/gitlab/objectified_hash.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gitlab/objectified_hash.rb b/lib/gitlab/objectified_hash.rb index e2aed31fb..2b70632c6 100644 --- a/lib/gitlab/objectified_hash.rb +++ b/lib/gitlab/objectified_hash.rb @@ -25,7 +25,7 @@ def inspect end def [](key) - @data[key] + data[key] end private