derrickaw commented on code in PR #38130:
URL: https://github.com/apache/beam/pull/38130#discussion_r3093139291
##########
sdks/python/apache_beam/yaml/yaml_mapping.py:
##########
@@ -210,78 +225,106 @@ def py_value_to_js_dict(py_value):
def _expand_javascript_mapping_func(
original_fields, expression=None, callable=None, path=None, name=None):
- # Check for installed js2py package
- if js2py is None:
+ # Check for installed quickjs package
+ if quickjs is None:
raise ValueError(
- "Javascript mapping functions are not supported on"
- " Python 3.12 or later.")
-
- # import remaining js2py objects
- from js2py import base
- from js2py.constructors import jsdate
- from js2py.internals import simplex
-
- js_array_type = (
- base.PyJsArray,
- base.PyJsArrayBuffer,
- base.PyJsInt8Array,
- base.PyJsUint8Array,
- base.PyJsUint8ClampedArray,
- base.PyJsInt16Array,
- base.PyJsUint16Array,
- base.PyJsInt32Array,
- base.PyJsUint32Array,
- base.PyJsFloat32Array,
- base.PyJsFloat64Array)
-
- def _js_object_to_py_object(obj):
- if isinstance(obj, (base.PyJsNumber, base.PyJsString, base.PyJsBoolean)):
- return base.to_python(obj)
- elif isinstance(obj, js_array_type):
- return [_js_object_to_py_object(value) for value in obj.to_list()]
- elif isinstance(obj, jsdate.PyJsDate):
- return obj.to_utc_dt()
- elif isinstance(obj, (base.PyJsNull, base.PyJsUndefined)):
- return None
- elif isinstance(obj, base.PyJsError):
- raise RuntimeError(obj['message'])
- elif isinstance(obj, base.PyJsObject):
- return {
- key: _js_object_to_py_object(value['value'])
- for (key, value) in obj.own.items()
- }
- elif isinstance(obj, base.JsObjectWrapper):
- return _js_object_to_py_object(obj._obj)
+ "Javascript mapping functions require the 'quickjs' package.")
- return obj
+ def make_bridge_source(func_name, call_expr):
+ keys_json = json.dumps(list(original_fields))
+ return (
+ f"function {func_name}(serialized_flags, ...values) {{ "
+ f" const keys = JSON.parse('{keys_json}'); "
+ f" const flags = serialized_flags.split(','); "
+ f" const row = {{}}; "
+ f" for (let i = 0; i < keys.length; i++) {{ "
+ f" let val = values[i]; "
+ f" if (flags[i] === '1') val = JSON.parse(val); "
+ f" row[keys[i]] = val; "
+ f" }} "
+ f" const result = {call_expr}; "
+ f" return (typeof result === 'object' && result !== null) "
+ f"? '__json__:' + JSON.stringify(result) : result; "
Review Comment:
good point, done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]