Skip to content

Commit

Permalink
Handle stringified numerics
Browse files Browse the repository at this point in the history
Previously, the type would check the type of value being passed in the DSL
and would use that type when setting the value in the Hocon config file
(i.e. a string value would be a string in the config file, and an array
would be an array value). The problem is that versions of Puppet < 4.0.0
stringify all values passed in the DSL, so it was impossible to pass a true
numeric value via the DSL (since it would be stringified).

This commit updates the type validation to account for this and implements
a munge block to convert the value type to a numeric. A test for this case
has also been added.
  • Loading branch information
Gary Larizza committed Apr 29, 2015
1 parent fed641a commit 248aedc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/puppet/provider/hocon_setting/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def value
val
end

def type
@resource[:type]
end

def type=(value)
@resource[:type] = value
end

def value=(new_value)
conf_file_modified = set_value(new_value)
Puppet::Util::ConfigSaver.save(resource[:path], conf_file_modified)
Expand Down
16 changes: 15 additions & 1 deletion lib/puppet/type/hocon_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@
raise "Type specified as #{@resource[:type]} but was #{value.class}"
end
when 'number'
unless value.is_a?(Numeric)
# Puppet stringifies numerics in versions of Puppet < 4.0.0
# Account for this in the type
begin
numeric_as_string = Integer(value)
rescue ArgumentError
numeric_as_string = false
end
unless (value.is_a?(Numeric) or numeric_as_string)
raise "Type specified as 'number' but was #{value.class}"
end
when 'array'
Expand All @@ -61,6 +68,13 @@
raise "Type was specified as #{@resource[:type]}, but should have been one of 'boolean', 'string', 'text', 'number', 'array', or 'hash'"
end
end

munge do |value|
if value.is_a?(String) and @resource[:type] == 'number'
value = Integer(value)
end
value
end
end

validate do
Expand Down
9 changes: 9 additions & 0 deletions spec/unit/puppet/provider/conf_setting/ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,15 @@ def validate_file(expected_content,tmpfile = tmpfile)
expect(provider.value[0]).to eql(12)
end

it "should be able to handle a numerical string value with number type specified" do
resource = Puppet::Type::Hocon_setting.new(common_params.merge(
:setting => 'test_key_1.master', :value => '12', :type => 'number'))
provider = described_class.new(resource)
provider.create
expect(provider.exists?).to be true
expect(provider.value[0]).to eql(12)
end

it "should be able to handle string value with string type specified" do
resource = Puppet::Type::Hocon_setting.new(common_params.merge(
:setting => 'test_key_1.master', :value => "abc", :type => 'string'))
Expand Down

0 comments on commit 248aedc

Please sign in to comment.