Puppet Function: sexpr

Defined in:
lib/puppet/parser/functions/sexpr.rb
Function type:
Ruby 3.x API

Overview

sexpr()Any

This converts a nested structure into a string containing an s-expression. It will serialize each element it encounters recursively:

  • String: will be passed as-is

  • Array: will be a clojure vector

  • Hash: will be a clojure map

Example:

The following puppet structure:

[ ‘where’, [‘service’,‘“users/users”’],

['with', {'service' => '"total users"', 'ttl' => 300},
  [ 'coalesce',
    [ 'throttle', 1, 1,
      [ 'smap', 'folds/sum',
        [ 'with', { 'host' => 'nil' }, 'index' ]
      ]
    ]
  ]
]

]

When passed to sexpr will yield the string:

(where

(service "users/users")
(with {:service "total users" :ttl 300}
  (coalesce
    (throttle 1 1
      (smap folds/sum
        (with {:host nil} index))))))

Arguments: $object $indent_level

Returns:

  • (Any)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/puppet/parser/functions/sexpr.rb', line 40

newfunction(:sexpr, :type => :rvalue, :doc => <<-EOS
This converts a nested structure into a string containing an s-expression.
It will serialize each element it encounters recursively:

* String: will be passed as-is
* Array:  will be a clojure vector
* Hash:   will be a clojure map

Example:

The following puppet structure:

[ 'where', ['service','"users/users"'],
['with', {'service' => '"total users"', 'ttl' => 300},
  [ 'coalesce',
    [ 'throttle', 1, 1,
      [ 'smap', 'folds/sum',
        [ 'with', { 'host' => 'nil' }, 'index' ]
      ]
    ]
  ]
]
]

When passed to sexpr will yield the string:

(where
(service "users/users")
(with {:service "total users" :ttl 300}
  (coalesce
    (throttle 1 1
      (smap folds/sum
        (with {:host nil} index))))))

Arguments: $object $indent_level
  EOS
) do |arguments|

  if arguments.empty?
    return []
  end

  indent = 0
  if arguments.length == 2
    indent = arguments[1].to_i
  elsif arguments.length == 1
    indent = 0
  else
    raise(Puppet::Error, "sexpr(): only one argument accepted")
  end
  if arguments[0].kind_of?(String)
    return '  ' * indent + arguments[0] + "\n"
  end
  return _serel(arguments[0],indent) + "\n"
end