Commit 5335b855 authored by allison.browne's avatar allison.browne

Add examples and specs for edge case of InlineHash

Unested symbolic or numeric keys should not be converted to strings.
These changes document that and adds specs reflecting that. Also
makes a minor change to simplify and speed perf in the InlineHash
implementation.
parent 804badac
......@@ -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,15 +30,17 @@ 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 =
if prefix
base_prefix = "#{prefix}#{connector}"
hash.map { |key, value| ["#{base_prefix}#{key}", value] }
else
hash.to_a
......
......@@ -6,6 +6,7 @@ describe Gitlab::Utils::InlineHash do
describe '.merge_keys' do
subject { described_class.merge_keys(source) }
context 'with string keys' do
let(:source) do
{
nested_param: {
......@@ -59,4 +60,40 @@ describe Gitlab::Utils::InlineHash do
end
end
end
context 'with un-nested symbol or numeric keys' do
let(:unested_symbol_key_source) do
{
unnested_symbol_key: :unnested_symbol_value,
12 => 22,
nested_symbol_key: {
nested_symbol_key_2: :nested_symbol_value
}
}
end
context 'without prefix' do
subject { described_class.merge_keys(unested_symbol_key_source) }
it 'converts only nested keys to inline strings' do
is_expected.to eq(
:unnested_symbol_key => :unnested_symbol_value,
12 => 22,
'nested_symbol_key.nested_symbol_key_2' => :nested_symbol_value
)
end
end
context 'with prefix' do
subject { described_class.merge_keys(unested_symbol_key_source, prefix: 'options') }
it 'converts prefixed keys to inline strings' do
is_expected.to eq(
'options.unnested_symbol_key' => :unnested_symbol_value,
'options.12' => 22,
'options.nested_symbol_key.nested_symbol_key_2' => :nested_symbol_value
)
end
end
end
end
end
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