forked from amundsen-io/amundsendatabuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic_transformer.py
35 lines (24 loc) · 987 Bytes
/
generic_transformer.py
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
# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0
import logging
from typing import Any, Dict
from pyhocon import ConfigTree
from databuilder.transformer.base_transformer import Transformer
CALLBACK_FUNCTION = 'callback_function'
FIELD_NAME = 'field_name'
LOGGER = logging.getLogger(__name__)
class GenericTransformer(Transformer):
"""
A generic transformer that accepts a callback function that transforms the record on specified field.
"""
def init(self, conf: ConfigTree) -> None:
self._callback_function = conf.get(CALLBACK_FUNCTION)
self._field_name = conf.get_string(FIELD_NAME)
def transform(self, record: Dict[str, Any]) -> Dict[str, Any]:
for k, v in record.items():
if k == self._field_name:
new_val = self._callback_function(v)
record[k] = new_val
return record
def get_scope(self) -> str:
return 'transformer.generic'