-
Notifications
You must be signed in to change notification settings - Fork 30
chore(low code): refactor model to component factory #673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 13 commits
b878987
0c27b96
2d8129b
e1a4e7e
2b569f5
bd551fc
01d2f87
fafab4d
57706a7
bc4a36f
ba33479
3cdd426
06e05b9
7d06719
218dabb
b212ed9
2d4fdae
b9e26bd
1b02cdc
3d48eb0
5e71790
cb93847
906d587
2fca25a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# | ||
# Copyright (c) 2025 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from dataclasses import dataclass | ||
from typing import Any, Callable, Generic, Mapping, Optional, Type, TypeVar | ||
|
||
from pydantic.v1 import BaseModel | ||
|
||
from airbyte_cdk.sources.connector_state_manager import ConnectorStateManager | ||
from airbyte_cdk.sources.declarative.models.declarative_component_schema import ValueType | ||
from airbyte_cdk.sources.message import MessageRepository | ||
from airbyte_cdk.sources.types import Config | ||
|
||
M = TypeVar("M", bound=BaseModel) | ||
|
||
|
||
@dataclass | ||
class AdditionalFlags: | ||
def __init__( | ||
self, | ||
emit_connector_builder_messages: bool, | ||
disable_retries: bool, | ||
message_repository: MessageRepository, | ||
connector_state_manager: ConnectorStateManager, | ||
limit_pages_fetched_per_slice: Optional[int], | ||
limit_slices_fetched: Optional[int], | ||
): | ||
self.emit_connector_builder_messages = emit_connector_builder_messages | ||
self.disable_retries = disable_retries | ||
self.message_repository = message_repository | ||
self.connector_state_manager = connector_state_manager | ||
self.limit_pages_fetched_per_slice = limit_pages_fetched_per_slice | ||
self.limit_slices_fetched = limit_slices_fetched | ||
|
||
@property | ||
def should_limit_slices_fetched(self) -> bool: | ||
""" | ||
Returns True if the number of slices fetched should be limited, False otherwise. | ||
This is used to limit the number of slices fetched during tests. | ||
""" | ||
return bool(self.limit_slices_fetched or self.emit_connector_builder_messages) | ||
|
||
|
||
@dataclass | ||
class ComponentConstructor(Generic[M]): | ||
@classmethod | ||
def resolve_dependencies( | ||
cls, | ||
model: M, | ||
config: Config, | ||
dependency_constructor: Callable[..., Any], | ||
additional_flags: AdditionalFlags, | ||
**kwargs: Any, | ||
) -> Mapping[str, Any]: | ||
""" | ||
Resolves the component's dependencies, this method should be created in the component, | ||
if there are any dependencies on other components, or we need to adopt / change / adjust / fine-tune | ||
specific component's behavior. | ||
""" | ||
return {} | ||
|
||
@classmethod | ||
def build( | ||
cls, | ||
model: M, | ||
config: Config, | ||
dependency_constructor: Callable[..., Any], | ||
additional_flags: AdditionalFlags, | ||
**kwargs: Any, | ||
) -> "ComponentConstructor[M]": | ||
""" | ||
Builds up the Component and it's component-specific dependencies. | ||
Order of operations: | ||
- build the dependencies first | ||
- build the component with the resolved dependencies | ||
""" | ||
|
||
# resolve the component dependencies first | ||
resolved_dependencies: Mapping[str, Any] = cls.resolve_dependencies( | ||
model=model, | ||
config=config, | ||
dependency_constructor=dependency_constructor, | ||
additional_flags=additional_flags, | ||
**kwargs, | ||
) | ||
|
||
# returns the instance of the component class, | ||
# with resolved dependencies and model-specific arguments. | ||
return cls(**resolved_dependencies) | ||
|
||
@staticmethod | ||
def _json_schema_type_name_to_type(value_type: Optional[ValueType]) -> Optional[Type[Any]]: | ||
|
||
if not value_type: | ||
return None | ||
names_to_types = { | ||
ValueType.string: str, | ||
ValueType.number: float, | ||
ValueType.integer: int, | ||
ValueType.boolean: bool, | ||
} | ||
return names_to_types[value_type] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to better understand the pattern: In an ideal world where this isn't a dataclass that exposes everything as public, would the logic of having the condition as
InterpolatedBoolean
be done inresolve_dependencies
or would we still have this in the init?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think for now we will still have this in
__init__
just to have less thing to refactor and avoid missing something. But it's a good idea and I think we consider changing the place of interpolation here or in__init__