Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping: Appending to list, Adding to dictionary #143

Open
josephfinlayson opened this issue Feb 23, 2023 · 4 comments
Open

Mapping: Appending to list, Adding to dictionary #143

josephfinlayson opened this issue Feb 23, 2023 · 4 comments
Assignees
Labels
enhancement This is a feature enhancement question This issue is a Help Question

Comments

@josephfinlayson
Copy link

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?

@timsavage timsavage added the question This issue is a Help Question label Feb 23, 2023
@timsavage
Copy link
Member

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]

@timsavage
Copy link
Member

See the examples in the mapping docs.
https://odin.readthedocs.io/en/latest/ref/mapping/classes.html#applying-transformations-to-data

@josephfinlayson
Copy link
Author

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!

@timsavage
Copy link
Member

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.

@timsavage timsavage reopened this Feb 24, 2023
@timsavage timsavage added the enhancement This is a feature enhancement label Feb 24, 2023
@timsavage timsavage self-assigned this Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This is a feature enhancement question This issue is a Help Question
Projects
None yet
Development

No branches or pull requests

2 participants