[dry-struct] Create camel case output of the object

Hi there :wave:

I’ve to build a massive JSON file based on few different API calls. I want to use Dry::Struct To easily manipulate that data like:

obj = Test::DataStruct.new
obj.send_envelope_description = 1

but expected output should be camelcase:
#<Test::DataStruct SendEnvelopeDescription=1>

Additionally, passed data should into Test::DataStruct obj should accept camelcase keys like:

data = { SendEnvelopeDescription: 1 }
test = TestDataStruct.new(data)
 => #<Test::DataStruct SendEnvelopeDescription=1>

I tried to use transform_keys { |key| key.to_s.camelize } but when I’m getting an error:

3.1.2 :045 > obj = Test::DataStruct.new(send_envelope_description: 1)

`block in resolve_missing_keys': [Test::DataStruct.new] :send_envelope_description is missing in Hash input (Dry::Struct::Error)

`block in resolve_missing_keys': :send_envelope_description is missing in Hash input (Dry::Types::MissingKeyError)

Is it possible to do so?

I don’t think dry-struct is supposed to deal with the matter. Just write a function that recursively transforms keys of a hash input, it’s 10 lines of code basically. This is what I use

      def camelize(value)
        case value
        when ::Array
          value.map { camelize(_1) }
        when ::Hash
          value.to_h do |key, v|
            [Dry::Inflector.camelize_lower(key.to_s).to_sym, camelize(v)]
          end
        else
          value
        end
      end

Then

camelize(struct.to_h)