Skip to content

improvement suggestion: class methods for data mapping #10

Description

@Leemur89

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"]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions