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
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)
def _get_validator_name(self) -> str: return util.get_defname("validator", self)
def _get_name(annotation: Annotation) -> str: return util.get_defname("serializer", annotation)
def _get_name(source: Type, target: Type) -> str: return get_defname("translator", (source, target))
def _get_name(annotation: Annotation) -> str: return get_defname("deserializer", annotation)
def _get_name(annotation: "Annotation", constr: Optional["const.ConstraintsT"]) -> str: return get_defname("deserializer", (annotation, constr))