Sha256: 5dccdfc3efb75220e34544b3abf2cecfe99990123b3d07c228422e6600733e78

Contents?: true

Size: 847 Bytes

Versions: 11

Compression:

Stored size: 847 Bytes

Contents

# typed: strict

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

      Target = type_member { {fixed: T::Enum} }

      sig { override.params(type: T::Types::Base).returns(T::Boolean) }
      def used_for_type?(type)
        return false unless type.respond_to?(:raw_type)

        !!(T.cast(type, T::Types::Simple).raw_type < T::Enum)
      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 inherit from T::Enum for Enum coercion.")) unless used_for_type?(type)

        Success.new(T.cast(type, T::Types::Simple).raw_type.from_serialized(value))
      rescue KeyError => e
        Failure.new(CoercionError.new(e.message))
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

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