Dry-transformation problem with hash

I have tried today used dry-transformation to transform payload which I receive from AWS SNS/SQS to shape which I need.

I have found that nest method (HashTransformation) doesn’t work with hash value? I can only assume that nest support only primitive datatype - Hash and Array aren’t supported. I am wondering why. Is there any reason for it?

example:

class MyMapper < Dry::Transformer::Pipe
  import Dry::Transformer::HashTransformations

  define! do
    unwrap :payload

    nest :order_fields, [:name, :description, :price]
  end
end

MyMapper.new.call({
  payload: {
    name: "foo",
    description: "bar",
    price: {
      value: "1.1",
      currency: "EUR"
    }
  }
})

It raises exception:

function name +price+ is not valid (Dry::Transformer::Compiler::InvalidFunctionNameError)

but when I changed it a bit to:

class MyMapper < Dry::Transformer::Pipe
  import Dry::Transformer::HashTransformations

  define! do
    unwrap :payload

    nest :order_fields, [:name, :description]
    nest :order_fields, [:price]
  end
end

Then it works.

Hey so actually the public API looks like this:

# frozen_string_literal: true

require "dry/transformer"

class Functions
  extend Dry::Transformer::Registry

  import Dry::Transformer::HashTransformations
end

class MyMapper < Dry::Transformer[Functions]
  define! do
    unwrap :payload

    nest :order_fields, %i[name description price]
  end
end

mapper = MyMapper.new

puts mapper.call(
  payload: {
    name: "foo",
    description: "bar",
    price: {
      value: "1.1",
      currency: "EUR"
    }
  }
).inspect

# {:order_fields=>{:name=>"foo", :description=>"bar", :price=>{:value=>"1.1", :currency=>"EUR"}}}

I know, we desperately need docs for this gem :slight_smile: