|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Airbyte, Inc., all rights reserved. |
| 3 | +# |
| 4 | + |
| 5 | +from dataclasses import dataclass, field |
| 6 | +from typing import Any, List, MutableMapping, Optional, Type, Union |
| 7 | + |
| 8 | +import dpath |
| 9 | + |
| 10 | +from airbyte_cdk.sources.declarative.interpolation.interpolated_boolean import InterpolatedBoolean |
| 11 | +from airbyte_cdk.sources.declarative.interpolation.interpolated_string import InterpolatedString |
| 12 | +from airbyte_cdk.sources.declarative.transformations.config_transformations.config_transformation import ( |
| 13 | + ConfigTransformation, |
| 14 | +) |
| 15 | +from airbyte_cdk.sources.types import FieldPointer |
| 16 | + |
| 17 | + |
| 18 | +@dataclass(frozen=True) |
| 19 | +class AddedFieldDefinition: |
| 20 | + """Defines the field to add on a config""" |
| 21 | + |
| 22 | + path: FieldPointer |
| 23 | + value: Union[InterpolatedString, str] |
| 24 | + value_type: Optional[Type[Any]] = None |
| 25 | + |
| 26 | + |
| 27 | +@dataclass(frozen=True) |
| 28 | +class ParsedAddFieldDefinition: |
| 29 | + """Defines the field to add on a config""" |
| 30 | + |
| 31 | + path: FieldPointer |
| 32 | + value: InterpolatedString |
| 33 | + value_type: Optional[Type[Any]] = None |
| 34 | + |
| 35 | + |
| 36 | +@dataclass |
| 37 | +class ConfigAddFields(ConfigTransformation): |
| 38 | + """ |
| 39 | + Transformation which adds fields to a config. The path of the added field can be nested. Adding nested fields will create all |
| 40 | + necessary parent objects (like mkdir -p). |
| 41 | +
|
| 42 | + This transformation has access to the config being transformed. |
| 43 | +
|
| 44 | + Examples of instantiating this transformation via YAML: |
| 45 | + - type: ConfigAddFields |
| 46 | + fields: |
| 47 | + # hardcoded constant |
| 48 | + - path: ["path"] |
| 49 | + value: "static_value" |
| 50 | +
|
| 51 | + # nested path |
| 52 | + - path: ["path", "to", "field"] |
| 53 | + value: "static" |
| 54 | +
|
| 55 | + # from config |
| 56 | + - path: ["derived_field"] |
| 57 | + value: "{{ config.original_field }}" |
| 58 | +
|
| 59 | + # by supplying any valid Jinja template directive or expression |
| 60 | + - path: ["two_times_two"] |
| 61 | + value: "{{ 2 * 2 }}" |
| 62 | +
|
| 63 | + Attributes: |
| 64 | + fields (List[AddedFieldDefinition]): A list of transformations (path and corresponding value) that will be added to the config |
| 65 | + """ |
| 66 | + |
| 67 | + fields: List[AddedFieldDefinition] |
| 68 | + condition: str = "" |
| 69 | + _parsed_fields: List[ParsedAddFieldDefinition] = field( |
| 70 | + init=False, repr=False, default_factory=list |
| 71 | + ) |
| 72 | + |
| 73 | + def __post_init__(self) -> None: |
| 74 | + self._filter_interpolator = InterpolatedBoolean(condition=self.condition, parameters={}) |
| 75 | + |
| 76 | + for add_field in self.fields: |
| 77 | + if len(add_field.path) < 1: |
| 78 | + raise ValueError( |
| 79 | + f"Expected a non-zero-length path for the AddFields transformation {add_field}" |
| 80 | + ) |
| 81 | + |
| 82 | + if not isinstance(add_field.value, InterpolatedString): |
| 83 | + if not isinstance(add_field.value, str): |
| 84 | + raise ValueError( |
| 85 | + f"Expected a string value for the AddFields transformation: {add_field}" |
| 86 | + ) |
| 87 | + else: |
| 88 | + self._parsed_fields.append( |
| 89 | + ParsedAddFieldDefinition( |
| 90 | + add_field.path, |
| 91 | + InterpolatedString.create(add_field.value, parameters={}), |
| 92 | + value_type=add_field.value_type, |
| 93 | + ) |
| 94 | + ) |
| 95 | + else: |
| 96 | + self._parsed_fields.append( |
| 97 | + ParsedAddFieldDefinition( |
| 98 | + add_field.path, |
| 99 | + add_field.value, |
| 100 | + value_type=add_field.value_type, |
| 101 | + ) |
| 102 | + ) |
| 103 | + |
| 104 | + def transform( |
| 105 | + self, |
| 106 | + config: MutableMapping[str, Any], |
| 107 | + ) -> None: |
| 108 | + """ |
| 109 | + Transforms a config by adding fields based on the provided field definitions. |
| 110 | +
|
| 111 | + :param config: The user-provided configuration to be transformed |
| 112 | + """ |
| 113 | + for parsed_field in self._parsed_fields: |
| 114 | + valid_types = (parsed_field.value_type,) if parsed_field.value_type else None |
| 115 | + value = parsed_field.value.eval(config, valid_types=valid_types) |
| 116 | + if not self.condition or self._filter_interpolator.eval( |
| 117 | + config, value=value, path=parsed_field.path |
| 118 | + ): |
| 119 | + dpath.new(config, parsed_field.path, value) |
0 commit comments