示例#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])
示例#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])
示例#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()
        }
示例#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()
        }
示例#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
        )
示例#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)
示例#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]
示例#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)