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: 31 additions & 2 deletions src/kat_transform/metadata.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
class FieldMetadata:
import typing

T = typing.TypeVar("T", bound="Metadata")


class Metadata:
def __init__(self) -> None:
self.entries: tuple[Metadata, ...] = (self,)

def get_metadata(self, metadata_type: type[T], exact: bool = False) -> T | None:
for entry in self.entries:
if type(entry) is metadata_type:
return entry

if not exact and isinstance(entry, metadata_type):
return entry

return None

def __or__(self, other: "Metadata") -> "CombinedMetadata":
return CombinedMetadata(*self.entries, *other.entries)


class CombinedMetadata(Metadata):
def __init__(self, *entries: Metadata):
super().__init__()
self.entries: tuple[Metadata, ...] = entries


class FieldMetadata(Metadata):
"""
Field metadata that can be used by other tools (like json schema generation)

This is fully customisable and up to you, how to and for what use it
"""


class SchemaMetadata:
class SchemaMetadata(Metadata):
"""
Schema metadata that can be used by other tools (like json schema generation)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from kat_transform.metadata import Metadata


def test_combination():
class CustomMeta(Metadata): ...

custom = CustomMeta()
empty = Metadata()

combined = custom | empty
assert combined.entries == (custom, empty)

assert combined.get_metadata(CustomMeta) is custom

assert combined.get_metadata(Metadata) is custom
assert combined.get_metadata(Metadata, exact=True) is empty