# This file is auto-generated by oagen. Do not edit.

from __future__ import annotations

from dataclasses import dataclass
from enum import Enum
from typing import Any, Dict, Optional
from workos._types import _raise_deserialize_error
from workos.common.models.user_role_assignment_source_type import (
    UserRoleAssignmentSourceType,
)


@dataclass(slots=True)
class UserRoleAssignmentSource:
    """The origin of the role assignment."""

    type: "UserRoleAssignmentSourceType"
    """Whether the role was assigned directly or derived from a group."""
    group_role_assignment_id: Optional[str]
    """The ID of the group role assignment the role was derived from, or null if direct."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "UserRoleAssignmentSource":
        """Deserialize from a dictionary."""
        try:
            return cls(
                type=UserRoleAssignmentSourceType(data["type"]),
                group_role_assignment_id=data["group_role_assignment_id"],
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("UserRoleAssignmentSource", e)

    def to_dict(self) -> Dict[str, Any]:
        """Serialize to a dictionary."""
        result: Dict[str, Any] = {}
        result["type"] = self.type.value if isinstance(self.type, Enum) else self.type
        if self.group_role_assignment_id is not None:
            result["group_role_assignment_id"] = self.group_role_assignment_id
        else:
            result["group_role_assignment_id"] = None
        return result
