Skip to content

Commit

Permalink
convert quoted integers to int object
Browse files Browse the repository at this point in the history
  • Loading branch information
aj-jester committed Jul 17, 2015
1 parent 297bab6 commit 0bfa3ee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/puppet/parser/functions/consul_sorted_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

def sorted_json(obj)
case obj
when String, Fixnum, Float, TrueClass, FalseClass, NilClass
when Fixnum, Float, TrueClass, FalseClass, NilClass
return obj.to_json
when String
# Convert quoted integers (string) to int
(obj.match(/\A[-]?[0-9]+\z/) ? obj.to_i : obj).to_json
when Array
arrayRet = []
obj.each do |a|
Expand Down
8 changes: 8 additions & 0 deletions spec/functions/consul_sorted_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@
describe 'consul_sorted_json' do
it { should run.with_params({'foo' => :undef}).and_return("{}") }
it { should run.with_params({'b' => 1, 'a' => 2, 'c' => 3}).and_return('{"a":2,"b":1,"c":3}')}
it { should run.with_params({
'w' => -1,
'x' => '8500',
'y' => '-8656',
'z' => 'foo bar 123 4 5 6',
})
.and_return('{"w":-1,"x":8500,"y":-8656,"z":"foo bar 123 4 5 6"}')
}
end

0 comments on commit 0bfa3ee

Please sign in to comment.