surrealist icon indicating copy to clipboard operation
surrealist copied to clipboard

Implement type systems

Open akxcv opened this issue 5 years ago • 2 comments

Closes #148.

Now, Surrealist has two type systems out of the box: Surrealist::TypeSystems::Builtin and Surrealist::TypeSystems::DryTypes.

Every type system has its own type-checking strategy, error reporting strategy, and coercion strategy. Error reporting is done during the type-checking via result objects.

Configuration

To set the type system to use across all serializers:

Surrealist.configure do |config|
  config.type_system = :builtin # the default
  config.type_system = :dry_types # use Dry::Types
  config.type_system = MyTypeSystem # use a custom type system
end

To set the type system for a given serializer:

class MySerializer < Surrealist::Serializer
  type_system :dry_types

  json_schema do
    # ...
  end

  # ...
end

Defining a custom type system

To define a custom type system, create a module that conforms to Surrealist's type system interface. For example, you could support Thy like this:

module ThyTypeSystem
  class << self
    include Surrealist::Result::DSL

    # Returns a Result.
    def check_type(value, type_class)
      thy_result = type_class.check(value)
      # If everything is okay, return a successful result.
      return success if result.success?
      # If the type check failed, return a Failure with a message.
      failure(result.message)
    end

    # Thy does not support type coercion.
    def coerce(value, _type_class)
      value
    end
  end
end

I've also taken this chance to remove Surrealist::Carrier in favour of Surrealist::Config. Carrier was a confusing class with unclear responsibilities and Configuration is your standard configuration object.

And, the grammatically wrong namespaces_nesting_level is changed to namespace_nesting_level. I've introduced aliases for backward compatibility. And deprecation warnings for them.

akxcv avatar Oct 17 '19 13:10 akxcv

Please take a look at this initial implementation. Some questions I have:

  1. How do we document the type system interface (besides the README)? Right now, documentation comments are kind of duplicated between Builtin and DryTypes.
  2. Currently, this is a breaking change, because Surrealist now forces you to use a single type system per serializer, but before it used to guess whether a type is builtin or from dry-types. I think that this is fine, it should be rather rare that someone uses two different type systems per serializer in their code. But we might want to create a separate "guessing" type system for backward compatibility (it would ship deprecated from the start).

akxcv avatar Oct 17 '19 13:10 akxcv

approved

0exp avatar Oct 17 '19 13:10 0exp