automapper-plus icon indicating copy to clipboard operation
automapper-plus copied to clipboard

Allow guessing of property names

Open mark-gerarts opened this issue 6 years ago • 2 comments

As it is now, the classes need to follow strict naming conventions in order to be mapped. A nice feature would be to be able to change this behaviour via the Options, making the mapper instead guess the source property name.

For example, when looking for the property firstName, the mapper would match the first occurrence of either [firstName, first_name, FirstName, ...].

Credit

mark-gerarts avatar Oct 17 '17 19:10 mark-gerarts

I am currently reading through the source to wrap my head around the architecture of this package. I'd really like to be a major contributor to this down the line.

There are clearly some arguments for and against this feature so I definitely think it should be something that must be explicitly set in the Options. Maybe something like:

$config
    ->registerMapping(CrazySource::class, SomeOtherCrazyDestination::class)
    ->withoutNamingConventions();

The method withoutNamingConventions() could simply set the naming conventions to a NoNamingConventions implementation. The NoNamingConventions would then be responsible for converting the property name, perhaps stripping it of underscores and converting it to lowercase?

samrap avatar Oct 23 '17 21:10 samrap

That's definitely a step in the right direction. Using a NoNamingConventions is an option, although it might be more fitting to put this logic in a NameResolver, either adding logic to the default one or creating a completely new one. The advantage here being that the NameResolver can already be overwritten in the Options.

That being said, I'm open for other ideas. I think the hardest part will be the actual guessing of source properties. If you wanna take a shot at this, then go ahead. I'll be more than happy to merge a good solution for this.

Thanks for your interest!

mark-gerarts avatar Oct 24 '17 11:10 mark-gerarts