# Is there a proper way to use dry-transformer with dry-struct?

**URL:** <https://discourse.dry-rb.org/t/is-there-a-proper-way-to-use-dry-transformer-with-dry-struct/1605>\
**Category:** Support\
**Created:** [January 27, 2023, 7:02pm UTC](https://discourse.dry-rb.org/t/is-there-a-proper-way-to-use-dry-transformer-with-dry-struct/1605 "2023-01-27T19:02:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dvandersluis](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/dvandersluis/32/846_2.png) [@dvandersluis](https://discourse.dry-rb.org/u/dvandersluis)\
**Post date:** [January 27, 2023, 7:02pm UTC](https://discourse.dry-rb.org/t/is-there-a-proper-way-to-use-dry-transformer-with-dry-struct/1605/1 "2023-01-27T19:02:29Z")

</div>

I’m trying to use dry-transformer with dry-struct to take a JSON api response and transform it into a struct. Is there any “official” way to do this? The best way I could figure out was to override the `self.new` method which I don’t love doing:

```auto
class MyStruct < Dry::Struct
  class Transformer < Dry::Transformer::Pipe
    import Dry::Transformer::HashTransformations

    define! do
      # ...
    end
  end

  def self.new(attributes)
    data = Transformer.new.call(attributes)
    super(data)
  end
end

```

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [January 27, 2023, 8:17pm UTC](https://discourse.dry-rb.org/t/is-there-a-proper-way-to-use-dry-transformer-with-dry-struct/1605/2 "2023-01-27T20:17:10Z")

</div>

You could write an instance method to do this but building structs is so common I like to have a helper for this.

```ruby
module MyTransformations
  extend Dry::Transformer::Registry
  def self.struct(attributes, klass) = klass.new(**attributes)
end

class Transformer < Dry::Transformer::Pipe
  import MyTransformations

  define! do
    struct MyStruct
  end
end

Transformer.new(attributes)

```

The alternate way to do a one-off would be

```ruby
class Transformer < Dry::Transformer::Pipe
  define! do
    build_struct
  end

  def build_struct(attributes)
    MyStruct.new(**attributes)
  end
end

```

---

<div class="post-metadata">

**Author:** ![dvandersluis](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/dvandersluis/32/846_2.png) [@dvandersluis](https://discourse.dry-rb.org/u/dvandersluis)\
**Post date:** [January 27, 2023, 9:08pm UTC](https://discourse.dry-rb.org/t/is-there-a-proper-way-to-use-dry-transformer-with-dry-struct/1605/3 "2023-01-27T21:08:14Z")

</div>

Interesting! So instead of having the Struct do the transformation, have the Transformer return a struct. I’ll try that out, thank you!
