Skip to content

Commit

Permalink
main: add tagging support to table model
Browse files Browse the repository at this point in the history
  • Loading branch information
balinthaller committed Apr 7, 2021
1 parent ea7b0bd commit 59e8fbc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
43 changes: 35 additions & 8 deletions carte_cli/model/carte_table_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ def __repr__(self) -> str:
)


class TableTag:
def __init__(self, key: str, value: str):
self.key = key
self.value = value

@classmethod
def from_frontmatter(cls, meta_dict: dict):
return cls(key=meta_dict["key"], value=meta_dict["value"])

def to_frontmatter(self):
return {"key": self.key, "value": self.value}

def __repr__(self) -> str:
return "TableTag({!r}: {!r})".format(self.key, self.value)


class TableMetadata:
def __init__(
self,
Expand All @@ -89,6 +105,7 @@ def __init__(
location: str,
columns: List[ColumnMetadata],
table_type: TableType,
tags: List[TableTag] = [],
description: str = None,
):
self.name = name
Expand All @@ -97,6 +114,7 @@ def __init__(
self.description = description
self.location = location
self.columns = columns
self.tags = tags
self.table_type = table_type

@classmethod
Expand All @@ -114,6 +132,7 @@ def from_databuilder(cls, table: DatabuilderTableMetadata):
else None
),
columns=columns,
tags=[],
table_type=(TableType.VIEW if table.is_view else TableType.TABLE),
)

Expand All @@ -124,6 +143,8 @@ def from_frontmatter(cls, metadata, content):
for col_dict in metadata.get("columns", [])
]

tags = [TableTag.from_frontmatter(tag) for tag in metadata.get("tags", [])]

try:
return cls(
name=metadata["title"],
Expand All @@ -132,6 +153,7 @@ def from_frontmatter(cls, metadata, content):
location=metadata.get("location", None),
connection=metadata.get("connection", None),
columns=columns,
tags=tags,
table_type=TableType(metadata.get("table_type", "table")),
)
except KeyError as e:
Expand All @@ -144,6 +166,7 @@ def to_frontmatter(self):
"location": self.location,
"database": self.database,
"columns": [col.to_frontmatter() for col in self.columns],
"tags": [tag.to_frontmatter() for tag in self.tags],
"table_type": self.table_type.value,
}
return metadata, self.description
Expand Down Expand Up @@ -189,19 +212,23 @@ def merge_with_existing(self, existing):
description=existing.description,
location=self.location,
columns=self.merge_columns(existing),
tags=existing.tags,
table_type=self.table_type,
)

def get_file_name(self):
return f"{self.connection}/{self.database}/{self.name}"

def __repr__(self) -> str:
return "CarteTableMetadata({!r}, {!r}, {!r}, {!r}, {!r}, {!r}, {!r})".format(
self.name,
self.database,
self.connection,
self.description,
self.location,
self.columns,
self.table_type,
return (
"CarteTableMetadata({!r}, {!r}, {!r}, {!r}, {!r}, {!r}, {!r}, {!r})".format(
self.name,
self.database,
self.connection,
self.description,
self.location,
self.columns,
self.tags,
self.table_type,
)
)
6 changes: 6 additions & 0 deletions tests/model/test_carte_table_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def test_from_databuilder(self):
assert result.database == "test-cluster"
assert result.location == "test-connection://test-cluster.test-db/test-name"
assert result.connection == "test-connection"
assert len(result.tags) == 0
assert len(result.columns) == 2
assert (
result.columns[0].__repr__()
Expand All @@ -102,6 +103,7 @@ def test_from_frontmatter(self):
"location": "test-location",
"connection": "test-connection",
"columns": [],
"tags": [{"key": "a", "value": "val1"}, {"key": "b", "value": "val2"}],
"table_type": "table",
}

Expand All @@ -113,6 +115,10 @@ def test_from_frontmatter(self):
assert result.connection == "test-connection"
assert result.columns == []
assert result.table_type == TableType.TABLE
assert result.tags[0].key == "a"
assert result.tags[0].value == "val1"
assert result.tags[1].key == "b"
assert result.tags[1].value == "val2"

def test_from_frontmatter_no_values(self):
source_metadata = {"title": "test-name"}
Expand Down

0 comments on commit 59e8fbc

Please sign in to comment.