derrickaw commented on code in PR #38236:
URL: https://github.com/apache/beam/pull/38236#discussion_r3119639022


##########
sdks/python/apache_beam/yaml/yaml_mapping.py:
##########
@@ -205,83 +211,78 @@ def py_value_to_js_dict(py_value):
     return py_value
 
 
+def js_to_py(obj):
+  """Converts mini-racer mapped objects to standard Python types.
+  
+  This is needed because ctx.eval returns objects that implement Mapping
+  and Iterable but are not picklable (like JSMappedObjectImpl and JSArrayImpl),
+  which would fail when Beam tries to serialize rows containing them.
+  We also preserve datetime objects which are correctly produced by ctx.eval
+  for JS Date objects.
+  """
+  if isinstance(obj, datetime.datetime):
+    return obj
+  elif isinstance(obj, Mapping):
+    return {k: js_to_py(v) for k, v in obj.items()}
+  elif not isinstance(obj, (str, bytes)) and isinstance(obj, Iterable):
+    return [js_to_py(v) for v in obj]
+  elif isinstance(obj, str):
+    try:
+      if obj.endswith('Z'):
+        return datetime.datetime.fromisoformat(obj[:-1] + '+00:00')
+      return datetime.datetime.fromisoformat(obj)
+    except ValueError:
+      return obj

Review Comment:
   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]

Reply via email to