Puppet Function: dcos::sorted_json

Defined in:
lib/puppet/functions/dcos/sorted_json.rb
Function type:
Ruby 4.x API

Summary

This function takes data, outputs making sure the hash keys are sorted

Overview

dcos::sorted_json(Hash $json)Data type

Examples:

sorted_json({"foo"=>"value","bar"=>"value"})

Would return: “bar”=>“value”,“foo”=>“value”

Parameters:

  • arguments

    The original array of arguments. Port this to individually managed params to get the full benefit of the modern function API.

  • json (Hash)

Returns:

  • (Data type)

    Describe what the function returns here



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/puppet/functions/dcos/sorted_json.rb', line 19

Puppet::Functions.create_function(:'dcos::sorted_json') do
  # @param arguments
  #   The original array of arguments. Port this to individually managed params
  #   to get the full benefit of the modern function API.
  #
  # @return [Data type]
  #   Describe what the function returns here
  #
  dispatch :default_impl do
    required_param 'Hash', :json
  end

  def sorted_generate(obj)
    case obj
    when Array
      array_ret = []
      obj.each do |a|
        array_ret.push(sorted_generate(a))
      end
      '[' << array_ret.join(',') << ']'
    when Hash
      ret = []
      obj.keys.sort.each do |k|
        ret.push(k.to_json << ":#{sorted_generate(obj[k])}")
      end
      '{' << ret.join(',') << '}'
    when String
      obj.to_json
    else
      obj.to_json
    end
  end # end def

  def sorted_json(h)
    sorted_generate(h)
  end

  def default_impl(json)
    sorted_json(json)
  end
end