Puppet Function: rspamd::ucl::print_config_value

Defined in:
vendor_modules/rspamd/functions/ucl/print_config_value.pp
Function type:
Puppet Language

Summary

returns a properly quoted UCL config value

Overview

rspamd::ucl::print_config_value(Any $value, Rspamd::Ucl::ValueType $type = 'auto')Any

Function: rspamd::ucl::print_config_value()

Parameters:

  • value (Any)

    the value to be printed

  • type (Rspamd::Ucl::ValueType) (defaults to: 'auto')

    the type to be enforced (or `auto` to detect from value)

Returns:

  • (Any)

    the formatted config value suitable for inclusion in a ucl config file

See Also:

  • puppet_classes::rspamdpuppet_classes::rspamd::config

Author:

  • Bernhard Frauendienst <puppet@nospam.obeliks.de>



14
15
16
17
18
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
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
# File 'vendor_modules/rspamd/functions/ucl/print_config_value.pp', line 14

function rspamd::ucl::print_config_value($value, Rspamd::Ucl::ValueType $type = 'auto') {
  $re_number = /\A(\d+(\.\d+)?([kKmMgG]b?|s|min|d|w|y)?|0x[0-9A-F]+)\z/
  $re_boolean = /\A(true|false|on|off|yes|no)\z/
  $target_type = $type ? {
    'auto' => $value ? {
      $re_number => 'number',
      Numeric => 'number',
      $re_boolean => 'boolean',
      Boolean => 'boolean',
      Array => 'array',
      default => 'string',
    },
    default => $type,
  }

  case $target_type {
    'number': {
      case $value {
        $re_number, Numeric: {
          String($value)
        }
        default: {
          fail("Cannot convert ${value} to numeric UCL value.")
        }
      }
    }
    'boolean': {
      case $value {
        $re_boolean, Boolean: {
          String($value)
        }
        default: {
          fail("Cannot convert ${value} to boolean UCL value.")
        }
      }
    }
    'string': {
      case $value {
        /\n/: {
          $eod = $value ? {
            /^EOD$/ => "EOD${fqdn_rand(1000000)}",
            default => 'EOD',
          }
          "<<${eod}\n${value}\n${eod}\n"
        }
        $re_number, $re_boolean: {
          # Make sure strings that look like numbers and booleans aren't printed verbatim
          rspamd::ucl::quote_string_value($value)
        }
        /\A[A-Za-z0-9]+\z/: {
          $value
        }
        String: {
          rspamd::ucl::quote_string_value($value)
        }
        default: {
          # for everything else, convert to string, and then quote it
          rspamd::ucl::quote_string_value(String($value))
        }
      }
    }
    'array': {
      $_values = any2array($value)
      $_joined_values = join($_values.map|$v| { rspamd::ucl::print_config_value($v) }, ', ')
      "[ ${_joined_values} ]"
    }
    default: {
      fail("Invalid value type ${target_type}. This should not happen.")
    }
  }
}