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

Dynamic entry_name based on type of child property #1476

Open
svenhaack opened this issue Feb 16, 2023 · 1 comment
Open

Dynamic entry_name based on type of child property #1476

svenhaack opened this issue Feb 16, 2023 · 1 comment

Comments

@svenhaack
Copy link

svenhaack commented Feb 16, 2023

Hello Johnannes,

first of all I wanted to thank you personally for the serializer. We are using it successfully in 2 projects and really love it.

Currently I am facing a problem and hope to get help here in the group.

I have a user object in which I have stored favourite dishes of the users as a list.

An example:

<user firstname="John" lastname="Doe">
  <favourites>
	<pizzas>
	  <pizza name="Pizza Hawaii" />
	</pizzas>
	<pastas>
	  <pasta name="Lasagne" /> 
	</pastas>
  </favourites>
</user>

I need the favorite foods in a flat list now though, so in this case:

<favourites>
	<pizza name="Pizza Hawaii" />
	<pasta name="Lasagne" /> 
</favourites>

For this I implemented a getItems() method in my Favourites object instead of getPizzas()and getPastas(), which returns me dishes of type "Pizza" or "Pasta".

Now how can I implement that I can set the entry_name in the list depending on the child object.

My Favourites specification looked like this before:

pizzas:
  expose: true
  accessor:
    getter: getPizzas
  xml_list:
    entry_name: pizza
pastas:
  expose: true
  accessor:
    getter: getPastas
  xml_list:
    entry_name: pasta

The new specification now looks like this:

items:
  expose: true
  accessor:
  getter: getItems
  xml_list:
    entry_name: <pizza or pasta based on the type>

Pizza and Pasta object now has a "type" property storing "pizza" and "pasta", I did already exclude it in the child types because the structure here is given and it cannot be added as a new attribute type.

How can this be achieved?

Thanks for your help!

Best regards,
Sven

@scyzoryck
Copy link
Collaborator

Hi @svenhaack !

I think inline option under xml_list will help you to achieve your goal.

See: yml_reference.rst

Best, scyzoryck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants