Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion dbt_loom/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List, Union
from urllib.parse import ParseResult, urlparse

from pydantic import BaseModel, Field, validator
from pydantic import BaseModel, Field, field_validator, validator

from dbt_loom.clients.az_blob import AzureReferenceConfig
from dbt_loom.clients.dbt_cloud import DbtCloudReferenceConfig
Expand Down Expand Up @@ -49,6 +49,18 @@ def default_path(cls, v, values) -> ParseResult:
return urlparse(Path(v).absolute().as_uri())


_TYPE_TO_CONFIG = {
ManifestReferenceType.file: FileReferenceConfig,
ManifestReferenceType.dbt_cloud: DbtCloudReferenceConfig,
ManifestReferenceType.paradime: ParadimeReferenceConfig,
ManifestReferenceType.gcs: GCSReferenceConfig,
ManifestReferenceType.s3: S3ReferenceConfig,
ManifestReferenceType.azure: AzureReferenceConfig,
ManifestReferenceType.snowflake: SnowflakeReferenceConfig,
ManifestReferenceType.databricks: DatabricksReferenceConfig,
}


class ManifestReference(BaseModel):
"""Reference information for a manifest to be loaded into dbt-loom."""

Expand All @@ -67,6 +79,25 @@ class ManifestReference(BaseModel):
excluded_packages: List[str] = Field(default_factory=list)
optional: bool = False

@field_validator("config", mode="before")
@classmethod
def resolve_config_type(cls, v, info):
"""Resolve the config to the correct type based on the manifest
reference type.

Pydantic v1 Union resolution tries each type in order and returns the
first match. This can cause configs meant for other types (e.g.,
DatabricksReferenceConfig) to be incorrectly parsed as
FileReferenceConfig when both have a `path` field. This validator
explicitly constructs the correct config class based on `type`.
"""
ref_type = info.data.get("type")
if ref_type is not None and isinstance(v, dict):
config_cls = _TYPE_TO_CONFIG.get(ref_type)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Praise: I quite like the explicit factory approach for picking the config type. Thanks for adding this.

if config_cls is not None:
return config_cls(**v)
return v


class dbtLoomConfig(BaseModel):
"""Configuration for dbt Loom"""
Expand Down
22 changes: 22 additions & 0 deletions tests/test_manifest_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ManifestReferenceType,
LoomConfigurationError,
)
from dbt_loom.clients.dbx import DatabricksReferenceConfig
from dbt_loom.manifests import ManifestLoader, UnknownManifestPathType


Expand Down Expand Up @@ -130,3 +131,24 @@ def test_load_from_local_filesystem_not_optional_missing():
manifest_loader = ManifestLoader()
with pytest.raises(LoomConfigurationError):
manifest_loader.load(manifest_reference)


def test_manifest_reference_resolves_databricks_config():

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Praise: Kudos for adding tests.

"""Verify that type=databricks produces DatabricksReferenceConfig, not FileReferenceConfig."""
ref = ManifestReference(
name="test_dbx",
type=ManifestReferenceType.databricks,
config={"path": "/Volumes/my_catalog/my_schema/my_volume/manifest.json.gz"},
)
assert isinstance(ref.config, DatabricksReferenceConfig)
assert ref.config.path == "/Volumes/my_catalog/my_schema/my_volume/manifest.json.gz"


def test_manifest_reference_resolves_file_config():
"""Verify that type=file still produces FileReferenceConfig."""
ref = ManifestReference(
name="test_file",
type=ManifestReferenceType.file,
config={"path": "manifest.json"},
)
assert isinstance(ref.config, FileReferenceConfig)
Loading