Welcome to mirror list, hosted at ThFree Co, Russian Federation.

functions_stack.rb « interpolation « config « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 951d1121d4f130a1a25ef144380d2a0268d99501 (plain)
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
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module Interpolation
        ##
        # This class matches the given function string with a predefined
        # function and then applies it to the input value.
        #
        class FunctionsStack
          Output = Struct.new(:value, :errors) do
            def success?
              errors.empty?
            end
          end

          FUNCTIONS = [
            Functions::Truncate
          ].freeze

          attr_reader :errors

          def initialize(function_expressions)
            @errors = []
            @functions = build_stack(function_expressions)
          end

          def valid?
            errors.none?
          end

          def evaluate(input_value)
            return Output.new(nil, errors) unless valid?

            functions.reduce(Output.new(input_value, [])) do |output, function|
              break output unless output.success?

              output_value = function.execute(output.value)

              if function.valid?
                Output.new(output_value, [])
              else
                Output.new(nil, function.errors)
              end
            end
          end

          private

          attr_reader :functions

          def build_stack(function_expressions)
            function_expressions.map do |function_expression|
              matching_function = FUNCTIONS.find { |function| function.matches?(function_expression) }

              if matching_function.present?
                matching_function.new(function_expression)
              else
                message = "no function matching `#{function_expression}`: " \
                          'check that the function name, arguments, and types are correct'

                errors << message
              end
            end
          end
        end
      end
    end
  end
end