You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by po...@apache.org on 2023/02/19 14:41:31 UTC

[airflow] branch main updated: Make json and yaml available in templates (#28930)

This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new ffdc696942 Make json and yaml available in templates (#28930)
ffdc696942 is described below

commit ffdc696942d96a14a5ee0279f950e3114817055c
Author: RachitSharma2001 <45...@users.noreply.github.com>
AuthorDate: Sun Feb 19 06:41:11 2023 -0800

    Make json and yaml available in templates (#28930)
    
    * Add json_loads macro
    
    * Update __init__.py
    
    ---------
    
    Co-authored-by: eladkal <45...@users.noreply.github.com>
    Co-authored-by: Tzu-ping Chung <ur...@gmail.com>
---
 airflow/macros/__init__.py  |  2 ++
 tests/macros/test_macros.py | 31 +++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/airflow/macros/__init__.py b/airflow/macros/__init__.py
index ca36dbe00a..240b847c34 100644
--- a/airflow/macros/__init__.py
+++ b/airflow/macros/__init__.py
@@ -17,6 +17,7 @@
 # under the License.
 from __future__ import annotations
 
+import json  # noqa
 import time  # noqa
 import uuid  # noqa
 from datetime import datetime, timedelta
@@ -26,6 +27,7 @@ from typing import Any
 import dateutil  # noqa
 from pendulum import DateTime
 
+import airflow.utils.yaml as yaml  # noqa
 from airflow.utils.deprecation_tools import add_deprecated_classes
 
 __deprecated_classes = {
diff --git a/tests/macros/test_macros.py b/tests/macros/test_macros.py
index f6d615ea38..76ea22dd92 100644
--- a/tests/macros/test_macros.py
+++ b/tests/macros/test_macros.py
@@ -89,3 +89,34 @@ def test_ds_format(ds, input_format, output_format, expected):
 def test_datetime_diff_for_humans(dt, since, expected):
     result = macros.datetime_diff_for_humans(dt, since)
     assert result == expected
+
+
+@pytest.mark.parametrize(
+    "input_value, expected",
+    [
+        ('{"field1":"value1", "field2":4, "field3":true}', {"field1": "value1", "field2": 4, "field3": True}),
+        (
+            '{"field1": [ 1, 2, 3, 4, 5 ], "field2" : {"mini1" : 1, "mini2" : "2"}}',
+            {"field1": [1, 2, 3, 4, 5], "field2": {"mini1": 1, "mini2": "2"}},
+        ),
+    ],
+)
+def test_json_loads(input_value, expected):
+    result = macros.json.loads(input_value)
+    assert result == expected
+
+
+@pytest.mark.parametrize(
+    "input_value, expected",
+    [
+        ('{"field1":"value1", "field2":4, "field3":true}', {"field1": "value1", "field2": 4, "field3": True}),
+        ("field1: value1\nfield2: value2", {"field1": "value1", "field2": "value2"}),
+        (
+            'field1: [ 1, 2, 3, 4, 5 ]\nfield2: {"mini1" : 1, "mini2" : "2"}',
+            {"field1": [1, 2, 3, 4, 5], "field2": {"mini1": 1, "mini2": "2"}},
+        ),
+    ],
+)
+def test_yaml_loads(input_value, expected):
+    result = macros.yaml.safe_load(input_value)
+    assert result == expected