Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle stringified numerics #12

Merged
merged 6 commits into from
May 1, 2015
Merged

Conversation

glarizza
Copy link

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.

# Puppet stringifies numerics in versions of Puppet < 4.0.0
# Account for this in the type
begin
numeric_as_string = Integer(value)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glarizza do we need to account for floats? Would it cause any harm if we were to simply s/Integer/Float/g in your PR?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably wouldn't harm it - I'll make the change. Also updating the provider with mk_resource_methods instead of explicit getter/setter, so I'll push those changes and a test this morning.

@cprice404
Copy link

@fpringvaldsen would you mind reviewing this? And when we hear back from Gary on the float question maybe we can add a commit and another test if he doesn't have time?

Gary Larizza added 2 commits April 30, 2015 08:12
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.

squash
Previously, the changes made only handled Integer values and didn't account for
Float values.  This commit accounts for Floats and adds a test for the same.
@glarizza
Copy link
Author

Please hold off on merging this for three issues:

1.) Using Float() is going to convert integers to floating point numbers with decimal points
2.) 'type' shouldn't be a property, it should be a parameter
3.) This change is turning Floating point numbers into strings

The type property is a property because we need access to its value from
within the `value` property's validate block. If `mk_resource_methods` is
used, syncronization fails - it will constantly try to synchronize the
type property.  This commit changes that behavior.
@glarizza
Copy link
Author

Okay, so here's where we're at:

I'm using Float() to handle floating point values, but it will convert all integer values set in the DSL to a float (i.e. the file will contain 13.0 even if 13 is set in the manifest). The tests don't check for this because 13.0 == 13 in Ruby (though 13.0.eql?(13) is false - so that's the check when/if we differentiate). We COULD add support for integer vs float in the type property to account for this, or just not care that 13.0 is entered into the file (I'd assume the former is better than the latter).

@cprice404
Copy link

Could we try to convert to an int first, and if that fails, try float, and then fall back to string? Then we wouldn't need to add the extra type?

Previously, there was no way to determine whether a string value was intended
to be an integer or a float.  This commit first attempts to cast a string value
as an integer. Failing that, an attempt to cast the string to a float is attempted.
If that fails, and the `type` property is set to 'number', an error is raised.

Tests were added to specifically test each case.
@glarizza
Copy link
Author

Modified the code and added tests. This should meet our needs.

@glarizza
Copy link
Author

Wait, oops, forgot the munge. Please hold...

A previous commit added detection for float/integer string values to the
validation block of the `type` property. This commit adds that same logic
to the munge block to the value is munged to the appropriate value
@glarizza
Copy link
Author

Okay, I noticed that the tests didn't catch my omission on the munge block, and that's because there were no tests for the type. I added some and fixed the munge. NOW we should be good...

@glarizza glarizza force-pushed the fix_numeric branch 2 times, most recently from 41a26e6 to e1d2ff2 Compare April 30, 2015 16:11
provider = described_class.new(resource)
provider.create
expect(provider.exists?).to be true
#expect(provider.value[0]).to eql(12)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should remove this commented line.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and pushed

On Thu, Apr 30, 2015 at 1:20 PM, Preben Ingvaldsen <notifications@github.com

wrote:

In spec/unit/puppet/provider/conf_setting/ruby_spec.rb
#12 (comment)
:

  • it "should be able to handle a float value with number type specified" do
  •  resource = Puppet::Type::Hocon_setting.new(common_params.merge(
    
  •                                                 :setting => 'test_key_1.master', :value => 13.37, :type => 'number'))
    
  •  provider = described_class.new(resource)
    
  •  provider.create
    
  •  expect(provider.exists?).to be true
    
  •  expect(provider.value[0].eql?(13.37)).to be(true)
    
  • end
  • it "should be able to handle an Integer 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)
    

I think we should remove this commented line.


Reply to this email directly or view it on GitHub
https://github.com/puppetlabs/puppetlabs-hocon/pull/12/files#r29449776.

Gary Larizza
Professional Services Engineer
Puppet Labs

http://garylarizza.com

Previously, there were no spec tests to determine whether type validation and
munging was successful.  This commit adds them.  One test is commented out
because it is failing - because :array_matching is set to :all, EVERY
string/array/boolean/number value comes wrapped in an array. The validation
doesn't account for this currently, and a later commit will fix it.
@MSLilah
Copy link
Contributor

MSLilah commented Apr 30, 2015

@glarizza Thanks for this! I'm 👍 on this PR, everything looks good to me.

The last thing I think we need to do on this is add some information on the type parameter in the README to indicate that the parameter will need to explicitly be set to 'number' if a numerical value is passed in when a version of Puppet < 4.0.0 is used, but that can go into a subsequent PR.

@cprice404 or @jpinsonault could one of you give this a review?

cprice404 added a commit that referenced this pull request May 1, 2015
Handle stringified numerics
@cprice404 cprice404 merged commit d7ca17c into puppetlabs:master May 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants