Skip to content

Commit

Permalink
Rename to ObjectBuilderTextProcessor.
Browse files Browse the repository at this point in the history
  • Loading branch information
plypaul committed Aug 8, 2024
1 parent 261e609 commit b566c14
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from abc import ABC, abstractmethod

from dbt_semantic_interfaces.parsing.text_input.ti_description import (
QueryItemDescription,
ObjectBuilderItemDescription,
)


Expand All @@ -22,6 +22,6 @@ class QueryItemDescriptionRenderer(ABC):
"""

@abstractmethod
def render_description(self, item_description: QueryItemDescription) -> str:
def render_description(self, item_description: ObjectBuilderItemDescription) -> str:
"""Return the string that will be substituted for the query item in the Jinja template."""
raise NotImplementedError
14 changes: 7 additions & 7 deletions dbt_semantic_interfaces/parsing/text_input/rendering_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from typing_extensions import override

from dbt_semantic_interfaces.parsing.text_input.ti_description import (
ObjectBuilderItemDescription,
ObjectBuilderMethod,
QueryItemDescription,
QueryItemType,
)
from dbt_semantic_interfaces.parsing.text_input.ti_exceptions import (
Expand Down Expand Up @@ -56,7 +56,7 @@ def _create(name: str, entity_path: Sequence[str] = ()) -> _RenderingClassForJin
return _RenderingClassForJinjaTemplate(
description_processor=description_processor,
allowed_methods=allowed_methods,
initial_item_description=QueryItemDescription(
initial_item_description=ObjectBuilderItemDescription(
item_type=item_type,
item_name=name,
entity_path=tuple(entity_path),
Expand Down Expand Up @@ -85,7 +85,7 @@ def _create(
return _RenderingClassForJinjaTemplate(
description_processor=description_processor,
allowed_methods=allowed_methods,
initial_item_description=QueryItemDescription(
initial_item_description=ObjectBuilderItemDescription(
item_type=item_type,
item_name=time_dimension_name,
entity_path=tuple(entity_path),
Expand All @@ -108,7 +108,7 @@ def _create(entity_name: str, entity_path: Sequence[str] = ()) -> _RenderingClas
return _RenderingClassForJinjaTemplate(
description_processor=description_processor,
allowed_methods=allowed_methods,
initial_item_description=QueryItemDescription(
initial_item_description=ObjectBuilderItemDescription(
item_type=item_type,
item_name=entity_name,
entity_path=tuple(entity_path),
Expand All @@ -131,7 +131,7 @@ def _create(metric_name: str, group_by: Sequence[str] = ()) -> _RenderingClassFo
return _RenderingClassForJinjaTemplate(
description_processor=description_processor,
allowed_methods=allowed_methods,
initial_item_description=QueryItemDescription(
initial_item_description=ObjectBuilderItemDescription(
item_type=item_type,
item_name=metric_name,
entity_path=(),
Expand Down Expand Up @@ -161,7 +161,7 @@ def __init__(
self,
description_processor: QueryItemDescriptionProcessor,
allowed_methods: FrozenSet[ObjectBuilderMethod],
initial_item_description: QueryItemDescription,
initial_item_description: ObjectBuilderItemDescription,
) -> None:
"""Initializer.
Expand All @@ -178,7 +178,7 @@ def __init__(
def _update_current_description(
self,
builder_method: ObjectBuilderMethod,
new_description: QueryItemDescription,
new_description: ObjectBuilderItemDescription,
) -> None:
if builder_method not in self._allowed_builder_methods:
raise InvalidBuilderMethodException(
Expand Down
12 changes: 6 additions & 6 deletions dbt_semantic_interfaces/parsing/text_input/ti_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


@dataclass(frozen=True)
class QueryItemDescription:
class ObjectBuilderItemDescription:
"""Describes a query item specified by the user.
For example, the following specified in an order-by of a saved query:
Expand All @@ -21,7 +21,7 @@ class QueryItemDescription:
->
QueryItemDescription(
ObjectBuilderItemDescription(
item_type=GroupByItemType.DIMENSION,
item_name="user__created_at",
entity_path=['listing'],
Expand Down Expand Up @@ -101,9 +101,9 @@ def create_modified(
time_granularity_name: Optional[str] = None,
date_part_name: Optional[str] = None,
descending: Optional[bool] = None,
) -> QueryItemDescription:
) -> ObjectBuilderItemDescription:
"""Create one with the same fields as self except the ones provided."""
return QueryItemDescription(
return ObjectBuilderItemDescription(
item_type=self.item_type,
item_name=self.item_name,
entity_path=self.entity_path,
Expand All @@ -113,9 +113,9 @@ def create_modified(
descending=descending or self.descending,
)

def with_descending_unset(self) -> QueryItemDescription:
def with_descending_unset(self) -> ObjectBuilderItemDescription:
"""Return this with the `descending` field set to None."""
return QueryItemDescription(
return ObjectBuilderItemDescription(
item_type=self.item_type,
item_name=self.item_name,
entity_path=self.entity_path,
Expand Down
29 changes: 17 additions & 12 deletions dbt_semantic_interfaces/parsing/text_input/ti_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,29 @@
ObjectBuilderJinjaRenderHelper,
)
from dbt_semantic_interfaces.parsing.text_input.ti_description import (
QueryItemDescription,
ObjectBuilderItemDescription,
)
from dbt_semantic_interfaces.parsing.text_input.ti_exceptions import (
QueryItemJinjaException,
)
from dbt_semantic_interfaces.parsing.text_input.valid_method import ValidMethodMapping


class QueryItemTextProcessor:
class ObjectBuilderTextProcessor:
"""Performs processing actions for text containing query items specified in the object-builder syntax.
This currently supports:
* Collecting `QueryItemDescription`s from a Jinja template.
* Collecting `ObjectBuilderItemDescription`s from a Jinja template.
* Rendering a Jinja template using a specified renderer.
"""

def get_description(self, query_item_input: str, valid_method_mapping: ValidMethodMapping) -> QueryItemDescription:
"""Get the `QueryItemDescription` for a single item e.g. `Dimension('listing__country').descending(True)`."""
def get_description(
self, query_item_input: str, valid_method_mapping: ValidMethodMapping
) -> ObjectBuilderItemDescription:
"""Get the `ObjectBuilderItemDescription` for a single item.
e.g. `Dimension('listing__country').descending(True)`.
"""
descriptions = self.collect_descriptions_from_template(
jinja_template="{{ " + query_item_input + " }}",
valid_method_mapping=valid_method_mapping,
Expand All @@ -49,8 +54,8 @@ def collect_descriptions_from_template(
self,
jinja_template: str,
valid_method_mapping: ValidMethodMapping,
) -> Sequence[QueryItemDescription]:
"""Returns the `QueryItemDescription`s that are found in a Jinja template.
) -> Sequence[ObjectBuilderItemDescription]:
"""Returns the `ObjectBuilderItemDescription`s that are found in a Jinja template.
Args:
jinja_template: A Jinja-template string like `{{ Dimension('listing__country') }} = 'US'`.
Expand Down Expand Up @@ -135,7 +140,7 @@ class QueryItemDescriptionProcessor(ABC):
"""General processor that does something to a query-item description seen in a Jinja template."""

@abstractmethod
def process_description(self, item_description: QueryItemDescription) -> str:
def process_description(self, item_description: ObjectBuilderItemDescription) -> str:
"""Process the given description, and return a string that would be substituted into the Jinja template."""
raise NotImplementedError

Expand All @@ -144,14 +149,14 @@ class _CollectDescriptionProcessor(QueryItemDescriptionProcessor):
"""Processor that collects all descriptions that were processed."""

def __init__(self) -> None: # noqa: D107
self._items: List[QueryItemDescription] = []
self._items: List[ObjectBuilderItemDescription] = []

def collected_descriptions(self) -> Sequence[QueryItemDescription]:
def collected_descriptions(self) -> Sequence[ObjectBuilderItemDescription]:
"""Return all descriptions that were processed so far."""
return self._items

@override
def process_description(self, item_description: QueryItemDescription) -> str:
def process_description(self, item_description: ObjectBuilderItemDescription) -> str:
if item_description not in self._items:
self._items.append(item_description)

Expand All @@ -169,5 +174,5 @@ def __init__(self, renderer: QueryItemDescriptionRenderer) -> None: # noqa: D10
self._renderer = renderer

@override
def process_description(self, item_description: QueryItemDescription) -> str:
def process_description(self, item_description: ObjectBuilderItemDescription) -> str:
return self._renderer.render_description(item_description)
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
)
from dbt_semantic_interfaces.enum_extension import assert_values_exhausted
from dbt_semantic_interfaces.parsing.text_input.ti_description import (
QueryItemDescription,
ObjectBuilderItemDescription,
QueryItemType,
)
from dbt_semantic_interfaces.parsing.text_input.ti_processor import (
QueryItemTextProcessor,
ObjectBuilderTextProcessor,
)
from dbt_semantic_interfaces.parsing.text_input.valid_method import (
ConfiguredValidMethodMapping,
Expand All @@ -26,9 +26,9 @@ class WhereFilterParser:
"""Parses the template in the WhereFilter into FilterCallParameterSets."""

@staticmethod
def parse_item_descriptions(where_sql_template: str) -> Sequence[QueryItemDescription]:
def parse_item_descriptions(where_sql_template: str) -> Sequence[ObjectBuilderItemDescription]:
"""Parses the filter and returns the item descriptions."""
text_processor = QueryItemTextProcessor()
text_processor = ObjectBuilderTextProcessor()

try:
return text_processor.collect_descriptions_from_template(
Expand Down
10 changes: 5 additions & 5 deletions dbt_semantic_interfaces/validations/saved_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from dbt_semantic_interfaces.call_parameter_sets import FilterCallParameterSets
from dbt_semantic_interfaces.naming.keywords import METRIC_TIME_ELEMENT_NAME
from dbt_semantic_interfaces.parsing.text_input.ti_description import (
QueryItemDescription,
ObjectBuilderItemDescription,
QueryItemType,
)
from dbt_semantic_interfaces.parsing.text_input.ti_processor import (
QueryItemTextProcessor,
ObjectBuilderTextProcessor,
)
from dbt_semantic_interfaces.parsing.text_input.valid_method import (
ConfiguredValidMethodMapping,
Expand Down Expand Up @@ -142,7 +142,7 @@ def _check_where(saved_query: SavedQuery) -> Sequence[ValidationIssue]:
@staticmethod
def _parse_query_item(
saved_query: SavedQuery,
text_processor: QueryItemTextProcessor,
text_processor: ObjectBuilderTextProcessor,
query_item_input: str,
element_type: SavedQueryElementType,
valid_method_mapping: ValidMethodMapping,
Expand Down Expand Up @@ -184,7 +184,7 @@ def _check_order_by(saved_query: SavedQuery) -> Sequence[ValidationIssue]:
return validation_issues

valid_query_item_descriptions = set()
text_processor = QueryItemTextProcessor()
text_processor = ObjectBuilderTextProcessor()
for metric in saved_query.query_params.metrics:
# In an order-by, a metric is specified as "Metric('bookings')" while in the metrics section, it's only the
# metric name.
Expand Down Expand Up @@ -299,7 +299,7 @@ def validate_manifest(semantic_manifest: SemanticManifestT) -> Sequence[Validati
class _ParseQueryItemResult:
"""Result of parsing a string like `Dimension('listing__country')`."""

item_description: Optional[QueryItemDescription]
item_description: Optional[ObjectBuilderItemDescription]
validation_issue: Optional[ValidationIssue]

def __post_init__(self) -> None:
Expand Down

0 comments on commit b566c14

Please sign in to comment.