File: empty.rb

package info (click to toggle)
puppet-agent 8.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,404 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (87 lines) | stat: -rw-r--r-- 2,076 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
85
86
87
# frozen_string_literal: true

# Returns `true` if the given argument is an empty collection of values.
#
# This function can answer if one of the following is empty:
# * `Array`, `Hash` - having zero entries
# * `String`, `Binary` - having zero length
#
# For backwards compatibility with the stdlib function with the same name the
# following data types are also accepted by the function instead of raising an error.
# Using these is deprecated and will raise a warning:
#
# * `Numeric` - `false` is returned for all `Numeric` values.
# * `Undef` - `true` is returned for all `Undef` values.
#
# @example Using `empty`
#
# ```puppet
# notice([].empty)
# notice(empty([]))
# # would both notice 'true'
# ```
#
# @since Puppet 5.5.0 - support for Binary
#
Puppet::Functions.create_function(:empty) do
  dispatch :collection_empty do
    param 'Collection', :coll
  end

  dispatch :sensitive_string_empty do
    param 'Sensitive[String]', :str
  end

  dispatch :string_empty do
    param 'String', :str
  end

  dispatch :numeric_empty do
    param 'Numeric', :num
  end

  dispatch :binary_empty do
    param 'Binary', :bin
  end

  dispatch :undef_empty do
    param 'Undef', :x
  end

  def collection_empty(coll)
    coll.empty?
  end

  def sensitive_string_empty(str)
    str.unwrap.empty?
  end

  def string_empty(str)
    str.empty?
  end

  # For compatibility reasons - return false rather than error on floats and integers
  # (Yes, it is strange)
  #
  def numeric_empty(num)
    deprecation_warning_for('Numeric')
    false
  end

  def binary_empty(bin)
    bin.length == 0
  end

  # For compatibility reasons - return true rather than error on undef
  # (Yes, it is strange, but undef was passed as empty string in 3.x API)
  #
  def undef_empty(x)
    true
  end

  def deprecation_warning_for(arg_type)
    file, line = Puppet::Pops::PuppetStack.top_of_stack
    msg = _("Calling function empty() with %{arg_type} value is deprecated.") % { arg_type: arg_type }
    Puppet.warn_once('deprecations', "empty-from-#{file}-#{line}", msg, file, line)
  end
end