# Are they some code sample of hierarchical Dry::Struct?

**URL:** <https://discourse.dry-rb.org/t/are-they-some-code-sample-of-hierarchical-dry-struct/212>\
**Category:** Support\
**Created:** [February 10, 2017, 11:32am UTC](https://discourse.dry-rb.org/t/are-they-some-code-sample-of-hierarchical-dry-struct/212 "2017-02-10T11:32:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![whisky](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/whisky/32/328_2.png) [@whisky](https://discourse.dry-rb.org/u/whisky)\
**Post date:** [February 10, 2017, 11:32am UTC](https://discourse.dry-rb.org/t/are-they-some-code-sample-of-hierarchical-dry-struct/212/1 "2017-02-10T11:32:29Z")

</div>

Does someone has sample code to share of `Dry::Struct` with hierarchical attributes ?

I’m trying some initialisation code around lines like :

```auto
module Config
  class Sidekiq < Dry::Struct
      constructor_type :schema
      attribute :redis, Dry::Types['optional.strict.hash'].schema(
          # not recommended for redis (lower speed),
          # but if we need several *segregated* cubes
          # we could namespace them (i.e all redis keys will be prefixed)
          :namespace, Dry::Types['strict.string'].optional,
          # If your client is single-threaded, we just need a single connection in our Redis connection pool
          :size, Dry::Types['coercible.int'].default(1)
      )
  end
end

```

but I got exception as #schema do not expect such signature  
and if I use a `Dry::Types['optional.strict.hash'].schema([tuples of id, type])` the initialisation of type think this is a `Dry::Sum` type.

I thought that this use case, could be better read with a `&block` to yield like

```auto
module Config
  class Sidekiq < Dry::Struct
      constructor_type :schema
      attribute :redis, Dry::Types['optional.strict.hash'] do 
          :namespace, Dry::Types['strict.string'].optional,
          :size, Dry::Types['coercible.int'].default(1)
      end

```

that will latter deliver `.dot` accessors like `myconfig.redis.namespace` or `myconfig[:redis][:namespace]`

I used to do a lot config -\> actual Class like this using variations of `Hashie/Mashie` or using `Representable`  
it could be interesting to have the same kind of hierarchical structuration in `Dry::Struct`

For now I don’t see #schema usage even on ‘big’ sample like `Berg`

---

<div class="post-metadata">

**Author:** ![flash-gordon](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/flash-gordon/32/494_2.png) [@flash-gordon](https://discourse.dry-rb.org/u/flash-gordon)\
**Post date:** [February 10, 2017, 5:05pm UTC](https://discourse.dry-rb.org/t/are-they-some-code-sample-of-hierarchical-dry-struct/212/2 "2017-02-10T17:05:10Z")

</div>

You can use a struct inside a struct, this will give a more predictable behavior tbh

```ruby
module Config
  class Sidekiq < Dry::Struct
    class RedisConfig < Dry::Struct
      constructor_type :schema

      attribute :namespace, Dry::Types['strict.string'].optional
      attribute :size, Dry::Types['coercible.int'].default(1)
    end

    constructor_type :schema

    attribute :redis, RedisConfig
  end
end

```

---

<div class="post-metadata">

**Author:** ![whisky](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/whisky/32/328_2.png) [@whisky](https://discourse.dry-rb.org/u/whisky)\
**Post date:** [February 10, 2017, 5:42pm UTC](https://discourse.dry-rb.org/t/are-they-some-code-sample-of-hierarchical-dry-struct/212/3 "2017-02-10T17:42:08Z")

</div>

lovely !!!

Thanks I’ll try this right away 😉
