Skip to content

graflo.architecture.graph_types.identifiers

Graph identifier aliases and edge-key serialization.

Attributes

EdgeId = tuple[str, str, str | None] module-attribute

EdgePhysicalKey = tuple[str, str, str | None, str | None] module-attribute

GraphEntity = str | EdgeId module-attribute

VertexName = str module-attribute

Functions:

deserialize_edge_key(key)

Deserialize a JSON-array edge key back to an edge id tuple.

Source code in graflo/architecture/graph_types/identifiers.py
def deserialize_edge_key(key: str) -> tuple[str, str, str | None]:
    """Deserialize a JSON-array edge key back to an edge id tuple."""
    try:
        parsed = json.loads(key)
    except json.JSONDecodeError as exc:
        raise ValueError(f"Invalid edge key JSON: {key!r}") from exc
    if not isinstance(parsed, list) or len(parsed) != 3:
        raise ValueError(f"Invalid edge key JSON array: {key!r}")
    if not all(isinstance(part, str) for part in parsed[:2]):
        raise ValueError(f"Invalid edge key JSON array: {key!r}")
    relation_part = parsed[2]
    if relation_part is not None and not isinstance(relation_part, str):
        raise ValueError(f"Invalid edge key JSON array: {key!r}")
    relation = relation_part if relation_part else None
    return (parsed[0], parsed[1], relation)

deserialize_entity_key(key)

Source code in graflo/architecture/graph_types/identifiers.py
def deserialize_entity_key(key: str | tuple[str, str, str | None]) -> GraphEntity:
    if isinstance(key, tuple):
        return key
    if key.startswith("["):
        return deserialize_edge_key(key)
    return key

serialize_edge_key(edge_id)

Serialize an edge id tuple to a JSON-safe string key.

Uses a JSON array [source, target, relation] so vertex or relation names may contain | or other special characters without ambiguity.

Source code in graflo/architecture/graph_types/identifiers.py
def serialize_edge_key(edge_id: tuple[str, str, str | None]) -> str:
    """Serialize an edge id tuple to a JSON-safe string key.

    Uses a JSON array ``[source, target, relation]`` so vertex or relation names
    may contain ``|`` or other special characters without ambiguity.
    """
    source, target, relation = edge_id
    return json.dumps([source, target, relation], separators=(",", ":"))

serialize_entity_key(key)

Source code in graflo/architecture/graph_types/identifiers.py
def serialize_entity_key(key: GraphEntity) -> str:
    if isinstance(key, str):
        return key
    return serialize_edge_key(key)