Sha256: f45d2c4c67dfaa00a7b41c94e47a36f200bc7af4d184942602fe7c96f924e88b

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

# typed: strict

module Typed
  class Serializer
    extend T::Sig
    extend T::Helpers
    extend T::Generic
    abstract!

    Input = type_member
    Output = type_member
    Params = T.type_alias { T::Hash[Symbol, T.untyped] }
    DeserializeResult = T.type_alias { Result[T::Struct, DeserializeError] }

    sig { returns(Schema) }
    attr_reader :schema

    sig { params(schema: Schema).void }
    def initialize(schema:)
      @schema = schema
    end

    sig { abstract.params(source: Input).returns(DeserializeResult) }
    def deserialize(source)
    end

    sig { abstract.params(struct: T::Struct).returns(Result[Output, SerializeError]) }
    def serialize(struct)
    end

    private

    sig { params(creation_params: Params).returns(DeserializeResult) }
    def deserialize_from_creation_params(creation_params)
      results = schema.fields.map do |field|
        value = creation_params[field.name]

        if value.nil? || field.works_with?(value)
          field.validate(value)
        else
          coercion_result = Coercion.coerce(type: field.type, value: value)

          if coercion_result.success?
            field.validate(coercion_result.payload)
          else
            Failure.new(Validations::ValidationError.new(coercion_result.error.message))
          end
        end
      end

      Validations::ValidationResults
        .new(results: results)
        .combine
        .and_then do |validated_params|
          Success.new(schema.target.new(**validated_params))
        end
    end

    sig { params(struct: T::Struct, should_serialize_values: T::Boolean).returns(T::Hash[Symbol, T.untyped]) }
    def serialize_from_struct(struct:, should_serialize_values: false)
      hsh = schema.fields.each_with_object({}) { |field, hsh| hsh[field.name] = field.serialize(struct.send(field.name)) }.compact

      HashTransformer.new(should_serialize_values: should_serialize_values).deep_symbolize_keys(hsh)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sorbet-schema-0.5.1 lib/typed/serializer.rb