File: compare.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 (127 lines) | stat: -rw-r--r-- 3,598 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# frozen_string_literal: true

# Compares two values and returns -1, 0 or 1 if first value is smaller, equal or larger than the second value.
# The compare function accepts arguments of the data types `String`, `Numeric`, `Timespan`, `Timestamp`, and `Semver`,
# such that:
#
# * two of the same data type can be compared
# * `Timespan` and `Timestamp` can be compared with each other and with `Numeric`
#
# When comparing two `String` values the comparison can be made to consider case by passing a third (optional)
# boolean `false` value - the default is `true` which ignores case as the comparison operators
# in the Puppet Language.
#
Puppet::Functions.create_function(:compare) do
  local_types do
    type 'TimeComparable = Variant[Numeric, Timespan, Timestamp]'
    type 'Comparable = Variant[String, Semver, TimeComparable]'
  end

  dispatch :on_numeric do
    param 'Numeric', :a
    param 'Numeric', :b
  end

  dispatch :on_string do
    param 'String', :a
    param 'String', :b
    optional_param 'Boolean', :ignore_case
  end

  dispatch :on_version do
    param 'Semver', :a
    param 'Semver', :b
  end

  dispatch :on_time_num_first do
    param 'Numeric', :a
    param 'Variant[Timespan, Timestamp]', :b
  end

  dispatch :on_timestamp do
    param 'Timestamp', :a
    param 'Variant[Timestamp, Numeric]', :b
  end

  dispatch :on_timespan do
    param 'Timespan', :a
    param 'Variant[Timespan, Numeric]', :b
  end

  argument_mismatch :on_error do
    param 'Comparable', :a
    param 'Comparable', :b
    repeated_param 'Any', :ignore_case
  end

  argument_mismatch :on_not_comparable do
    param 'Any', :a
    param 'Any', :b
    repeated_param 'Any', :ignore_case
  end

  def on_numeric(a, b)
    a <=> b
  end

  def on_string(a, b, ignore_case = true)
    if ignore_case
      a.casecmp(b)
    else
      a <=> b
    end
  end

  def on_version(a, b)
    a <=> b
  end

  def on_time_num_first(a, b)
    # Time data types can compare against Numeric but not the other way around
    # the comparison is therefore done in reverse and the answer is inverted.
    -(b <=> a)
  end

  def on_timespan(a, b)
    a <=> b
  end

  def on_timestamp(a, b)
    a <=> b
  end

  def on_not_comparable(a, b, *ignore_case)
    # TRANSLATORS 'compare' is a name
    _("compare(): Non comparable type. Only values of the types Numeric, String, Semver, Timestamp and Timestamp can be compared. Got %{type_a} and %{type_b}") % {
      type_a: type_label(a), type_b: type_label(b)
    }
  end

  def on_error(a, b, *ignore_case)
    unless ignore_case.empty?
      unless a.is_a?(String) && b.is_a?(String)
        # TRANSLATORS 'compare' is a name
        return _("compare(): The third argument (ignore case) can only be used when comparing strings")
      end
      unless ignore_case.size == 1
        # TRANSLATORS 'compare' is a name
        return _("compare(): Accepts at most 3 arguments, got %{actual_number}") % { actual_number: 2 + ignore_case.size }
      end
      unless ignore_case[0].is_a?(Boolean)
        # TRANSLATORS 'compare' is a name
        return _("compare(): The third argument (ignore case) must be a Boolean. Got %{type}") % { type: type_label(ignore_case[0]) }
      end
    end

    if a.class != b.class
      # TRANSLATORS 'compare' is a name
      _("compare(): Can only compare values of the same type (or for Timestamp/Timespan also against Numeric). Got %{type_a} and %{type_b}") % {
        type_a: type_label(a), type_b: type_label(b)
      }
    end
  end

  def type_label(x)
    Puppet::Pops::Model::ModelLabelProvider.new.label(x)
  end
end