Git Product home page Git Product logo

Comments (7)

backus avatar backus commented on August 19, 2024

If you define the nested structs then it works fine:

gem 'dry-types', '~> 0.8.1'
require 'dry/types'

module Types
  include Dry::Types.module

  module Coercible
    # these can be anything I just defined these so I could make your example work
    Email = Types::String
    Phone = Types::String
  end
end

class Preference < Dry::Types::Struct
  attribute :food, Types::Array
end

class Profile < Dry::Types::Struct
  attribute :email, Types::Coercible::Email # defined somewhere else
  attribute :phone, Types::Coercible::Phone # ^^

  attribute :preferences, Preference
end

class User < Dry::Types::Struct
  attribute :name, Types::Strict::String
  attribute :profile, Profile
end

user = User.new(
  name: 'Joe',
  profile: {
    email: '[email protected]',
    phone: '1(234)567-8900',
    preferences: {
      food: ['ice-cream']
    }
  }
)

user # => #<User name="Joe" profile=#<Profile email="[email protected]" phone="1(234)567-8900" preferences=#<Preference food=["ice-cream"]>>>

Edit: Ah I didn't see your note about explicit definitions

from dry-struct.

backus avatar backus commented on August 19, 2024

I know you mentioned not wanting to explicitly define these structs but what about just defining them within the class along side the attributes as private constants:

gem 'dry-types', '~> 0.8.1'
require 'dry/types'

module Types
  include Dry::Types.module

  module Coercible
    Email = Types::String
    Phone = Types::String
  end
end

class User < Dry::Types::Struct
  class Profile < Dry::Types::Struct
    class Preference < Dry::Types::Struct
      attribute :food, Types::Array
    end
    private_constant(:Preference)

    attribute :email, Types::Coercible::Email # defined somewhere else
    attribute :phone, Types::Coercible::Phone # ^^

    attribute :preferences, Preference
  end
  private_constant(:Profile)

  attribute :name, Types::Strict::String
  attribute :profile, Profile
end

user = User.new(
  name: 'Joe',
  profile: {
    email: '[email protected]',
    phone: '1(234)567-8900',
    preferences: {
      food: ['ice-cream']
    }
  }
)

user # => #<User name="Joe" profile=#<User::Profile email="[email protected]" phone="1(234)567-8900" preferences=#<User::Profile::Preference food=["ice-cream"]>>>

then the definitions are local to their usage, they can't be reused, but you also don't have to worry about dry-types dynamically constructing anonymous structs for you

from dry-struct.

nepalez avatar nepalez commented on August 19, 2024

@backus Yes, I do it just in a way of your last example. I think it is still a bit overkillish.

I also see the reason we cannot (or should not) implement a "plain" block syntax I gave above. Because the next question I ask myself is... what if I need that nested struct to be optional?

I have no good solution for this, so this is a problem yet, just the proposal for further discussion.

from dry-struct.

Fryie avatar Fryie commented on August 19, 2024

I believe this would be extremely useful, I have a response payload from an API that I want to store as a dry-struct and I think that having to create explicit inner classes for every layer of nesting adds too much overhead for me.

from dry-struct.

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

this is in master, #58

from dry-struct.

Fryie avatar Fryie commented on August 19, 2024

Cool thing! Just to clarify, these three things I'm not sure if they're possible yet:

  • Having an array whose elements are an anonymous struct (instead of just a single instance of that element)
  • Marking the nested struct as optional
  • Supplying a default for the nested struct

otherwise, I could create follow-up issues.

from dry-struct.

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

We'll unlikely go that far atm, there is transform_types for things like this, you also can build your own DSL on top of dry-struct, it's an easy thing to do.

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.