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

add custom mixin classes #732

Closed
leo-b opened this issue Dec 19, 2022 · 4 comments
Closed

add custom mixin classes #732

leo-b opened this issue Dec 19, 2022 · 4 comments

Comments

@leo-b
Copy link

leo-b commented Dec 19, 2022

Hi!

First of all thanks for providing such cool XML bindings!
Generating code using python dataclasses really produces the most beautiful and clearly arranged representations of my bloated XML structures.

In my project I'd like to enrich the data container object hierarchy generated by xsdata (which is based on a externally provided xsd schema) by my own methods that add some custom logic.

My idea is to extend the dataclass output by automatically adding mixin classes from a custom module that is specified during model generation.
Those custom classes should be automatically added to my generated dataclasses as additional base classes.

Do you think this could make sense and would be possible with reasonable effort?
Could you give some hints how to implement this? Are class extensions the correct place to add the mixins?

Example:

  • code generated by xsdata without custom mixins:
@dataclass
class AddressList:
    class Meta:
        name = "addressList"

    adresse: List[Address] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "min_occurs": 1,
        }
    )
  • my custom_mixin_classes.py:
class MyAddressList:
    class Meta:
        name = "addressList"

    def get_primary_addr(self):
       # custom code to compute primary address
  • code generated by xsdata with custom mixins:
from custom_mixin_classes import MyAddressList

@dataclass
class AddressList(MyAddressList):
    class Meta:
        name = "addressList"

    adresse: List[Address] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "min_occurs": 1,
        }
    )

Cheers,
--leo

@tefra
Copy link
Owner

tefra commented Dec 24, 2022

Something like an output configuration something like this?

  <Modifications>
    <Class qualname="generated.models.AddressList">
      <AddBaseClass qualname="custom.mixins.MyAddressList"/>
    </Class>
  </Modifications>

@rvalyi
Copy link

rvalyi commented Dec 24, 2022

We would be interested in this too, like to plug custom Python validators...
cc @renatonlima

BTW, may be I did it wrong, but in my xsdata-odoo plugin (inspired from your xsdata-plantuml plugin, I tried to have custom configuration options and that didn't seem easy because from what I remember it would load the default GeneratorConfig class before even loading my plugin and in GeneratorConfig the attributes seem fixed. So if you touch the GeneratorConfig and have some idea to make this easier...

@leo-b
Copy link
Author

leo-b commented Dec 24, 2022

Something like an output configuration something like this?

Or maybe just configure a python module name, enumerate over the classes in that module and use class Meta info to connect them to the superclass? (Just like the parser finds the correct class for the root node if no clazz argument is specified.) This would reduce the config overhead if there are many mixins to attach.

@tefra
Copy link
Owner

tefra commented May 28, 2023

Hey @leo-b

I added the ability to extend generated models with base classes and custom decorators
https://xsdata.readthedocs.io/en/latest/examples/extending-models.html

The update is on master wave

@tefra tefra closed this as completed May 28, 2023
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

3 participants