Sha256: 174ae3714f8ad3adb6f9ae565d71a15f2c412039ed232bdeb7edeb68ab90fee7

Contents?: true

Size: 1.87 KB

Versions: 9

Compression:

Stored size: 1.87 KB

Contents

# Utility module for type assertion
#
module Puppet::Pops::Types
module TypeAsserter
  # Asserts that a type_to_check is assignable to required_type and raises
  # a {Puppet::ParseError} if that's not the case
  #
  # @param subject [String] String to be prepended to the exception message
  # @param expected_type [PAnyType] Expected type
  # @param type_to_check [PAnyType] Type to check against the required type
  # @return The type_to_check argument
  #
  # @api public
  def self.assert_assignable(subject, expected_type, type_to_check)
    report_type_mismatch(subject, expected_type, type_to_check) unless expected_type.assignable?(type_to_check)
    type_to_check
  end

  # Asserts that a value is an instance of a given type and raises
  # a {Puppet::ParseError} if that's not the case
  #
  # @param subject [String] String to be prepended to the exception message
  # @param expected_type [PAnyType] Expected type for the value
  # @param value [Object] Value to check
  # @param nil_ok [Boolean] Can be true to allow nil value. Optional and defaults to false
  # @return The value argument
  #
  # @api public
  def self.assert_instance_of(subject, expected_type, value, nil_ok = false)
    unless value.nil? && nil_ok
      report_type_mismatch(subject, expected_type, TypeCalculator.singleton.infer_set(value).generalize) unless expected_type.instance?(value)
    end
    value
  end

  def self.report_type_mismatch(subject, expected_type, actual_type)
    # Do not give all the details for inferred types - i.e. format as Integer, instead of Integer[n, n] for exact
    # value, which is just confusing. (OTOH: may need to revisit, or provide a better "type diff" output).
    #
    raise TypeAssertionError.new(
        "#{subject} value has wrong type, expected #{expected_type}, actual #{actual_type}", expected_type, actual_type)
  end
  private_class_method :report_type_mismatch
end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
puppet-4.3.2 lib/puppet/pops/types/type_asserter.rb
puppet-4.3.2-x86-mingw32 lib/puppet/pops/types/type_asserter.rb
puppet-4.3.2-x64-mingw32 lib/puppet/pops/types/type_asserter.rb
puppet-4.3.1 lib/puppet/pops/types/type_asserter.rb
puppet-4.3.1-x86-mingw32 lib/puppet/pops/types/type_asserter.rb
puppet-4.3.1-x64-mingw32 lib/puppet/pops/types/type_asserter.rb
puppet-4.3.0 lib/puppet/pops/types/type_asserter.rb
puppet-4.3.0-x86-mingw32 lib/puppet/pops/types/type_asserter.rb
puppet-4.3.0-x64-mingw32 lib/puppet/pops/types/type_asserter.rb