marazt / object-mapper

ObjectMapper is a class for automatic object mapping in Python
MIT License
102 stars 28 forks source link

dataclass are not supported #25

Open arkadyzalko opened 4 years ago

arkadyzalko commented 4 years ago

@dataclass is a pretty good feature and unfortunatelly is not supported.

https://github.com/marazt/object-mapper/blob/9d62e9bf00cd78afdaccd6e950a6bb4a1dd1cc06/mapper/object_mapper.py#L171

Example

class ObjectMapperTest(unittest.TestCase):
    """
    Unit tests for the `ObjectMapper` module.
    """
    @dataclass
    class FromTestDataClass:
        a: int
        b: str

    @dataclass
    class ToTestDataClass:
        a: int
        b: str

    def test_mapping_creation_without_mappings_correct_using_dataclass(self):
        """ Test mapping creation without mappings with dataclass"""

        # Arrange
        from_class = FromTestClass()
        mapper = ObjectMapper()
        mapper.create_map(FromTestDataClass, ToTestDataClass)

        # Act
        result = mapper.map(FromTestDataClass(a=1, b='2'))

        # Assert
        self.assertTrue(isinstance(result, ToTestDataClass), "Target types must be same")
        self.assertEqual(result.name, from_class.name, "Name mapping must be equal")
        self.assertEqual(result, ToTestDataClass(a=1, b='2'), "Class instance must be equal")
raman-nbg commented 3 years ago

There is a pull request for this: #26

arisliang commented 3 years ago

Nice addition, when would we publish it?

arkadyzalko commented 3 years ago

The PR is not merged yet. @marazt could you merge this PR?