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

improvement suggestion: class methods for data mapping #10

Open
Leemur89 opened this issue Apr 3, 2024 · 1 comment
Open

improvement suggestion: class methods for data mapping #10

Leemur89 opened this issue Apr 3, 2024 · 1 comment

Comments

@Leemur89
Copy link

Leemur89 commented Apr 3, 2024

Hello,

When most endpoints share a common structure (like headers or other parameters), it could be useful to have a parent Dto class from which inheriting all Dto classes.
No code change is require, but the following snippets could complement the documentation of the data mapping.

For instance in my case I have a common parameter customer-name that is present in the OAS in most endpoints.

class DtoCommon(Dto):
    CUSTOMER = os.getenv("CUSTOMER")

    @classmethod
    def get_parameter_relations(cls) -> List:
        customer = PropertyValueConstraint(
            property_name="customer-name", values=[cls. CUSTOMER]
        )
        return ([
                customer,
            ]
            + cls.parameter_relations()
        )

    @classmethod
    def parameter_relations(cls) -> List:
        return []

    @classmethod
    def get_relations(cls) -> List:
        return cls.relations()

    @classmethod
    def relations(cls) -> List:
        return []

And then create the "child" classes simply as follow:

class DtoChild(DtoCommon):
    @classmethod
    def parameter_relations(cls) -> List:
        return [IdDependency(
          property_name="wagegroup_id",
          get_path="/wagegroups",
          error_code=451,
        )]

And I can go even further by adding class attributes that would list the ignored properties:

class DtoCommon(Dto):
    CUSTOMER = os.getenv("CUSTOMER")
    IGNORED_PROPERTIES: List[str] = []
    IGNORED_PARAMETERS: List[str] = []

    @classmethod
    def get_parameter_relations(cls) -> List:
        customer = PropertyValueConstraint(
            property_name="customer-name", values=[cls. CUSTOMER]
        )
        ignored_parameters = list(
            map(
                lambda value: PropertyValueConstraint(
                    property_name=value, values=[IGNORE]
                ),
                cls.IGNORED_PARAMETERS,
            )
        )
        return (
            ignored_parameters
            + [
                customer,
            ]
            + cls.parameter_relations()
        )

    @classmethod
    def parameter_relations(cls) -> List:
        return []

    @classmethod
    def get_relations(cls) -> List:
        ignored_properties = list(
            map(
                lambda value: PropertyValueConstraint(
                    property_name=value, values=[IGNORE]
                ),
                cls.IGNORED_PROPERTIES,
            )
        )
        return ignored_properties + cls.relations()

    @classmethod
    def relations(cls) -> List:
        return []

And simply add the list of ignored properties in the child class:

class DtoOtherChild(DtoCommon):
  IGNORED_PROPERTIES = ["limit", "age"]
@robinmackaij
Copy link
Collaborator

Having some form of "advanced mapping guide" would be nice to have. In a client project, we're doing similar things, but I can't share that as public examples.

The design in indeed an ABC implementation to allow advanced logic in Python to be flexible in handling different scenarios.

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