Skip to content

Commit 01cf5a6

Browse files
committed
update remap to handle interpolated keys/values
1 parent c64e588 commit 01cf5a6

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

airbyte_cdk/sources/declarative/transformations/config_transformations/add_fields.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ def transform(
113113
for parsed_field in self._parsed_fields:
114114
valid_types = (parsed_field.value_type,) if parsed_field.value_type else None
115115
value = parsed_field.value.eval(config, valid_types=valid_types)
116-
is_empty_condition = not self.condition
117-
if is_empty_condition or self._filter_interpolator.eval(
116+
if not self.condition or self._filter_interpolator.eval(
118117
config, value=value, path=parsed_field.path
119118
):
120119
dpath.new(config, parsed_field.path, value)

airbyte_cdk/sources/declarative/transformations/config_transformations/remap_field.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
33
#
44

5-
from dataclasses import dataclass
5+
from dataclasses import dataclass, field
66
from typing import Any, List, Mapping, MutableMapping, Union
77

8+
from airbyte_cdk.sources.declarative.interpolation.interpolated_boolean import InterpolatedBoolean
9+
from airbyte_cdk.sources.declarative.interpolation.interpolated_mapping import InterpolatedMapping
810
from airbyte_cdk.sources.declarative.interpolation.interpolated_string import InterpolatedString
911
from airbyte_cdk.sources.declarative.transformations.config_transformations.config_transformation import (
1012
ConfigTransformation,
@@ -19,6 +21,7 @@ class ConfigRemapField(ConfigTransformation):
1921

2022
map: Mapping[str, Any]
2123
field_path: List[Union[InterpolatedString, str]]
24+
config: Mapping[str, Any] = field(default_factory=dict)
2225

2326
def __post_init__(self) -> None:
2427
if not self.field_path:
@@ -31,6 +34,7 @@ def __post_init__(self) -> None:
3134
self._field_path[path_index] = InterpolatedString.create(
3235
self.field_path[path_index], parameters={}
3336
)
37+
self._map = InterpolatedMapping(self.map, parameters={}).eval(config=self.config)
3438

3539
def transform(
3640
self,
@@ -51,10 +55,10 @@ def transform(
5155
return
5256
current = current[component]
5357

54-
if not isinstance(current, Mapping):
58+
if not isinstance(current, MutableMapping):
5559
return
5660

5761
field_name = path_components[-1]
5862

59-
if field_name in current and current[field_name] in self.map:
60-
current[field_name] = self.map[current[field_name]]
63+
if field_name in current and current[field_name] in self._map:
64+
current[field_name] = self._map[current[field_name]]

unit_tests/sources/declarative/transformations/config_transformations/test_config_remap_field.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,26 @@
33

44
import pytest
55

6-
from airbyte_cdk.sources.declarative.transformations.config_transformations.remap_field import (
6+
from airbyte_cdk.sources.declarative.transformations.config_transformations import (
77
ConfigRemapField as RemapField,
88
)
99

1010

1111
class TestRemapField(TestCase):
1212
def test_given_valid_inputs_when_transform_then_field_is_remapped(self):
13-
remap_transform = RemapField(
14-
field_path=["authorization", "auth_type"],
15-
map={"client_credentials": "oauth2", "api_key": "key_auth"},
16-
)
17-
1813
config = {
1914
"authorization": {
2015
"auth_type": "client_credentials",
2116
"client_id": "12345",
2217
"client_secret": "secret",
2318
}
2419
}
20+
remap_transform = RemapField(
21+
field_path=["authorization", "auth_type"],
22+
map={"client_credentials": "oauth2", "api_key": "key_auth"},
23+
config=config
24+
)
25+
2526
original_config = deepcopy(config)
2627

2728
remap_transform.transform(config)
@@ -110,3 +111,18 @@ def test_multiple_transformations_applied_in_sequence(self):
110111

111112
assert config["auth"]["type"] == "oauth2"
112113
assert config["environment"] == "development"
114+
115+
def test_amazon_seller_partner_marketplace_remap_with_interpolated_mapping(self):
116+
117+
mapping = {
118+
"endpoint": {
119+
"ES": "{{ 'https://sellingpartnerapi' if config.environment == 'production' else 'https://sandbox.sellingpartnerapi' }}-eu.amazon.com",
120+
}
121+
}
122+
sandbox_config = {"environment": "sandbox", "marketplace": "ES"}
123+
production_config = {"environment": "production", "marketplace": "ES"}
124+
RemapField(field_path=["marketplace"], map=mapping["endpoint"], config=sandbox_config).transform(sandbox_config)
125+
RemapField(field_path=["marketplace"], map=mapping["endpoint"], config=production_config).transform(production_config)
126+
127+
assert sandbox_config["marketplace"] == "https://sandbox.sellingpartnerapi-eu.amazon.com"
128+
assert production_config["marketplace"] == "https://sellingpartnerapi-eu.amazon.com"

0 commit comments

Comments
 (0)