Commit b7e51d1c authored by Stan Hu's avatar Stan Hu

Merge branch '32490-improve-performance-of-gitlab-utils-inlinehash' into 'master'

Resolve "Improve performance of Gitlab::Utils::InlineHash"

See merge request gitlab-org/gitlab!17824
parents 6e1f17da 5e47b9a9
......@@ -11,13 +11,15 @@ module Gitlab
#
# {
# 'root_param' => 'Root',
# 12 => 'number',
# symbol: 'symbol',
# nested_param: {
# key: 'Value'
# },
# 'very' => {
# 'deep' => {
# 'nested' => {
# 'param' => 'Deep nested value'
# 12 => 'Deep nested value'
# }
# }
# }
......@@ -28,14 +30,21 @@ module Gitlab
#
# {
# 'root_param' => 'Root',
# 12 => 'number',
# symbol: symbol,
# 'nested_param.key' => 'Value',
# 'very.deep.nested.param' => 'Deep nested value'
# 'very.deep.nested.12' => 'Deep nested value'
# }
#
def merge_keys(hash, prefix: nil, connector: '.')
result = {}
base_prefix = prefix ? "#{prefix}#{connector}" : ''
pairs = hash.map { |key, value| ["#{base_prefix}#{key}", value] }
pairs =
if prefix
base_prefix = "#{prefix}#{connector}"
hash.map { |key, value| ["#{base_prefix}#{key}", value] }
else
hash.to_a
end
until pairs.empty?
key, value = pairs.shift
......
......@@ -9,9 +9,11 @@ describe Gitlab::Utils::InlineHash do
let(:source) do
{
nested_param: {
key: 'Value'
key: :Value
},
'root_param' => 'Root',
unnested_symbol_key: :unnested_symbol_value,
12 => 22,
'very' => {
'deep' => {
'nested' => {
......@@ -24,15 +26,17 @@ describe Gitlab::Utils::InlineHash do
it 'transforms a nested hash into a one-level hash' do
is_expected.to eq(
'nested_param.key' => 'Value',
'nested_param.key' => :Value,
'root_param' => 'Root',
:unnested_symbol_key => :unnested_symbol_value,
12 => 22,
'very.deep.nested.param' => 'Deep nested value'
)
end
it 'retains key insertion order' do
expect(subject.keys)
.to eq(%w(nested_param.key root_param very.deep.nested.param))
.to eq(['nested_param.key', 'root_param', :unnested_symbol_key, 12, 'very.deep.nested.param'])
end
context 'with a custom connector' do
......@@ -40,8 +44,10 @@ describe Gitlab::Utils::InlineHash do
it 'uses the connector to merge keys' do
is_expected.to eq(
'nested_param::key' => 'Value',
'nested_param::key' => :Value,
'root_param' => 'Root',
:unnested_symbol_key => :unnested_symbol_value,
12 => 22,
'very::deep::nested::param' => 'Deep nested value'
)
end
......@@ -52,8 +58,10 @@ describe Gitlab::Utils::InlineHash do
it 'prefixes all the keys' do
is_expected.to eq(
'options.nested_param.key' => 'Value',
'options.nested_param.key' => :Value,
'options.root_param' => 'Root',
'options.unnested_symbol_key' => :unnested_symbol_value,
'options.12' => 22,
'options.very.deep.nested.param' => 'Deep nested value'
)
end
......
......@@ -3,7 +3,7 @@
require 'fast_spec_helper'
describe Gitlab::Utils::SafeInlineHash do
context '.merge_keys!' do
describe '.merge_keys!' do
let(:source) { { 'foo' => { 'bar' => 'baz' } } }
let(:validator) { instance_double(Gitlab::Utils::DeepSize, valid?: valid) }
......
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