""" Schemas templates to use with swagger apispec @marshal_with instead schema classes in cases when schema can not be used. For example, when we need to return list or dict of flat types. """ from abc import ABC from typing import Any, Dict class SwaggerTemplate(dict, ABC): """Template for flat swagger output.""" template: str def __init__(self, *args, **kwargs): super().__init__() self["type"] = self.template class ListTemplate(SwaggerTemplate): """Template for list of simple items.""" template = "array" def __init__(self, item): super().__init__() if not isinstance(item, SwaggerTemplate): raise ValueError(f"{self.__class__.__name__} item's type should be SwaggerTemplate subclass.") self["items"] = item class DictTemplate(SwaggerTemplate): """Template for dictionary of simple items.""" template = "object" def __init__(self, value, example: Dict[str, Any] = None): super().__init__() self["additionalProperties"] = value if example: self["example"] = example class StringTemplate(SwaggerTemplate): """String template.""" template = "string" def __init__(self, format=None): super().__init__() if format: self["format"] = format class IntegerTemplate(SwaggerTemplate): """Integer template.""" template = "integer" class BooleanTemplate(SwaggerTemplate): """Boolean template.""" template = "boolean"