Skip to content

Commit

Permalink
rename to '$options'
Browse files Browse the repository at this point in the history
  • Loading branch information
girarda committed Jul 21, 2022
1 parent be432c3 commit 880e453
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
:param keywords:
:return: partially created object
"""
OPTIONS_STR = "$options"


def create(func, /, *args, **keywords):
Expand All @@ -26,9 +27,9 @@ def newfunc(*fargs, **fkeywords):
# config is a special keyword used for interpolation
config = all_keywords.pop("config", None)

# options is a special keyword used for interpolation and propagation
if "options" in all_keywords:
options = all_keywords.get("options")
# $options is a special keyword used for interpolation and propagation
if "$options" in all_keywords:
options = all_keywords.get("$options")
else:
options = dict()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import importlib
from typing import Any, Mapping, Type, Union, get_args, get_origin, get_type_hints

from airbyte_cdk.sources.declarative.create_partial import create
from airbyte_cdk.sources.declarative.create_partial import OPTIONS_STR, create
from airbyte_cdk.sources.declarative.interpolation.jinja import JinjaInterpolation
from airbyte_cdk.sources.declarative.parsers.class_types_registry import CLASS_TYPES_REGISTRY
from airbyte_cdk.sources.declarative.parsers.default_implementation_registry import DEFAULT_IMPLEMENTATIONS_REGISTRY
Expand Down Expand Up @@ -43,8 +43,8 @@ def build(self, class_or_class_name: Union[str, Type], config, **kwargs):
class_ = class_or_class_name

# create components in options before propagating them
if "options" in kwargs:
kwargs["options"] = {k: self._create_subcomponent(k, v, kwargs, config, class_) for k, v in kwargs["options"].items()}
if OPTIONS_STR in kwargs:
kwargs[OPTIONS_STR] = {k: self._create_subcomponent(k, v, kwargs, config, class_) for k, v in kwargs[OPTIONS_STR].items()}

updated_kwargs = {k: self._create_subcomponent(k, v, kwargs, config, class_) for k, v in kwargs.items()}
return create(class_, config=config, **updated_kwargs)
Expand All @@ -71,11 +71,11 @@ def _create_subcomponent(self, key, definition, kwargs, config, parent_class):
"""
if self.is_object_definition_with_class_name(definition):
# propagate kwargs to inner objects
definition["options"] = self._merge_dicts(kwargs.get("options", dict()), definition.get("options", dict()))
definition[OPTIONS_STR] = self._merge_dicts(kwargs.get(OPTIONS_STR, dict()), definition.get(OPTIONS_STR, dict()))
return self.create_component(definition, config)()
elif self.is_object_definition_with_type(definition):
# If type is set instead of class_name, get the class_name from the CLASS_TYPES_REGISTRY
definition["options"] = self._merge_dicts(kwargs.get("options", dict()), definition.get("options", dict()))
definition[OPTIONS_STR] = self._merge_dicts(kwargs.get(OPTIONS_STR, dict()), definition.get(OPTIONS_STR, dict()))
object_type = definition.pop("type")
class_name = CLASS_TYPES_REGISTRY[object_type]
definition["class_name"] = class_name
Expand All @@ -87,14 +87,14 @@ def _create_subcomponent(self, key, definition, kwargs, config, parent_class):
# We don't have to instantiate builtin types (eg string and dict) because definition is already going to be of that type
if expected_type and not self._is_builtin_type(expected_type):
definition["class_name"] = expected_type
definition["options"] = self._merge_dicts(kwargs.get("options", dict()), definition.get("options", dict()))
definition[OPTIONS_STR] = self._merge_dicts(kwargs.get(OPTIONS_STR, dict()), definition.get(OPTIONS_STR, dict()))
return self.create_component(definition, config)()
else:
return definition
elif isinstance(definition, list):
return [
self._create_subcomponent(
key, sub, self._merge_dicts(kwargs.get("options", dict()), self._get_subcomponent_options(sub)), config, parent_class
key, sub, self._merge_dicts(kwargs.get(OPTIONS_STR, dict()), self._get_subcomponent_options(sub)), config, parent_class
)
for sub in definition
]
Expand All @@ -103,7 +103,7 @@ def _create_subcomponent(self, key, definition, kwargs, config, parent_class):
if expected_type and not isinstance(definition, expected_type):
# call __init__(definition) if definition is not a dict and is not of the expected type
# for instance, to turn a string into an InterpolatedString
options = kwargs.get("options", {})
options = kwargs.get(OPTIONS_STR, {})
try:
# enums can't accept options
if issubclass(expected_type, enum.Enum):
Expand Down Expand Up @@ -148,7 +148,7 @@ def get_default_type(parameter_name, parent_class):
@staticmethod
def _get_subcomponent_options(sub: Any):
if isinstance(sub, dict):
return sub.get("options", {})
return sub.get(OPTIONS_STR, {})
else:
return {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_datetime_stream_slicer():
content = """
stream_slicer:
type: DatetimeStreamSlicer
options:
$options:
datetime_format: "%Y-%m-%d"
start_datetime:
type: MinMaxDatetime
Expand Down Expand Up @@ -195,7 +195,7 @@ def test_full_config():
cursor_field: [ ]
list_stream:
ref: "*ref(partial_stream)"
options:
$options:
name: "lists"
primary_key: "id"
extractor:
Expand Down Expand Up @@ -275,7 +275,7 @@ def test_create_requester():
requester:
type: HttpRequester
path: "/v3/marketing/lists"
options:
$options:
name: lists
url_base: "https://api.sendgrid.com"
authenticator:
Expand Down Expand Up @@ -329,7 +329,7 @@ def test_config_with_defaults():
content = """
lists_stream:
type: "DeclarativeStream"
options:
$options:
name: "lists"
primary_key: id
url_base: "https://api.sendgrid.com"
Expand Down Expand Up @@ -426,7 +426,7 @@ def test_no_transformations(self):
content = f"""
the_stream:
type: DeclarativeStream
options:
$options:
{self.base_options}
"""
config = parser.parse(content)
Expand All @@ -438,7 +438,7 @@ def test_remove_fields(self):
content = f"""
the_stream:
type: DeclarativeStream
options:
$options:
{self.base_options}
transformations:
- type: RemoveFields
Expand All @@ -456,7 +456,7 @@ def test_add_fields(self):
content = f"""
the_stream:
class_name: airbyte_cdk.sources.declarative.declarative_stream.DeclarativeStream
options:
$options:
{self.base_options}
transformations:
- type: AddFields
Expand Down

0 comments on commit 880e453

Please sign in to comment.