Sha256: 4fe9d3d016f26be18b42910218e0db6fd3069ac7f105b5615387ec1585106336

Contents?: true

Size: 1.77 KB

Versions: 25

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module Bolt
  class PAL
    class YamlPlan
      class Parameter
        attr_reader :name, :value, :type_expr

        PARAMETER_KEYS = Set['type', 'default', 'description']

        def initialize(param, definition)
          definition ||= {}
          validate_param(param, definition)

          @name = param
          @value = definition['default']
          @type_expr = Puppet::Pops::Types::TypeParser.singleton.parse(definition['type']) if definition['type']
        end

        def validate_param(param, definition)
          unless param.is_a?(String) && param.match?(Bolt::PAL::YamlPlan::VAR_NAME_PATTERN)
            raise Bolt::Error.new("Invalid parameter name #{param.inspect}", "bolt/invalid-plan")
          end

          definition_keys = definition.keys.to_set
          unless PARAMETER_KEYS.superset?(definition_keys)
            invalid_keys = definition_keys - PARAMETER_KEYS
            raise Bolt::Error.new("Plan parameter #{param.inspect} contains illegal key(s)" \
                                  " #{invalid_keys.to_a.inspect}",
                                  "bolt/invalid-plan")
          end
        end

        def captures_rest
          false
        end

        def transpile
          result = String.new
          result << "\n\s\s"

          # Param type
          if @type_expr.respond_to?(:type_string)
            result << @type_expr.type_string + " "
          elsif !@type_expr.nil?
            result << @type_expr.to_s + " "
          end

          # Param name
          result << "$#{@name}"

          # Param default
          if @value
            default = @type_expr.to_s =~ /String/ ? "'#{@value}'" : @value
            result << " = #{default}"
          end
          result
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
bolt-1.39.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.38.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.37.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.36.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.35.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.34.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.33.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.32.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.31.1 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.31.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.30.1 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.30.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.29.1 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.29.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.28.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.27.1 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.27.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.26.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.25.0 lib/bolt/pal/yaml_plan/parameter.rb
bolt-1.24.0 lib/bolt/pal/yaml_plan/parameter.rb