Sha256: abe36759f38179ec109f793c8e6327b36adf90eeb8bf3925ca8d6942ec970b9a
Contents?: true
Size: 1.31 KB
Versions: 8
Compression:
Stored size: 1.31 KB
Contents
# typed: strict module Typed module Coercion class StructCoercer < Coercer extend T::Generic Target = type_member { {fixed: T::Struct} } 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::Struct) 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::Struct for Struct coercion.")) unless used_for_type?(type) return Success.new(value) if type.recursively_valid?(value) return Failure.new(CoercionError.new("Value of type '#{value.class}' cannot be coerced to #{type} Struct.")) unless value.is_a?(Hash) deserialization_result = T.cast(type, T::Types::Simple) .raw_type .deserialize_from(:hash, value) if deserialization_result.success? deserialization_result else Failure.new(CoercionError.new(deserialization_result.error.message)) end rescue ArgumentError, RuntimeError Failure.new(CoercionError.new("Given hash could not be coerced to #{type}.")) end end end end
Version data entries
8 entries across 8 versions & 1 rubygems