Source code for scim2_sqlalchemy.mapping

from collections.abc import Callable
from collections.abc import Iterable
from collections.abc import Iterator
from collections.abc import Mapping
from dataclasses import dataclass
from dataclasses import field
from enum import Enum
from inspect import isclass
from typing import Any

from pydantic import BaseModel
from scim2_models import Attribute as SchemaAttribute
from scim2_models import AttributeBinding
from scim2_models import Mutability
from scim2_models import Path
from scim2_models import Resource
from scim2_models import ResponseParameters
from scim2_models import Schema
from scim2_models import Uniqueness
from sqlalchemy import Column
from sqlalchemy import ColumnElement
from sqlalchemy import Dialect
from sqlalchemy import Enum as SqlEnum
from sqlalchemy import String
from sqlalchemy import Uuid
from sqlalchemy import cast
from sqlalchemy import func
from sqlalchemy import inspect
from sqlalchemy import literal
from sqlalchemy.dialects import mysql
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import InstrumentedAttribute
from sqlalchemy.orm import Mapper
from sqlalchemy.orm import RelationshipProperty
from sqlalchemy.orm import strategy_options
from sqlalchemy.sql.compiler import SQLCompiler
from sqlalchemy.sql.functions import FunctionElement
from sqlalchemy.types import TypeDecorator
from sqlalchemy.types import TypeEngine

_MARIADB = ("mysql", "mariadb")
"""The names of the dialects connecting to MariaDB."""

_MARIADB_TEXT = mysql.TEXT(charset="utf8mb4", collation="utf8mb4_nopad_bin")
"""The type of a string column without length on MariaDB, which needs a length for a VARCHAR.

Its values differ by code point, as the storage compares them.
"""

_INTEGERS = range(-(2**63), 2**63)
"""The integers a database can store, in a BIGINT or in an SQLite INTEGER."""

_UUID_GROUPS = ((1, 8), (9, 4), (13, 4), (17, 4), (21, 12))
"""The start and the length of each group of digits of a UUID, without its hyphens."""


class _Storage(Enum):
    """How a mapping stores an attribute.

    A derived attribute is read from another resource, such as the display of
    a member, so writing it changes nothing. A ref is the $ref of a link,
    built from its value: clients may send it, and writing it changes nothing
    either. A link type is the type a Link gives its entries: clients may send
    it, and the storage checks it against the linked resource.
    """

    written = "written"
    read_only = "read only"
    derived = "derived"
    ref = "ref"
    link_type = "link type"


class _UuidText(FunctionElement[str]):
    """The text of a UUID column, in the form with hyphens that SCIM holds."""

    type = String()
    inherit_cache = True


@compiles(_UuidText)
def _compile_uuid_text(element: _UuidText, compiler: SQLCompiler, **kw: Any) -> str:
    """Write the text of a UUID.

    A database without a UUID type stores its 32 digits without hyphens, so
    the hyphens are put back. A variant of the column type for the database,
    such as a string, is written as it is.
    """
    (column,) = element.clauses
    text: ColumnElement[str] = cast(column, String)
    uuid_type = _uuid_type(column.type.dialect_impl(compiler.dialect))
    if uuid_type is not None and not (
        compiler.dialect.supports_native_uuid and uuid_type.native
    ):
        groups = [
            func.substr(text, start, length, type_=String)
            for start, length in _UUID_GROUPS
        ]
        text = groups[0]
        for group in groups[1:]:
            text = text + "-" + group
    return compiler.process(text, **kw)


def _uuid_type(sql_type: Any) -> Uuid[Any] | None:
    """Return the Uuid type of a column, also under a TypeDecorator, or None for another type."""
    sql_type = getattr(sql_type, "impl_instance", sql_type)
    return sql_type if isinstance(sql_type, Uuid) else None


def _table_column(expression: Any) -> Column[Any] | None:
    """Return the table column of an expression, or None for a hybrid property or a column property over a SQL expression."""
    columns = getattr(getattr(expression, "property", None), "columns", None)
    column = columns[0] if columns else None
    return column if isinstance(column, Column) else None


