Esempio n. 1
0
    def decode(self, data: dict, field_type: typing.Type) -> typing.Any:
        origin_args = typing.get_args(field_type)

        if data is None:
            return None

        return self.transcoder.decode(data, origin_args[0])
Esempio n. 2
0
    def encode(self, obj: typing.Any, field_type: typing.Type) -> typing.Any:
        origin_args = typing.get_args(field_type)

        if obj is None:
            return None

        return self.transcoder.encode(obj, origin_args[0])
Esempio n. 3
0
    def decode(self, data: dict, field_type: typing.Type) -> typing.Any:
        origin_args = typing.get_args(field_type)

        return {
            self.trancoder.decode(k, origin_args[0]):
            self.trancoder.decode(v, origin_args[1])
            for k, v in data.items()
        }
Esempio n. 4
0
    def encode(self, obj: typing.Any, field_type: typing.Type) -> typing.Any:
        origin_args = typing.get_args(field_type)

        return {
            self.trancoder.encode(k, origin_args[0]):
            self.trancoder.encode(v, origin_args[1])
            for k, v in obj.items()
        }
Esempio n. 5
0
    def can_handle(self, field_type: typing.Type) -> bool:
        origin = typing.get_origin(field_type)
        origin_args = typing.get_args(field_type)

        return (  # typing.Optional[some_type]
            origin is typing.Union and len(origin_args) == 2
            and origin_args[1] is type(None)  # noqa: E721
        )
Esempio n. 6
0
    def decode(self, data: dict, field_type: typing.Type) -> typing.Any:
        origin = typing.get_origin(field_type) or field_type
        origin_args = typing.get_args(field_type)

        return origin(self.trancoder.decode(o, origin_args[0]) for o in data)
Esempio n. 7
0
    def encode(self, obj: typing.Any, field_type: typing.Type) -> typing.Any:
        origin_args = typing.get_args(field_type)

        return [self.trancoder.encode(o, origin_args[0]) for o in obj]
Esempio n. 8
0
    def can_handle(self, field_type: typing.Type) -> bool:
        origin = typing.get_origin(field_type) or field_type
        origin_args = typing.get_args(field_type)

        return (origin in (list, tuple) and len(origin_args) == 2
                and origin_args[1] == Ellipsis)