-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathinterpolated_nested_mapping.py
More file actions
52 lines (41 loc) · 1.83 KB
/
interpolated_nested_mapping.py
File metadata and controls
52 lines (41 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
#
from dataclasses import InitVar, dataclass
from typing import Any, Mapping, Optional, Union
from airbyte_cdk.sources.declarative.interpolation.jinja import JinjaInterpolation
from airbyte_cdk.sources.types import Config
NestedMappingEntry = Union[
dict[str, "NestedMapping"], list["NestedMapping"], str, int, float, bool, None
]
NestedMapping = Union[dict[str, NestedMappingEntry], str, dict[str, Any]]
@dataclass
class InterpolatedNestedMapping:
"""
Wrapper around a nested dict which can contain lists and primitive values where both the keys and values are interpolated recursively.
Attributes:
mapping (NestedMapping): to be evaluated
"""
mapping: NestedMapping
parameters: InitVar[Mapping[str, Any]]
def __post_init__(self, parameters: Optional[Mapping[str, Any]]) -> None:
self._interpolation = JinjaInterpolation()
self._parameters = parameters
def eval(self, config: Config, **additional_parameters: Any) -> Any:
return self._eval(self.mapping, config, **additional_parameters)
def _eval(
self, value: Union[NestedMapping, NestedMappingEntry], config: Config, **kwargs: Any
) -> Any:
# Recursively interpolate dictionaries and lists
if isinstance(value, str):
return self._interpolation.eval(value, config, parameters=self._parameters, **kwargs)
elif isinstance(value, dict):
interpolated_dict = {
self._eval(k, config, **kwargs): self._eval(v, config, **kwargs)
for k, v in value.items()
}
return {k: v for k, v in interpolated_dict.items() if v is not None}
elif isinstance(value, list):
return [self._eval(v, config, **kwargs) for v in value]
else:
return value