"""Schema and validator for source environment."""
from typing import Mapping

from google.cloud import bigquery_migration_v2
from marshmallow import Schema, fields, post_load


class SourceEnvSchema(Schema):
    """Schema and validator for SourceEnv."""

    default_database = fields.String()
    schema_search_path = fields.List(fields.String())

    @post_load
    def build(self, data, **kwargs):  # type: ignore[no-untyped-def] # pylint: disable=unused-argument
        return bigquery_migration_v2.SourceEnv(**data)

    @classmethod
    def from_mapping(
        cls, mapping: Mapping[str, object]
    ) -> bigquery_migration_v2.SourceEnv:
        """Factory method for creating a SourceEnv from a Mapping.

        Args:
            mapping: A Mapping of the form:
                {"default_database": "foo", "schema_search_path": ["bar", "baz"]}.

        Returns:
            A SourceEnv instance.
        """
        source_env: bigquery_migration_v2.SourceEnv = cls().load(mapping)
        return source_env
