Sha256: 12427a89f4a1bd3962e344484276c03b253ba42e5413b32cb79ef7f68f03e572

Contents?: true

Size: 1.96 KB

Versions: 262

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true
# https://jira.corp.stripe.com/browse/RUBYPLAT-1107
# typed: false

module T::Types
  # Takes a list of types. Validates each item in an array using the type in the same position
  # in the list.
  class FixedArray < Base
    attr_reader :types

    def initialize(types)
      @types = types.map {|type| T::Utils.coerce(type)}
    end

    # @override Base
    def name
      "[#{@types.join(', ')}]"
    end

    # @override Base
    def recursively_valid?(obj)
      if obj.is_a?(Array) && obj.length == @types.length
        i = 0
        while i < @types.length
          if !@types[i].recursively_valid?(obj[i])
            return false
          end
          i += 1
        end
        true
      else
        false
      end
    end

    # @override Base
    def valid?(obj)
      if obj.is_a?(Array) && obj.length == @types.length
        i = 0
        while i < @types.length
          if !@types[i].valid?(obj[i])
            return false
          end
          i += 1
        end
        true
      else
        false
      end
    end

    # @override Base
    private def subtype_of_single?(other)
      case other
      when FixedArray
        # Properly speaking, covariance here is unsound since arrays
        # can be mutated, but sorbet implements covariant tuples for
        # ease of adoption.
        @types.size == other.types.size && @types.zip(other.types).all? do |t1, t2|
          t1.subtype_of?(t2)
        end
      else
        false
      end
    end

    # This gives us better errors, e.g.:
    # "Expected [String, Symbol], got [String, String]"
    # instead of
    # "Expected [String, Symbol], got Array".
    #
    # @override Base
    def describe_obj(obj)
      if obj.is_a?(Array)
        if obj.length == @types.length
          item_classes = obj.map(&:class).join(', ')
          "type [#{item_classes}]"
        else
          "array of size #{obj.length}"
        end
      else
        super
      end
    end
  end
end

Version data entries

262 entries across 262 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.9172 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9167 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9163 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9161 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9158 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9155 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9154 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9152 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9151 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9147 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9139 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9133 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9130 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9125 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9120 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9115 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9112 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9105 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9104 lib/types/types/fixed_array.rb
sorbet-runtime-0.5.9103 lib/types/types/fixed_array.rb