Skip to content

Commit

Permalink
Move stableTargetId typehint to extractedData
Browse files Browse the repository at this point in the history
  • Loading branch information
cutoffthetop committed Feb 14, 2024
1 parent 2619f1a commit c2767d2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
17 changes: 2 additions & 15 deletions mex/common/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,26 +214,13 @@ def __str__(self) -> str:
class MExModel(BaseModel):
"""Abstract base model for extracted data, merged item and rule set classes.
This class defines an `identifier` field and gives type hints for `stableTargetId`
and the class variable `entityType`.
This class defines an `identifier` field and gives a type hint for the frozen class
variable `entityType`.
"""

model_config = ConfigDict(extra="forbid")

if TYPE_CHECKING:
# Sometimes multiple primary sources describe the same activity, resource, etc.
# and a complete metadata item can only be created by merging these fragments.
# The `stableTargetID` is part of all models in `mex.common.models` to allow
# MEx to identify which extracted items describe the same thing and should be
# merged to create a complete metadata item.
# The name might be a bit misleading (also due to historical reasons), but the
# "stability" is only guaranteed for one "real world" or "digital world" thing
# having the same ID in MEx over time. But not as a guarantee, that the same
# metadata sources contribute to the complete metadata item.
# Because we anticipate that items have to be merged, the `stableTargetID` is
# also used as the foreign key for all fields containing references.
stableTargetId: Any

# We add the entityType as a final class variable to all `MExModel` subclasses.
# This helps with assigning the correct class when reading raw JSON entities.
# Simple duck-typing would not work, because some entity types have overlapping
Expand Down
16 changes: 15 additions & 1 deletion mex/common/models/extracted_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Annotated, Any
from typing import TYPE_CHECKING, Annotated, Any

from pydantic import Field, model_validator, validate_call

Expand All @@ -23,6 +23,20 @@ class ExtractedData(MExModel):
See below, for a full description.
"""

if TYPE_CHECKING:
# Sometimes multiple primary sources describe the same activity, resource, etc.
# and a complete metadata item can only be created by merging these fragments.
# The `stableTargetID` is part of all models in `mex.common.models` to allow
# MEx to identify which extracted items describe the same thing and should be
# merged to create a complete metadata item.
# The name might be a bit misleading (also due to historical reasons), but the
# "stability" is only guaranteed for one "real world" or "digital world" thing
# having the same ID in MEx over time. But not as a guarantee, that the same
# metadata sources contribute to the complete metadata item.
# Because we anticipate that items have to be merged, the `stableTargetID` is
# also used as the foreign key for all fields containing references.
stableTargetId: Any

hadPrimarySource: Annotated[
PrimarySourceID,
Field(
Expand Down
7 changes: 6 additions & 1 deletion mex/common/public_api/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
MERGED_MODEL_CLASSES_BY_NAME,
MExModel,
)
from mex.common.models.extracted_data import ExtractedData
from mex.common.public_api.models import (
PublicApiField,
PublicApiFieldValueTypes,
Expand Down Expand Up @@ -54,7 +55,11 @@ def transform_mex_model_to_public_api_item(model: MExModel) -> PublicApiItem:
)
return PublicApiItem(
entityType=model.__class__.__name__,
businessId=model.stableTargetId,
businessId=(
model.stableTargetId
if isinstance(model, ExtractedData)
else model.identifier
),
values=api_values,
)

Expand Down

0 comments on commit c2767d2

Please sign in to comment.