forked from datahub-project/datahub
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(sdk/patch): improve patch implementation internals (datahub-…
- Loading branch information
Showing
21 changed files
with
535 additions
and
992 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
79 changes: 79 additions & 0 deletions
79
metadata-ingestion/src/datahub/specific/aspect_helpers/custom_properties.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from abc import abstractmethod | ||
from typing import Dict, Optional, Tuple | ||
|
||
from typing_extensions import Self | ||
|
||
from datahub.emitter.mcp_patch_builder import MetadataPatchProposal, PatchPath | ||
|
||
|
||
class HasCustomPropertiesPatch(MetadataPatchProposal): | ||
@classmethod | ||
@abstractmethod | ||
def _custom_properties_location(self) -> Tuple[str, PatchPath]: | ||
... | ||
|
||
def add_custom_property(self, key: str, value: str) -> Self: | ||
"""Add a custom property to the entity. | ||
Args: | ||
key: The key of the custom property. | ||
value: The value of the custom property. | ||
Returns: | ||
The patch builder instance. | ||
""" | ||
aspect_name, path = self._custom_properties_location() | ||
self._add_patch( | ||
aspect_name, | ||
"add", | ||
path=(*path, key), | ||
value=value, | ||
) | ||
return self | ||
|
||
def add_custom_properties( | ||
self, custom_properties: Optional[Dict[str, str]] = None | ||
) -> Self: | ||
if custom_properties is not None: | ||
for key, value in custom_properties.items(): | ||
self.add_custom_property(key, value) | ||
return self | ||
|
||
def remove_custom_property(self, key: str) -> Self: | ||
"""Remove a custom property from the entity. | ||
Args: | ||
key: The key of the custom property to remove. | ||
Returns: | ||
The patch builder instance. | ||
""" | ||
aspect_name, path = self._custom_properties_location() | ||
self._add_patch( | ||
aspect_name, | ||
"remove", | ||
path=(*path, key), | ||
value={}, | ||
) | ||
return self | ||
|
||
def set_custom_properties(self, custom_properties: Dict[str, str]) -> Self: | ||
"""Sets the custom properties of the entity. | ||
This method replaces all existing custom properties with the given dictionary. | ||
Args: | ||
custom_properties: A dictionary containing the custom properties to be set. | ||
Returns: | ||
The patch builder instance. | ||
""" | ||
|
||
aspect_name, path = self._custom_properties_location() | ||
self._add_patch( | ||
aspect_name, | ||
"add", | ||
path=path, | ||
value=custom_properties, | ||
) | ||
return self |
67 changes: 67 additions & 0 deletions
67
metadata-ingestion/src/datahub/specific/aspect_helpers/ownership.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from typing import List, Optional | ||
|
||
from typing_extensions import Self | ||
|
||
from datahub.emitter.mcp_patch_builder import MetadataPatchProposal | ||
from datahub.metadata.schema_classes import ( | ||
OwnerClass, | ||
OwnershipClass, | ||
OwnershipTypeClass, | ||
) | ||
|
||
|
||
class HasOwnershipPatch(MetadataPatchProposal): | ||
def add_owner(self, owner: OwnerClass) -> Self: | ||
"""Add an owner to the entity. | ||
Args: | ||
owner: The Owner object to add. | ||
Returns: | ||
The patch builder instance. | ||
""" | ||
self._add_patch( | ||
OwnershipClass.ASPECT_NAME, | ||
"add", | ||
path=("owners", owner.owner, str(owner.type)), | ||
value=owner, | ||
) | ||
return self | ||
|
||
def remove_owner( | ||
self, owner: str, owner_type: Optional[OwnershipTypeClass] = None | ||
) -> Self: | ||
"""Remove an owner from the entity. | ||
If owner_type is not provided, the owner will be removed regardless of ownership type. | ||
Args: | ||
owner: The owner to remove. | ||
owner_type: The ownership type of the owner (optional). | ||
Returns: | ||
The patch builder instance. | ||
""" | ||
self._add_patch( | ||
OwnershipClass.ASPECT_NAME, | ||
"remove", | ||
path=("owners", owner) + ((str(owner_type),) if owner_type else ()), | ||
value=owner, | ||
) | ||
return self | ||
|
||
def set_owners(self, owners: List[OwnerClass]) -> Self: | ||
"""Set the owners of the entity. | ||
This will effectively replace all existing owners with the new list - it doesn't really patch things. | ||
Args: | ||
owners: The list of owners to set. | ||
Returns: | ||
The patch builder instance. | ||
""" | ||
self._add_patch( | ||
OwnershipClass.ASPECT_NAME, "add", path=("owners",), value=owners | ||
) | ||
return self |
72 changes: 72 additions & 0 deletions
72
metadata-ingestion/src/datahub/specific/aspect_helpers/structured_properties.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from typing import List, Union | ||
|
||
from typing_extensions import Self | ||
|
||
from datahub.emitter.mcp_patch_builder import MetadataPatchProposal | ||
from datahub.metadata.schema_classes import ( | ||
StructuredPropertiesClass, | ||
StructuredPropertyValueAssignmentClass, | ||
) | ||
from datahub.utilities.urns.structured_properties_urn import ( | ||
make_structured_property_urn, | ||
) | ||
|
||
|
||
class HasStructuredPropertiesPatch(MetadataPatchProposal): | ||
def set_structured_property( | ||
self, key: str, value: Union[str, float, List[Union[str, float]]] | ||
) -> Self: | ||
"""Add or update a structured property. | ||
Args: | ||
key: the name of the property (either bare or urn form) | ||
value: the value of the property (for multi-valued properties, this can be a list) | ||
Returns: | ||
The patch builder instance. | ||
""" | ||
self.remove_structured_property(key) | ||
self.add_structured_property(key, value) | ||
return self | ||
|
||
def remove_structured_property(self, key: str) -> Self: | ||
"""Remove a structured property. | ||
Args: | ||
key: the name of the property (either bare or urn form) | ||
Returns: | ||
The patch builder instance. | ||
""" | ||
|
||
self._add_patch( | ||
StructuredPropertiesClass.ASPECT_NAME, | ||
"remove", | ||
path=("properties", make_structured_property_urn(key)), | ||
value={}, | ||
) | ||
return self | ||
|
||
def add_structured_property( | ||
self, key: str, value: Union[str, float, List[Union[str, float]]] | ||
) -> Self: | ||
"""Add a structured property. | ||
Args: | ||
key: the name of the property (either bare or urn form) | ||
value: the value of the property (for multi-valued properties, this value will be appended to the list) | ||
Returns: | ||
The patch builder instance. | ||
""" | ||
|
||
self._add_patch( | ||
StructuredPropertiesClass.ASPECT_NAME, | ||
"add", | ||
path=("properties", make_structured_property_urn(key)), | ||
value=StructuredPropertyValueAssignmentClass( | ||
propertyUrn=make_structured_property_urn(key), | ||
values=value if isinstance(value, list) else [value], | ||
), | ||
) | ||
return self |
Oops, something went wrong.