def _write_only(binding: AttributeBinding) -> bool:
    """Whether the schema makes an attribute write-only, itself or through its parent."""
    return Mutability.write_only in (
        binding.model.get_field_annotation(binding.field_name, Mutability),
        binding.get_annotation(Mutability),
    )


def _python_type(expression: Any) -> type | None:
    """Return the Python type of the values of an expression, or None if its type tells none.

    SQLAlchemy 2.0 raises NotImplementedError for such a type, and 2.1 returns object.
    """
    try:
        python_type: type = expression.type.python_type
    except NotImplementedError:
        return None
    return None if python_type is object else python_type


def _holds_text(expression: Any) -> bool:
    """Whether an expression holds strings. A type that tells no Python type is taken as text."""
    return _python_type(expression) in (str, None)


def _key(binding: AttributeBinding) -> tuple[type[BaseModel], str, str | None]:
    """Return what identifies an attribute, whatever the case or the form of its path."""
    return (binding.model, binding.field_name, binding.sub_field_name)


def _overlap(
    key: tuple[type[BaseModel], str, str | None],
    other: tuple[type[BaseModel], str, str | None],
) -> bool:
    """Tell whether two attributes are the same, or one holds the other."""
    return key == other or (key[:2] == other[:2] and None in (key[2], other[2]))


