Skip to content

Instantly share code, notes, and snippets.

@aj-jester
Last active January 18, 2018 12:39
Show Gist options
  • Save aj-jester/e0078c38db9eb7c1ef45 to your computer and use it in GitHub Desktop.
Save aj-jester/e0078c38db9eb7c1ef45 to your computer and use it in GitHub Desktop.
Puppet parser function that takes unsorted hash and outputs sorted JSON object.
#
# LICENSE: https://gist.github.com/aj-jester/e0078c38db9eb7c1ef45
#
require 'json'
module JSON
class << self
@@loop = 0
def sorted_generate(obj)
case obj
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|
arrayRet.push(sorted_generate(a))
end
return "[" << arrayRet.join(',') << "]";
when Hash
ret = []
obj.keys.sort.each do |k|
ret.push(k.to_json << ":" << sorted_generate(obj[k]))
end
return "{" << ret.join(",") << "}";
else
raise Exception("Unable to handle object of type <%s>" % obj.class.to_s)
end
end
def sorted_pretty_generate(obj, indent_len=4)
# Indent length
indent = " " * indent_len
case obj
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 = []
# We need to increase the loop count before #each so the objects inside are indented twice.
# When we come out of #each we decrease the loop count so the closing brace lines up properly.
#
# If you start with @@loop = 1, the count will be as follows
#
# "start_join": [ <-- @@loop == 1
# "192.168.50.20", <-- @@loop == 2
# "192.168.50.21", <-- @@loop == 2
# "192.168.50.22" <-- @@loop == 2
# ] <-- closing brace <-- @@loop == 1
#
@@loop += 1
obj.each do |a|
arrayRet.push(sorted_pretty_generate(a, indent_len))
end
@@loop -= 1
return "[\n#{indent * (@@loop + 1)}" << arrayRet.join(",\n#{indent * (@@loop + 1)}") << "\n#{indent * @@loop}]";
when Hash
ret = []
# This loop works in a similar way to the above
@@loop += 1
obj.keys.sort.each do |k|
ret.push("#{indent * @@loop}" << k.to_json << ": " << sorted_pretty_generate(obj[k], indent_len))
end
@@loop -= 1
return "{\n" << ret.join(",\n") << "\n#{indent * @@loop}}";
else
raise Exception("Unable to handle object of type <%s>" % obj.class.to_s)
end
end # end def
end # end class
end # end module
module Puppet::Parser::Functions
newfunction(:sorted_json, :type => :rvalue, :doc => <<-EOS
This function takes unsorted hash and outputs JSON object making sure the keys are sorted.
Optionally you can pass 2 additional parameters, pretty generate and indent length.
*Examples:*
-------------------
-- UNSORTED HASH --
-------------------
unsorted_hash = {
'client_addr' => '127.0.0.1',
'bind_addr' => '192.168.34.56',
'start_join' => [
'192.168.34.60',
'192.168.34.61',
'192.168.34.62',
],
'ports' => {
'rpc' => 8567,
'https' => 8500,
'http' => -1,
},
}
-----------------
-- SORTED JSON --
-----------------
sorted_json(unsorted_hash)
{"bind_addr":"192.168.34.56","client_addr":"127.0.0.1",
"ports":{"http":-1,"https":8500,"rpc":8567},
"start_join":["192.168.34.60","192.168.34.61","192.168.34.62"]}
------------------------
-- PRETTY SORTED JSON --
------------------------
Params: data <hash>, pretty <true|false>, indent <int>.
sorted_json(unsorted_hash, true, 4)
{
"bind_addr": "192.168.34.56",
"client_addr": "127.0.0.1",
"ports": {
"http": -1,
"https": 8500,
"rpc": 8567
},
"start_join": [
"192.168.34.60",
"192.168.34.61",
"192.168.34.62"
]
}
EOS
) do |args|
unsorted_hash = args[0] || {}
pretty = args[1] || false
indent_len = args[2].to_i || 4
unsorted_hash.reject! {|key, value| value == :undef }
if pretty
return JSON.sorted_pretty_generate(unsorted_hash, indent_len) << "\n"
else
return JSON.sorted_generate(unsorted_hash)
end
end
end
#
# LICENSE: https://gist.github.com/aj-jester/e0078c38db9eb7c1ef45
#
require 'spec_helper'
describe 'sorted_json', :type => :puppet_function do
let(:test_hash){ { 'z' => 3, 'a' => '1', 'p' => '2', 's' => '-7' } }
before do
@json = subject.call([test_hash, true])
end
it "sorts keys" do
expect( @json.index('a') ).to be < @json.index('p')
expect( @json.index('p') ).to be < @json.index('s')
expect( @json.index('s') ).to be < @json.index('z')
end
it "prints pretty json" do
expect(@json.split("\n").size).to eql(test_hash.size + 2) # +2 for { and }
end
it "prints ugly json" do
json = subject.call([test_hash]) # pretty=false by default
expect(json.split("\n").size).to eql(1)
end
it "validate ugly json" do
json = subject.call([test_hash]) # pretty=false by default
expect(json).to match("{\"a\":1,\"p\":2,\"s\":-7,\"z\":3}")
end
context 'nesting' do
let(:nested_test_hash){ { 'z' => [{'l' => 3, 'k' => '2', 'j'=> '1'}],
'a' => {'z' => '3', 'x' => '1', 'y' => '2'},
'p' => [ '9','8','7'] } }
before do
@json = subject.call([nested_test_hash, true])
end
it "sorts nested hashes" do
expect( @json.index('x') ).to be < @json.index('y')
expect( @json.index('y') ).to be < @json.index('z')
end
end
end
The MIT License (MIT)
Copyright (c) 2015 aj-jester
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@aj-jester
Copy link
Author

sorted_json

Sorting a hash in Ruby is rather confusing. Add puppet type casting on top and each time the result could be different. Here is a good explanation. This causes puppet to notify services on config changes when the order of the hash keys changes on every puppet run.

  • Sorts deep nested hashes and converts them to JSON
  • Multiple data types supported
  • Integers are placed without quotes (negative ints supported)
  • Generates human readable JSON config
Example

Pass an unsorted hash to the function and returns a sorted json blob.

$sorted_json_config = sorted_json($unsorted_hash)

or use it directly with file's content attribute:

file { '/etc/service.json':
  content => sorted_json($unsorted_hash),
}
Prettier example

It optionally takes boolean as a second parameter to enable human readable config; defaults to false. You can also toggle indentation length using a third parameter; defaults to 4.

$unsorted_hash = {
  'client_addr' => '127.0.0.1',
  'bind_addr'   => '192.168.34.56',
  'start_join'  => [
    '192.168.34.60',
    '192.168.34.61',
    '192.168.34.62',
  ],
  'ports'       => {
    'rpc'   => 8567,
    'https' => 8500,
    'http'  => -1,
  },
}

sorted_json($unsorted_hash, true, 4)
{
    "bind_addr": "192.168.34.56",
    "client_addr": "127.0.0.1",
    "ports": {
        "http": -1,
        "https": 8500,
        "rpc": 8567
    },
    "start_join": [
        "192.168.34.60",
        "192.168.34.61",
        "192.168.34.62"
    ]
}

Authors

https://github.com/aj-jester
https://github.com/halkeye (https://gist.github.com/halkeye/2287885)
https://github.com/falzm (https://gist.github.com/falzm/8575549)
https://github.com/teaforthecat (tests)

@baurmatt
Copy link

newfunction() is Puppet 3 Syntax. Please move to create_function()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment