Sha256: f4d76ba8e04881f8d264787ac5d27634523d7821f93d3806d668e0e08c0e3f12

Contents?: true

Size: 1.69 KB

Versions: 234

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true
# typed: false

module T::Private
  # Dynamically confirm that `value` is recursively a valid value of
  # type `type`, including recursively through collections. Note that
  # in some cases this runtime check can be very expensive, especially
  # with large collections of objects.
  def self.check_type_recursive!(value, type)
    T::Private::Casts.cast_recursive(value, type, cast_method: "T.check_type_recursive!")
  end

  module Casts
    def self.cast(value, type, cast_method:)
      begin
        error = T::Utils.coerce(type).error_message_for_obj(value)
        return value unless error

        caller_loc = T.must(caller_locations(2..2)).first

        suffix = "Caller: #{T.must(caller_loc).path}:#{T.must(caller_loc).lineno}"

        raise TypeError.new("#{cast_method}: #{error}\n#{suffix}")
      rescue TypeError => e # raise into rescue to ensure e.backtrace is populated
        T::Configuration.inline_type_error_handler(e)
        value
      end
    end

    # there's a lot of shared logic with the above one, but factoring
    # it out like this makes it easier to hopefully one day delete
    # this one
    def self.cast_recursive(value, type, cast_method:)
      begin
        error = T::Utils.coerce(type).error_message_for_obj_recursive(value)
        return value unless error

        caller_loc = T.must(caller_locations(2..2)).first

        suffix = "Caller: #{T.must(caller_loc).path}:#{T.must(caller_loc).lineno}"

        raise TypeError.new("#{cast_method}: #{error}\n#{suffix}")
      rescue TypeError => e # raise into rescue to ensure e.backtrace is populated
        T::Configuration.inline_type_error_handler(e)
        value
      end
    end
  end
end

Version data entries

234 entries across 234 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.9064 lib/types/private/casts.rb
sorbet-runtime-0.5.9058 lib/types/private/casts.rb
sorbet-runtime-0.5.9057 lib/types/private/casts.rb
sorbet-runtime-0.5.9053 lib/types/private/casts.rb
sorbet-runtime-0.5.9052 lib/types/private/casts.rb
sorbet-runtime-0.5.9048 lib/types/private/casts.rb
sorbet-runtime-0.5.9045 lib/types/private/casts.rb
sorbet-runtime-0.5.9042 lib/types/private/casts.rb
sorbet-runtime-0.5.9041 lib/types/private/casts.rb
sorbet-runtime-0.5.9035 lib/types/private/casts.rb
sorbet-runtime-0.5.9033 lib/types/private/casts.rb
sorbet-runtime-0.5.9030 lib/types/private/casts.rb
sorbet-runtime-0.5.9019 lib/types/private/casts.rb
sorbet-runtime-0.5.9013 lib/types/private/casts.rb
sorbet-runtime-0.5.9006 lib/types/private/casts.rb
sorbet-runtime-0.5.9001 lib/types/private/casts.rb
sorbet-runtime-0.5.9000 lib/types/private/casts.rb
sorbet-runtime-0.5.8993 lib/types/private/casts.rb
sorbet-runtime-0.5.6539 lib/types/private/casts.rb
sorbet-runtime-0.5.6537 lib/types/private/casts.rb