Puppet Function: parsehocon

Defined in:
modules/stdlib/lib/puppet/functions/parsehocon.rb
Function type:
Ruby 4.x API

Overview

parsehocon(String $hocon_string, Optional[Any] $default)Any

Examples:

How to parse hocon

$data = parsehocon("{any valid hocon: string}")

Parameters:

  • hocon_string (String)

    A valid HOCON string

  • default (Optional[Any])

    An optional default to return if parsing hocon_string fails

Returns:

  • (Any)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'modules/stdlib/lib/puppet/functions/parsehocon.rb', line 13

Puppet::Functions.create_function(:parsehocon) do
  # @param hocon_string A valid HOCON string
  # @param default An optional default to return if parsing hocon_string fails
  dispatch :parsehocon do
    param          'String', :hocon_string
    optional_param 'Any',    :default
  end

  def parsehocon(hocon_string, default = :no_default_provided)
    require 'hocon/config_factory'

    begin
      data = Hocon::ConfigFactory.parse_string(hocon_string)
      data.resolve.root.unwrapped
    rescue Hocon::ConfigError::ConfigParseError => err
      Puppet.debug("Parsing hocon failed with error: #{err.message}")
      raise err if default == :no_default_provided
      default
    end
  end
end