1
0
mirror of synced 2026-01-01 18:02:53 -05:00
Files
airbyte/airbyte-cdk/python/airbyte_cdk/sources/declarative/interpolation/interpolated_boolean.py
Brian Lai bd31100774 initial first pass converting every component to dataclasses (#15189)
* initial first pass converting every component to dataclasses

* replace the hackier options pass through

* get rid of the hackier way

* fix issues w/ type hints by making options required and lots of fixes to the language to fix compatability for dataclasses

* add dataclasses-jsonschema to setup

* fix oauth authenticator to avoid dataclass name collisions

* fix spacing for CI tests

* remove property from oauth and fix a interpolation bug

* pr feedback and cleaning up the code a bit, attempt at avoiding renaming

* fix templates and bugs surfaced during greenhouse spec testing

* fix tests

* fix missing options in some declarative components

* fix tests related to pulling latest master

* fix issue w/ passing state, slice, and token to subcomponents

* switch name back to get_access_token() since no name collision anymore
2022-08-05 17:39:27 -04:00

48 lines
1.8 KiB
Python

#
# Copyright (c) 2022 Airbyte, Inc., all rights reserved.
#
from dataclasses import InitVar, dataclass
from typing import Any, Final, List, Mapping
from airbyte_cdk.sources.declarative.interpolation.jinja import JinjaInterpolation
from airbyte_cdk.sources.declarative.types import Config
from dataclasses_jsonschema import JsonSchemaMixin
FALSE_VALUES: Final[List[Any]] = ["False", "false", "{}", "[]", "()", "", "0", "0.0", "False", "false", {}, False, [], (), set()]
@dataclass
class InterpolatedBoolean(JsonSchemaMixin):
f"""
Wrapper around a string to be evaluated to a boolean value.
The string will be evaluated as False if it interpolates to a value in {FALSE_VALUES}
Attributes:
condition (str): The string representing the condition to evaluate to a boolean
"""
condition: str
options: InitVar[Mapping[str, Any]]
def __post_init__(self, options: Mapping[str, Any]):
self._default = "False"
self._interpolation = JinjaInterpolation()
self._options = options
def eval(self, config: Config, **additional_options):
"""
Interpolates the predicate condition string using the config and other optional arguments passed as parameter.
:param config: The user-provided configuration as specified by the source's spec
:param additional_options: Optional parameters used for interpolation
:return: The interpolated string
"""
if isinstance(self.condition, bool):
return self.condition
else:
evaluated = self._interpolation.eval(self.condition, config, self._default, options=self._options, **additional_options)
if evaluated in FALSE_VALUES:
return False
# The presence of a value is generally regarded as truthy, so we treat it as such
return True