Skip to content

Commit f50203c

Browse files
committed
use jinja template to help simplify the code context
1 parent 9241326 commit f50203c

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

sdks/python/apache_beam/yaml/yaml_mapping.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from typing import TypeVar
3131
from typing import Union
3232

33+
import jinja2
3334
import apache_beam as beam
3435
from apache_beam.io.filesystems import FileSystems
3536
from apache_beam.portability.api import schema_pb2
@@ -69,6 +70,25 @@
6970
'Partition': 'by',
7071
}
7172

73+
JS_EXPR_TEMPLATE = jinja2.Template(
74+
"""
75+
var {{ func_id }} = (__row__) => {
76+
{% for field in valid_fields %}
77+
const {{ field }} = __row__['{{ field }}'];
78+
{% endfor %}
79+
return ({{ expr }});
80+
};
81+
""")
82+
83+
JS_AGGREGATOR_TEMPLATE = jinja2.Template(
84+
"""
85+
var __aggregate_fn__ = (__row__) => ({
86+
{% for name, func_name in field_funcs.items() %}
87+
"{{ name }}": {{ func_name }}(__row__){% if not loop.last %},{% endif %}
88+
{% endfor %}
89+
});
90+
""")
91+
7292

7393
def normalize_mapping(spec):
7494
"""
@@ -262,10 +282,9 @@ def __init__(self, fields, original_fields, input_schema):
262282
n for n in original_fields
263283
if n in e and _JS_IDENTIFIER_PATTERN.match(n)
264284
]
265-
consts = " ".join(
266-
[f"const {n} = __row__['{n}'];" for n in valid_fields])
267-
code = f"var func_{i} = (__row__) => {{ {consts} return ({e}); }}"
268-
script.append(code)
285+
code = JS_EXPR_TEMPLATE.render(
286+
func_id=f"func_{i}", valid_fields=valid_fields, expr=e)
287+
script.append(code.strip())
269288
self.field_funcs[name] = f"func_{i}"
270289
elif 'callable' in expr:
271290
code = f"var func_{i} = {expr['callable']}"
@@ -279,12 +298,8 @@ def __init__(self, fields, original_fields, input_schema):
279298
self.field_funcs[name] = func_name
280299

281300
if self.field_funcs:
282-
aggregator_entries = ", ".join([
283-
f'"{name}": {func_name}(__row__)'
284-
for name, func_name in self.field_funcs.items()
285-
])
286-
script.append(
287-
f"var __aggregate_fn__ = (__row__) => ({{ {aggregator_entries} }});")
301+
code = JS_AGGREGATOR_TEMPLATE.render(field_funcs=self.field_funcs)
302+
script.append(code.strip())
288303

289304
self.script = "\n".join(script) if script else None
290305

0 commit comments

Comments
 (0)