[docs] class Attribute: """A column holding a SCIM attribute that the storage only reads, or only writes. To store a value in another form, map the attribute to a :class:`~sqlalchemy.ext.hybrid.hybrid_property` that converts it, in Python and in SQL. :param expression: The column, or the :class:`~sqlalchemy.ext.hybrid.hybrid_property`, holding the value. :param readable: Whether the storage reads the attribute. When :data:`False`, the attribute is write-only, such as a password: the storage never returns it, nor filters or sorts on it. :param writable: Whether the storage writes the attribute. When :data:`False`, the attribute is read-only. """ def __init__( self, expression: Any, *, readable: bool = True, writable: bool = True ) -> None: self.expression = expression self.readable = readable self.writable = writable
[docs] class Hashed(TypeDecorator[str]): """A column type storing the hash of a string, such as a password. The column stores what the hash function returns for the value written. Map it with ``readable=False``, since the storage cannot read a hash back. :param hash_value: The function hashing a value. :param length: The length of the column. Without it, MariaDB stores a TEXT. """ impl = String cache_ok = True def __init__( self, hash_value: Callable[[str], str], length: int | None = None ) -> None: super().__init__(length) self.hash_value = hash_value self.length = length def load_dialect_impl(self, dialect: Dialect) -> TypeEngine[Any]: """Return the type of the column, a TEXT on MariaDB without a length.""" if self.length is None and dialect.name in _MARIADB: return dialect.type_descriptor(_MARIADB_TEXT) return dialect.type_descriptor(self.impl_instance) def process_bind_param(self, value: str | None, dialect: Dialect) -> str | None: """Hash the value before it is sent to the database.""" return None if value is None else self.hash_value(value)
[docs] class Many: """The entries of a multi-valued attribute, stored in a child table. The entries belong to the resource: writing the resource replaces them. The relationship needs ``cascade="all, delete-orphan"`` (:ref:`sqlalchemy:cascade_delete_orphan`). Its ``order_by`` gives the order of the entries. Without it, the order is the one the database returns. A sort uses the ``primary`` entry, or else the first one. Since writing replaces the entries, their columns are all readable: a client never sends back a write-only value, so a write would lose it. Their columns are also either all written or none. The entries of a multi-valued simple attribute, such as a list of strings, are the values of one column of the child table. The storage keeps their order and their duplicates. :param relationship: The :func:`~sqlalchemy.orm.relationship` from the resource record to the entry records. :param attributes: The columns of the entry records, by sub-attribute name, such as ``"value"`` or ``"type"``. For a simple attribute, the column holding the values. """ def __init__(self, relationship: Any, attributes: Mapping[str, Any] | Any) -> None: self.relationship = relationship self.attributes = attributes
@dataclass(eq=False) class _Column: """A SCIM attribute stored in a column or a hybrid property. textual tells whether filters compare the column as text, as they do for an integer identifier. """ binding: AttributeBinding expression: ColumnElement[Any] key: str readable: bool writable: bool textual: bool = False @property def is_string(self) -> bool: """Whether the attribute is a SCIM string.""" target = self.binding.target_type return ( target is not None and SchemaAttribute.Type.from_python(target) == SchemaAttribute.Type.string ) @property def casefolded(self) -> bool: """Whether the values compare without their case.""" return self.is_string and not self.binding.case_exact @property def nullable(self) -> bool: """Whether the column can hold no value. A hybrid or a column property is taken as nullable.""" column = _table_column(self.expression) return column is None or bool(column.nullable) @property def required(self) -> bool: """Whether a new record needs a value: the column holds no null, and has no default.""" column = _table_column(self.expression) return ( column is not None and not column.nullable and column.default is None and column.server_default is None ) @property def enumeration(self) -> SqlEnum | None: """The type of the column when it is an Enum, which stores strings.""" sql_type = self.expression.type return sql_type if isinstance(sql_type, SqlEnum) else None @property def compared(self) -> ColumnElement[Any]: """The expression filters and sorts compare. An Enum column is compared as text. PostgreSQL applies no string function to its native enumerations, and sorts them in the order of their declaration. A UUID is compared in its form with hyphens. """ if ( self.textual or self.enumeration is not None or _uuid_type(self.expression.type) is not None ): return self.text return self.expression @property def text(self) -> ColumnElement[str]: """The values as text, such as a UUID in the form with hyphens.""" if _uuid_type(self.expression.type) is not None: return _UuidText(self.expression) return cast(self.expression, String) @staticmethod def stored_string(enumeration: SqlEnum, member: Enum) -> str: """Return the string an Enum column stores for a member: its name, or its value with values_callable.""" strings: dict[Enum, str] = dict( zip(type(member), enumeration.enums, strict=False) ) return strings[member] @dataclass(eq=False) class _Collection: """A multi-valued attribute, or a complex one, stored in related records. The entries of an owned collection belong to the resource. The others are links to the resources of another mapping, the target. A single collection holds one entry at most, such as the manager of a user. A simple collection holds the values of a multi-valued simple attribute, in the column it keeps under None. link_type is the type a Link gives its entries, and type_column the constant filters compare it as. """ binding: AttributeBinding relationship: InstrumentedAttribute[Any] columns: dict[str | None, _Column] = field(default_factory=dict) resource_type: str | None = None target: "ResourceMapping | None" = None link_type: str | None = None type_column: _Column | None = None @property def owned(self) -> bool: """Whether the entries belong to the resource, rather than link to other resources.""" return self.resource_type is None @property def entry_class(self) -> type[Any]: """The SQLAlchemy model of the entries.""" return self.relationship_property.mapper.class_ @property def relationship_property(self) -> RelationshipProperty[Any]: """The relationship holding the entries.""" return self.relationship.property # type: ignore[return-value] @property def single(self) -> bool: """Whether the collection holds one entry at most.""" return not self.binding.is_multivalued @property def simple(self) -> bool: """Whether the collection holds the values of a multi-valued simple attribute.""" return None in self.columns def column(self, sub_field_name: str | None) -> _Column | None: """Return the column of a sub-attribute, or of the values of a simple collection named alone. A complex collection named alone has no column. """ if sub_field_name == "type" and self.type_column is not None: return self.type_column return self.columns.get(sub_field_name) def exists(self, condition: Any = None) -> ColumnElement[bool]: """Return the condition that an entry exists, and matches condition when given.""" method = self.relationship.has if self.single else self.relationship.any return method() if condition is None else method(condition) @property def writable(self) -> bool: """Whether writing the resource writes these entries.""" if self.owned and not any(column.writable for column in self.columns.values()): return False mutability = self.binding.model.get_field_annotation( self.binding.field_name, Mutability ) return bool(mutability != Mutability.read_only)
[docs] class ResourceMapping: """How the resources of a SCIM model are stored in the records of a SQLAlchemy model. The paths are checked against the SCIM model when the mapping is created, so a mistake raises at import time rather than at the first request. :param model: The SCIM model of the resources, such as ``User[EnterpriseUser]``. :param record: The SQLAlchemy model of the records. :param attributes: The attributes, by SCIM path. A value is a column, a :class:`~sqlalchemy.ext.hybrid.hybrid_property`, an :class:`Attribute`, a :class:`Many`, a :class:`Link`, or a list of :class:`Link`. ``id``, ``meta.created`` and ``meta.lastModified`` are required. :param version: The ``version_id_col`` of the mapper of ``record``, which gives ``meta.version`` (:ref:`sqlalchemy:mapper_version_counter`). It only changes when the record changes, not when a linked record does: adding a member to a group leaves the version of the user as it is. :raises ValueError: When a path is not valid for the model, when a value does not fit the attribute of its path, or when ``id`` is mapped to a column that can hold ``NULL``. """ _REQUIRED = ("id", "meta.created", "meta.lastModified") def __init__( self, model: type[Resource[Any]], record: type[Any], attributes: Mapping[str, Any], *, version: Any, ) -> None: self.model = model self.record = record self._columns: dict[tuple[type[BaseModel], str, str | None], _Column] = {} self._collections: dict[tuple[type[BaseModel], str], list[_Collection]] = {} declared: dict[tuple[type[BaseModel], str, str | None], str] = {} for path, value in attributes.items(): binding = self._resolve(path) key = _key(binding) for other, other_path in declared.items(): if _overlap(key, other): raise ValueError( f"{other_path!r} and {path!r} map the same attribute" ) declared[key] = path self._add(path, binding, value) for required in self._REQUIRED: if _key(self._resolve(required)) not in declared: raise ValueError(f"The mapping of {model.__name__} needs {required!r}") mapper: Mapper[Any] = inspect(record) columns = getattr(getattr(version, "property", None), "columns", [None]) if mapper.version_id_col is None or mapper.version_id_col is not columns[0]: raise ValueError( f"The version of {model.__name__} must be the version_id_col " f"of the mapper of {record.__name__}" ) self._version: InstrumentedAttribute[Any] = version self._id = self._column("id") column = _table_column(self._id.expression) if column is not None and column.nullable: raise ValueError( "'id' is mapped to a column that can hold no value, " "but each resource needs one" ) self._id.textual = not _holds_text(self._id.expression) self._created = self._column("meta.created") self._last_modified = self._column("meta.lastModified")
[docs] def schemas(self) -> list[Schema]: """Return the schemas of the resources, restricted to what the mapping stores. They hold the mapped attributes only, and an attribute the mapping reads but never writes is ``readOnly``. Publish them, rather than the schemas of :attr:`model`, so that clients know what the server stores: build the provider of the server with :meth:`ScimProvider.from_discovery <scim2_models.ScimProvider.from_discovery>`, and pass it to the storage. :returns: The schema of the resource, then the schemas of its extensions. """ models: list[Any] = [self.model, *self.model.get_extension_models().values()] return [self._restrict(model.to_schema()) for model in models]
def _restrict(self, schema: Schema) -> Schema: """Return a schema with the attributes the mapping stores only.""" attributes = [] for attribute in schema.attributes or []: restricted = self._restrict_attribute( f"{schema.id}:{attribute.name}", attribute ) if restricted is not None: attributes.append(restricted) return schema.model_copy(update={"attributes": attributes}) def _restrict_attribute(self, urn: str, attribute: Any) -> Any: """Return an attribute of a schema as the mapping stores it, or None when it stores nothing of it.""" if attribute.sub_attributes: sub_attributes = [ restricted for sub_attribute in attribute.sub_attributes if ( restricted := self._restrict_attribute( f"{urn}.{sub_attribute.name}", sub_attribute ) ) is not None ] if not sub_attributes: return None written = any( sub_attribute.mutability != Mutability.read_only for sub_attribute in sub_attributes ) return attribute.model_copy( update={ "sub_attributes": sub_attributes, "mutability": attribute.mutability if written else Mutability.read_only, } ) storage = self._storage(urn) if storage is None: return None if storage in (_Storage.written, _Storage.link_type): return attribute if storage == _Storage.ref: linked = self._linked_resource_types(urn) return attribute.model_copy(update={"reference_types": linked}) return attribute.model_copy(update={"mutability": Mutability.read_only}) def _storage(self, urn: str) -> _Storage | None: """Return how the mapping stores the attribute of a URN, or None when it does not store it.""" key = urn.lower() for column in self._columns.values(): if column.binding.urn.lower() == key: return _Storage.written if column.writable else _Storage.read_only for collection in self._all_collections(): if not collection.owned: head = collection.binding.urn.lower() if key == f"{head}.value": return _Storage.written if key in (f"{head}.display", f"{head}.displayname"): return _Storage.derived if key == f"{head}.$ref": return _Storage.ref if key == f"{head}.type" and collection.link_type is not None: return _Storage.link_type continue for column in collection.columns.values(): if column.binding.urn.lower() == key: return _Storage.written if column.writable else _Storage.read_only return None def _nullable(self, urn: str) -> bool: """Tell whether the column of an attribute can hold no value, True for an attribute without column.""" key = urn.lower() return all( column.nullable for column in self._columns.values() if column.binding.urn.lower() == key ) def _linked_resource_types(self, urn: str) -> list[str]: """Return the resource types the Links of an attribute link to, from the URN of its $ref.""" head = urn.lower().removesuffix(".$ref") return [ collection.resource_type for collection in self._all_collections() if collection.binding.urn.lower() == head and collection.resource_type is not None ] def _all_collections(self) -> Iterator[_Collection]: """Yield the collections of every attribute, each Link of an attribute being a collection.""" for collections in self._collections.values(): yield from collections def _column(self, path: str) -> _Column: """Return the column of the attribute at a path.""" return self._columns[_key(self._resolve(path))] def _lookup( self, binding: AttributeBinding ) -> list[tuple[_Collection | None, _Column | None]]: """Return each collection holding an attribute, if any, with the column of the attribute in it. An attribute outside collections gives one pair, without collection. An attribute linking to several resource types gives a pair for each of its Links. The column is None for an attribute stored nowhere, and for a collection named alone, such as emails. """ collections = self._collections.get((binding.model, binding.field_name)) if collections is None: return [(None, self._columns.get(_key(binding)))] return [ (collection, collection.column(binding.sub_field_name)) for collection in collections ] def _sub_columns(self, binding: AttributeBinding) -> list[_Column]: """Return the mapped sub-attributes of a complex attribute holding a single value.""" return [ column for (model, field_name, sub_field_name), column in self._columns.items() if model is binding.model and field_name == binding.field_name and sub_field_name is not None ] def _string_columns(self) -> Iterator[_Column]: """Yield the columns holding strings, those of the collections included.""" for column in self._columns.values(): if column.is_string: yield column for collection in self._all_collections(): for column in collection.columns.values(): if column.is_string: yield column if collection.type_column is not None: yield collection.type_column def _unique_columns(self) -> Iterator[_Column]: """Yield the readable columns whose values no two resources share.""" for column in self._columns.values(): if column.readable and column.binding.get_annotation(Uniqueness) in ( Uniqueness.server, Uniqueness.global_, ): yield column def _returned_collections( self, parameters: ResponseParameters[Any] | None = None ) -> list[_Collection]: """Return the collections a response keeps, all of them without parameters.""" collections = list(self._all_collections()) if parameters is None: return collections paths = Path[self.model].iter_paths( # type: ignore[name-defined] include_subattributes=False, attributes=parameters.attributes, excluded_attributes=parameters.excluded_attributes, ) bindings = (path.resolve() for path in paths) kept = {binding.urn for binding in bindings if binding is not None} return [ collection for collection in collections if collection.binding.urn in kept ] def _loader_options( self, collections: Iterable[_Collection] ) -> list[strategy_options._AbstractLoad]: """Return the options loading collections along with the records. Loading them up front is what lets an asynchronous session read them. """ return [ strategy_options.selectinload(collection.relationship) for collection in collections ] def _targets( self, mappings: Mapping[str, "ResourceMapping"] ) -> list[tuple[_Collection, "ResourceMapping"]]: """Return each Link with the mapping of the resources it links to. Several storages may share a mapping, as long as they link it to the same mappings. """ return [ (collection, self._target(collection, mappings)) for collection in self._all_collections() if not collection.owned ] def _bind(self, links: list[tuple[_Collection, "ResourceMapping"]]) -> None: """Bind each Link to the mapping :meth:`_targets` returned for it.""" for collection, target in links: collection.target = target collection.columns = {} head = collection.binding.urn for binding, column in ( (self._resolve(f"{head}.value"), target._id), ( self._find(f"{head}.display") or self._find(f"{head}.displayName"), target._display(), ), ): if binding is None or column is None: continue collection.columns[binding.sub_field_name] = _Column( binding, column.expression, column.key, column.readable, False, column.textual, ) def _target( self, collection: _Collection, mappings: Mapping[str, "ResourceMapping"] ) -> "ResourceMapping": """Return the mapping a Link links to, refusing one that does not fit it.""" target = mappings.get(collection.resource_type) # type: ignore[arg-type] if target is None: raise ValueError( f"No mapping for the resource type {collection.resource_type!r}" ) if collection.target is not None and collection.target is not target: raise ValueError( f"{collection.binding.urn!r} already links to another mapping " f"of {collection.resource_type!r}: a mapping serves storages " "sharing the same mappings" ) return target def _display(self) -> _Column | None: """Return the column a link to these resources displays.""" for path in ("displayName", "userName"): binding = self._find(path) if binding is None: continue ((_, column),) = self._lookup(binding) if column is not None and column.readable: return column return None def _find(self, path: str) -> AttributeBinding | None: """Return the attribute at a path, or None if the model has none there.""" return Path[self.model](path).resolve() # type: ignore[name-defined] def _resolve(self, path: str) -> AttributeBinding: """Return the attribute at a path, refusing a path the model does not know.""" binding = self._find(path) if binding is None: raise ValueError(f"{path!r} is not an attribute of {self.model.__name__}") return binding def _add(self, path: str, binding: AttributeBinding, value: Any) -> None: """Check the mapping of the attribute at a path, and keep its column or its collections.""" target = binding.target_type is_complex = isclass(target) and issubclass(target, BaseModel) key = (binding.model, binding.field_name) if isinstance(value, Many): self._collections[key] = [self._many(path, binding, value)] return if isinstance(value, Link): value = [value] if isinstance(value, list) and all(isinstance(item, Link) for item in value): if not is_complex or binding.sub_field_name is not None: raise ValueError( f"{path!r} is not a complex attribute of {self.model.__name__}" ) self._collections[key] = self._links(path, binding, value) return if binding.is_multivalued or is_complex: raise ValueError( f"{path!r} holds several values or sub-attributes: map it with Many " "or Link, or map its sub-attributes" ) self._columns[_key(binding)] = self._build_column(path, binding, value) def _collection( self, path: str, binding: AttributeBinding, relationship: Any ) -> _Collection: """Build the collection of a relationship, holding as many records as the attribute holds entries.""" collection = _Collection(binding, relationship) if collection.relationship_property.uselist == collection.single: raise ValueError( f"{path!r} holds {'one entry' if collection.single else 'several entries'}, " f"but its relationship holds {'several' if collection.single else 'one'}" ) return collection def _many(self, path: str, binding: AttributeBinding, many: Many) -> _Collection: """Build the collection of a Many, with the columns of its entries, or the column of its values.""" target = binding.target_type is_complex = isclass(target) and issubclass(target, BaseModel) simple = not isinstance(many.attributes, Mapping) if ( binding.sub_field_name is not None or not binding.is_multivalued or is_complex == simple ): kind = "simple" if simple else "complex" raise ValueError( f"{path!r} is not a multi-valued {kind} attribute of " f"{self.model.__name__}" ) collection = self._collection(path, binding, many.relationship) if not collection.relationship_property.cascade.delete_orphan: raise ValueError( f"The relationship of {path!r} needs the delete-orphan cascade" ) paths: dict[str | None, str] = {} if simple: collection.columns[None] = self._build_column( path, binding, many.attributes ) for sub_path, expression in ({} if simple else many.attributes).items(): sub_binding = self._resolve(f"{path}.{sub_path}") if sub_binding.sub_field_name in paths: raise ValueError( f"{paths[sub_binding.sub_field_name]!r} and " f"'{path}.{sub_path}' map the same attribute" ) paths[sub_binding.sub_field_name] = f"{path}.{sub_path}" collection.columns[sub_binding.sub_field_name] = self._build_column( f"{path}.{sub_path}", sub_binding, expression ) columns = collection.columns.values() if not all( column.readable and not _write_only(column.binding) for column in columns ): raise ValueError( f"Writing {path!r} replaces its entries, " "so they cannot hold write-only values" ) if len({column.writable for column in columns}) > 1: raise ValueError( f"Writing {path!r} replaces its entries, " "so its sub-attributes are either all written or all read-only" ) return collection def _links( self, path: str, binding: AttributeBinding, links: list[Link] ) -> list[_Collection]: """Build the collections of the Links of an attribute, one per resource type. The type of the entries is compared, in filters, as a constant of each Link. """ resource_types = [link.resource_type for link in links] if not links or len(set(resource_types)) != len(resource_types): raise ValueError( f"{path!r} needs one Link per resource type, and at least one" ) if not binding.is_multivalued and len(links) > 1: raise ValueError(f"{path!r} holds one value, so it is mapped to one Link") typed = {link.type is not None for link in links} if len(typed) > 1: raise ValueError(f"Either every Link of {path!r} has a type, or none") type_binding = self._find(f"{path}.type") if typed == {True} and type_binding is None: raise ValueError(f"{path!r} has no type sub-attribute") collections = [] for link in links: collection = self._collection(path, binding, link.relationship) collection.resource_type = link.resource_type if link.type is not None: collection.link_type = link.type collection.type_column = _Column( type_binding, # type: ignore[arg-type] literal(link.type), "type", True, False, ) collections.append(collection) return collections def _build_column( self, path: str, binding: AttributeBinding, value: Any ) -> _Column: """Build the column of an attribute, and decide whether it is written. A read-only attribute, or a hybrid property without a setter, is never written. """ if binding.sub_field_name is not None and binding.target_is_multivalued: raise ValueError( f"{path!r} holds several values in a complex attribute, " "which the storage cannot map" ) attribute = value if isinstance(value, Attribute) else Attribute(value) expression = attribute.expression key = getattr(expression, "key", None) if not isinstance(key, str): raise ValueError( f"{path!r} must be mapped to a column or a hybrid property" ) if attribute.readable and isinstance(getattr(expression, "type", None), Hashed): raise ValueError( f"{path!r} stores a hash, which the storage cannot read back: " "map it with readable=False" ) writable = attribute.writable descriptor = inspect(expression.class_).all_orm_descriptors[key] mutabilities = { binding.model.get_field_annotation(binding.field_name, Mutability), binding.get_annotation(Mutability), } if Mutability.read_only in mutabilities or ( isinstance(descriptor, hybrid_property) and descriptor.fset is None ): writable = False column = _Column(binding, expression, key, attribute.readable, writable) python_type = _python_type(expression) if ( column.is_string and column.enumeration is None and python_type is not None and not issubclass(python_type, (str, bytes)) and _key(binding) != _key(self._resolve("id")) ): raise ValueError( f"{path!r} holds strings, but its column holds " f"{python_type.__name__} values: map it to a property whose " "expression casts the column to a string" ) return column