Code Monkey home page Code Monkey logo

Comments (5)

solnic avatar solnic commented on June 8, 2024 3

The only way to add custom types is to use public type definition API. It's a new thing, ie:

class Email
end

# shortcut which assumes `Email.method(:new)` is the constructor
Dry::Types.register_class(Email)

# this expands to:
type = Dry::Types::Definition.new(Email).constructor(Email.method(:new))
Dry::Types.register('email', type)

from dry-types.

solnic avatar solnic commented on June 8, 2024

Thanks for reporting this. I'd say it's a bug. I'll address this in the current refactoring.

from dry-types.

davidpelaez avatar davidpelaez commented on June 8, 2024

I had a problem that led me to this and it's basically that I cannot make the output of a type be a valid input for itself given this situation. This had the issue that I would take an object and coerce it at the entry of an HTTP call and then would pass that data to a series of steps that in the end get coerced again with the exact same coercer at the exit point. This is used for consistency so that we guarantee the format in both directions.

I solved the problem by defining a new type Nothing that can be nil or None:

require 'kleisli'
module Types
  class Nothing
    extend Dry::Data::TypeBuilder
    class << self
      def call(input)
        input.nil? ? Kleisli::Maybe::None.new : input
      end

      def try(input)
        call(input)
      end

      def valid?(input)
        #binding.pry
        input.nil? || input.is_a?(Kleisli::Maybe::None)
      end

      alias_method :[], :call
    end
  end
end

Dry::Data.register "types.nothing", Types::Nothing

class Kleisli::Maybe::None
  def to_json
    nil.to_json
  end
end

class Kleisli::Maybe::Some
  def to_json
    value.to_json
  end
end

Then instead of using #optional I created sum types by hand, e.g.: OptionalString = Nothing | String and this solved all my issues. I wanted to share this because maybe it can help the conversation by showing why another type besides Nil could be needed.

from dry-types.

davidpelaez avatar davidpelaez commented on June 8, 2024

I'm not sure this is the best way to create custom typed, but it's what I managed to figure out by reading the code since there were no docs about building custom types/coercers without recurring to sums or fmap.

from dry-types.

davidpelaez avatar davidpelaez commented on June 8, 2024

@solnic thanks for the PR and for the help with custom types!

from dry-types.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.