odin
odin copied to clipboard
Mapping: Appending to list, Adding to dictionary
I'm trying to map between two resources
class AirtableShopifyMapper(odin.Mapping):
from_resource = AirTableProduct
to_resource = ShopifyProduct
@odin.map_list_field(from_field='wholesale_price', to_field='metafields')
def wholesale_price(self, value):
return ["wholesale_price"]
@odin.map_list_field(from_field='sample_price', to_field='metafields')
def sample_price (self, value):
return ["sample_price"]
Specifically, I want to find out how to append to a field on the to_resource I can't work out how to do this. I've tried to access the to_object.metafields on self to keep appending, but I don't see it avaiable. Any tips?
Mapping rules are used to build up a dictionary of keywords that are applied to a new resource instance, this doesn't allow for fields to be appended to, however, a mapping rule can accept multiple inputs allowing for your mapping to be defined as:
class AirTableProductToShopifyProduct(odin.Mapping):
from_resource = AirTableProduct
to_resource = ShopifyProduct
@odin.map_list_field(from_field=('wholesale_price', 'sample_price')
def metafields(self, wholesale_price, sample_price):
return [wholesale_price, sample_price]
See the examples in the mapping docs. https://odin.readthedocs.io/en/latest/ref/mapping/classes.html#applying-transformations-to-data
Thank you, ideally for me there would be a way to have independent mappers to append to the list. I have 20 input fields, that need to be appended to this list.
To make the code more testable and maintainable, it would be useful to have a way of defining independent mappers, that append to the list. I will close this for now, but please consider this feature. Thanks!
I've been thinking about how that would be achieved effectively, as this is a bit of a special case I'm loath to add more complexity and change the behaviour of the internals of mappers to maintain compatibility with existing usage.
One possible solution would be a "combining rule" that can append the results of several rules (internally a field mapping is defined as the action on a mapping rule) before returning the final result. This would allow for more flexibility without needing to modify the internals of mappers.
I'm going to re-open this issue as a feature request.