anikolaienko / py-automapper

Python object auto mapper
https://anikolaienko.github.io/py-automapper/
MIT License
59 stars 10 forks source link

Support duplicate source objects in adding mappings #19

Closed bmulh closed 6 months ago

bmulh commented 1 year ago

It would be nice to be able to register multiple mappings for the same source object like:

mapper.add(Foo, Bar)
mapper.add(Foo, Baz)

foo = Foo()
bar = mapper.map(foo, Bar)
baz = mapper.map(foo, Baz)
anikolaienko commented 6 months ago

Hi @bmulh, Thank you for your issue. Sorry for very delayed response.

In this situation automapper wouldn't know to which output class to map. You can use instead:

foo = Foo()
bar = mapper.to(Bar).map(foo)
baz = mapper.to(Baz).map(foo)

add(...) function exists to explicitly associate mapping, e.g. Foo maps to Bar or Foo maps to Baz. But if you have more than 1 class that should be associated with Foo, then there is no way to know what output class user expects to receive: Bar or Baz.