Sha256: 84dcfd8c6f22cb4ccc0abd5cf5f165b8a21145467200f7eda2ac21271f442ec4

Contents?: true

Size: 1.13 KB

Versions: 11

Compression:

Stored size: 1.13 KB

Contents

# typed: strict

module Typed
  module Coercion
    class TypedArrayCoercer < Coercer
      extend T::Generic

      Target = type_member { {fixed: T::Array[T.untyped]} }

      sig { override.params(type: T::Types::Base).returns(T::Boolean) }
      def used_for_type?(type)
        type.is_a?(T::Types::TypedArray)
      end

      sig { override.params(type: T::Types::Base, value: Value).returns(Result[Target, CoercionError]) }
      def coerce(type:, value:)
        return Failure.new(CoercionError.new("Field type must be a T::Array.")) unless used_for_type?(type)
        return Failure.new(CoercionError.new("Value must be an Array.")) unless value.is_a?(Array)

        return Success.new(value) if type.recursively_valid?(value)

        coerced_results = value.map do |item|
          Coercion.coerce(type: T.cast(type, T::Types::TypedArray).type, value: item)
        end

        if coerced_results.all?(&:success?)
          Success.new(coerced_results.map(&:payload))
        else
          Failure.new(CoercionError.new(coerced_results.select(&:failure?).map(&:error).map(&:message).join(" | ")))
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
sorbet-schema-0.9.2 lib/typed/coercion/typed_array_coercer.rb
sorbet-schema-0.9.1 lib/typed/coercion/typed_array_coercer.rb
sorbet-schema-0.9.0 lib/typed/coercion/typed_array_coercer.rb
sorbet-schema-0.8.0 lib/typed/coercion/typed_array_coercer.rb
sorbet-schema-0.7.2 lib/typed/coercion/typed_array_coercer.rb
sorbet-schema-0.7.1 lib/typed/coercion/typed_array_coercer.rb
sorbet-schema-0.7.0 lib/typed/coercion/typed_array_coercer.rb
sorbet-schema-0.6.0 lib/typed/coercion/typed_array_coercer.rb
sorbet-schema-0.5.1 lib/typed/coercion/typed_array_coercer.rb
sorbet-schema-0.4.2 lib/typed/coercion/typed_array_coercer.rb
sorbet-schema-0.4.1 lib/typed/coercion/typed_array_coercer.rb