def iterator(self, type: Type, values: bool = False) -> "FieldIteratorT":
        """Get an iterator function for a given type, if possible."""

        if ismappingtype(type):
            iter = _valuescaller if values else _itemscaller
            return iter

        if isiterabletype(type):
            return _iter

        fields = self.get_fields(type, as_source=True) or {}

        if fields:
            func_name = get_defname("iterator", (type, values))
            oname = "o"
            ctx: dict = {}
            with Block(ctx) as main:
                with main.f(func_name, Block.p(oname)) as func:
                    if values:
                        for f in fields:
                            func.l(f"{Keyword.YLD} {oname}.{f}")
                    else:
                        for f in fields:
                            func.l(f"{Keyword.YLD} {f!r}, {oname}.{f}")

            return main.compile(name=func_name, ns=ctx)

        raise TranslatorTypeError(
            f"Cannot get iterator for type {type!r}, unable to determine fields."
        ) from None
Exemple #2
0
    def iterator(
        self,
        type: Type,
        values: bool = False,
        relaxed: bool = False,
        exclude: Tuple[str, ...] = (),
    ) -> IteratorT:
        """Get an iterator function for a given type, if possible."""
        mapping, iterable, builtin, namedtuple, typicklass = (
            ismappingtype(type),
            isiterabletype(type),
            isbuiltinsubtype(type),
            isnamedtuple(type),
            istypicklass(type),
        )
        if mapping:
            return _valuescaller if values else _itemscaller

        if (iterable, namedtuple, typicklass) == (True, False, False):
            return iter if values else enumerate

        if (builtin, iterable) == (True, False):
            raise TranslatorTypeError(
                f"Cannot get iterator for type {type.__name__!r}."
            ) from None

        fields = self.get_fields(type, as_source=True, exclude=exclude) or {}

        if not fields and not relaxed:
            raise TranslatorTypeError(
                f"Cannot get iterator for type {type.__name__!r}, "
                f"unable to determine fields."
            ) from None

        func_name = get_defname("iterator", (type, values))
        oname = "o"
        ctx: dict = {}
        with Block(ctx) as main:
            with main.f(func_name, Block.p(oname)) as func:
                if fields:
                    if values:
                        for f in fields:
                            func.l(f"{Keyword.YLD} {oname}.{f}")
                    else:
                        for f in fields:
                            func.l(f"{Keyword.YLD} {f!r}, {oname}.{f}")
                else:
                    func.l(f"{Keyword.YLD}")

        return main.compile(name=func_name, ns=ctx)
Exemple #3
0
 def _get_validator_name(self) -> str:
     return util.get_defname("validator", self)
Exemple #4
0
 def _get_name(annotation: Annotation) -> str:
     return util.get_defname("serializer", annotation)
Exemple #5
0
 def _get_name(source: Type, target: Type) -> str:
     return get_defname("translator", (source, target))
Exemple #6
0
 def _get_name(annotation: Annotation) -> str:
     return get_defname("deserializer", annotation)
Exemple #7
0
 def _get_name(annotation: "Annotation",
               constr: Optional["const.ConstraintsT"]) -> str:
     return get_defname("deserializer", (annotation, constr))