Add common function to parse python list

Add the common function to parse python list representation to a puppet
array value, to replace the same implementation maintained in a few
modules.

Change-Id: I4ec8a47fe865796e7c6cdebe8faafb0b8504e074
This commit is contained in:
Takashi Kajinami 2024-10-08 17:32:19 +09:00
parent 93d082e579
commit 762978b0df
2 changed files with 17 additions and 0 deletions

View File

@ -163,4 +163,8 @@ class Puppet::Provider::Openstack < Puppet::Provider
def self.parse_python_dict(text)
return JSON.parse(text.gsub(/'/, '"').gsub(/: False([,}])/,': false\1').gsub(/: True([,}])/,': true\1'))
end
def self.parse_python_list(text)
return JSON.parse(text.gsub(/'/, '"'))
end
end

View File

@ -229,4 +229,17 @@ name="test"
expect(Puppet::Provider::Openstack.parse_python_dict(s)).to eq({'key'=>'value', 'key2'=>false})
end
end
describe '#parse_python_list' do
it 'should return an array when provided with a python list' do
s = "['foo', 'bar', 'baz']"
expect(Puppet::Provider::Openstack.parse_python_list(s)).to eq(['foo', 'bar', 'baz'])
s = '[]'
expect(Puppet::Provider::Openstack.parse_python_list(s)).to eq([])
s = '[1, 2, 3]'
expect(Puppet::Provider::Openstack.parse_python_list(s)).to eq([1, 2, 3])
end
end
end