Git Product home page Git Product logo

Comments (4)

johanlunds avatar johanlunds commented on August 20, 2024 1

For what it's worth, this was very unexpected behavior to me:

class A < Dry::Struct
  attribute :id,    Types::Strict::Integer
  attribute :type,  Types::Strict::String
  attribute :name,  Types::Strict::String
end

class B < Dry::Struct
  attribute :id,               Types::Strict::Integer
  attribute :type,             Types::Strict::String
  attribute :name,             Types::Strict::String
  attribute :primary_function, Types::Strict::String.optional
end

class WithSum < Dry::Struct
  attribute :sum, A | B
end

WithSum.new(
  sum: B.new(
    id: 123, 
    type: 'foo', 
    name: 'bar', 
    primary_function: 'yo'
  )
) # => #<WithSum sum=#<A id=123 type="foo" name="bar">>

I expected it to return my instance of B, not A.

A nice first step, would be to at least document how sum types work.

But I wonder if it's intended to work this way too.

from dry-struct.

johanlunds avatar johanlunds commented on August 20, 2024 1

Thank you for the context @flash-gordon 🙏.

And thanks for the suggestion. I solved our case with constrained types:

Types::Strict::String.constrained(eql: 'my_type')

from dry-struct.

flash-gordon avatar flash-gordon commented on August 20, 2024

But I wonder if it's intended to work this way too.

The problem lies in implicit coercion with to_hash. B gets coerced to a hash and to an A in order. It wasn't a good design choice. You can reject extra key by making the schema strict with schema schema.strict:

class StrictStruct < Dry::Struct
  schema schema.strict
end

class A < StrictStruct
  attribute :id,    Types::Strict::Integer
  attribute :type,  Types::Strict::String
  attribute :name,  Types::Strict::String
end

class B < StrictStruct
  attribute :id,               Types::Strict::Integer
  attribute :type,             Types::Strict::String
  attribute :name,             Types::Strict::String
  attribute :primary_function, Types::Strict::String.optional
end

class WithSum < Dry::Struct
  attribute :sum, A | B
end

This will still coerce data internally but these attempts will fail. I think we'll remove to_hash in 2.0.

from dry-struct.

flash-gordon avatar flash-gordon commented on August 20, 2024

Yeah, I do something similar to discriminate variants. 👍

from dry-struct.

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.