From be8f59b2792458ab4c89b762c22aff3391c6d289 Mon Sep 17 00:00:00 2001 From: aj-jester Date: Thu, 16 Jul 2015 23:15:35 -0400 Subject: [PATCH] convert quoted integers to int object --- lib/puppet/parser/functions/consul_sorted_json.rb | 5 ++++- spec/functions/consul_sorted_json_spec.rb | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/puppet/parser/functions/consul_sorted_json.rb b/lib/puppet/parser/functions/consul_sorted_json.rb index a43b0570..e31c09a8 100644 --- a/lib/puppet/parser/functions/consul_sorted_json.rb +++ b/lib/puppet/parser/functions/consul_sorted_json.rb @@ -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 + return (obj.match(/\A[-]?[0-9]+\z/) ? obj.to_i : obj).to_json when Array arrayRet = [] obj.each do |a| diff --git a/spec/functions/consul_sorted_json_spec.rb b/spec/functions/consul_sorted_json_spec.rb index 8e841d99..670b662b 100644 --- a/spec/functions/consul_sorted_json_spec.rb +++ b/spec/functions/consul_sorted_json_spec.rb @@ -3,4 +3,11 @@ 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