Sha256: 2e29b980aec9721044f9656805ebb86b424339dcaa9a19fb1bdaca1df3c93212

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

# typed: strict

module Typed
  class HashSerializer < Serializer
    InputHash = T.type_alias { T::Hash[T.any(Symbol, String), T.untyped] }
    Input = type_member { {fixed: InputHash} }
    Output = type_member { {fixed: Params} }

    sig { params(schema: Schema, should_serialize_values: T::Boolean).void }
    def initialize(schema:, should_serialize_values: false)
      @should_serialize_values = should_serialize_values

      super(schema: schema)
    end

    sig { override.params(source: Input).returns(Result[T::Struct, DeserializeError]) }
    def deserialize(source)
      deserialize_from_creation_params(HashTransformer.new(should_serialize_values: should_serialize_values).deep_symbolize_keys(source))
    end

    sig { override.params(struct: T::Struct).returns(Result[Output, SerializeError]) }
    def serialize(struct)
      return Failure.new(SerializeError.new("'#{struct.class}' cannot be serialized to target type of '#{schema.target}'.")) if struct.class != schema.target

      Success.new(serialize_from_struct(struct: struct, should_serialize_values: should_serialize_values))
    end

    private

    sig { returns(T::Boolean) }
    attr_reader :should_serialize_values
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sorbet-schema-0.6.0 lib/typed/hash_serializer.rb
sorbet-schema-0.5.1 lib/typed/hash_serializer.rb
sorbet-schema-0.4.2 lib/typed/hash_serializer.rb
sorbet-schema-0.4.1 lib/typed/hash_serializer.rb