Sha256: 0b5d4d2d809b74c11694a2cf8b169cb9cbf49073e290dc8c2475daa00680834b
Contents?: true
Size: 1008 Bytes
Versions: 1
Compression:
Stored size: 1008 Bytes
Contents
# typed: strict module Typed module Coercion class StructCoercer < Coercer extend T::Generic Target = type_member { {fixed: T::Struct} } sig { override.params(type: Field::Type).returns(T::Boolean) } def used_for_type?(type) type.is_a?(Class) && !!(type < T::Struct) end sig { override.params(type: Field::Type, 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 type.is_a?(Class) && type < T::Struct return Success.new(value) if value.instance_of?(type) return Failure.new(CoercionError.new("Value of type '#{value.class}' cannot be coerced to #{type} Struct.")) unless value.is_a?(Hash) Success.new(type.from_hash!(HashTransformer.new.deep_stringify_keys(value))) rescue ArgumentError => e Failure.new(CoercionError.new(e.message)) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
sorbet-schema-0.4.0 | lib/typed/coercion/struct_coercer.rb |