Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make json and yaml available in templates #28930

Merged
merged 4 commits into from
Feb 19, 2023

Conversation

RachitSharma2001
Copy link
Contributor

@RachitSharma2001 RachitSharma2001 commented Jan 13, 2023

closes: #27079

It was suggested that a macro be added that allows for the deserialization of json or yaml strings (since yaml is a superset of jsons, I just deserialized using yaml).

I also added tests for this macro in tests/macro.

I would also be open to adding this functionality to the system tests if that is needed.

Copy link
Contributor

@Taragolis Taragolis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nits

Also we should add info into the Templates reference documentation. Otherwise how users would know how to use this macro?

airflow/macros/__init__.py Outdated Show resolved Hide resolved
Comment on lines 90 to 91

def deserialize(data: str) -> dict[Any, Any]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deserialize it is a bit broad name which could mean everything and might confuse users.

tests/macros/test_macros.py Outdated Show resolved Hide resolved
@RachitSharma2001 RachitSharma2001 force-pushed the airflow_json_loads branch 2 times, most recently from 172cfdc to a2fcdc1 Compare January 15, 2023 18:50
@RachitSharma2001
Copy link
Contributor Author

RachitSharma2001 commented Jan 15, 2023

Sounds good! I have integrated the requested changes in my latest push, including changing the name of the macro from deseralize to json_loads.

However, in terms of the documentation, I think it will already automatically be added to the templates reference. This is because this macro is a macro function, specified in airflow/macros along with ds_add, ds_format, datetime_diff_for_humans). In the bottom of templates_ref.rst, it loads the documentation provided for each function in the that macro file. So I think the documentation I added to the json_loads macro (specified in the comment provided under the function) will actually be linked in the templates reference. Let me know if this is correct or if my understanding is wrong.

@RachitSharma2001 RachitSharma2001 changed the title Add deserialize macro Add json_loads macro Jan 15, 2023
@uranusjr
Copy link
Member

I’d prefer adding two separate macros, one for only JSON and another for YAML support.

@RachitSharma2001 RachitSharma2001 changed the title Add json_loads macro Add json_loads and yaml_loads macro Jan 17, 2023
@RachitSharma2001
Copy link
Contributor Author

Sounds good! I have created those two separate macros and have rebased. Let me know if any other changes are needed.

@uranusjr
Copy link
Member

Hmm, looking at other things in the module, I wonder if we should simply import json and yaml into it, so users can just write {{ json.loads(...) }} instead of needing to remember a slightly different syntax.

@potiuk
Copy link
Member

potiuk commented Jan 18, 2023

Hmm, looking at other things in the module, I wonder if we should simply import json and yaml into it, so users can just write {{ json.loads(...) }} instead of needing to remember a slightly different syntax.

Yeah. It woudl be useful to add json/yaml generic imports for many other use cases. The only potential problem is safety of those, but I think those two shoudl be rather safe to add.

@RachitSharma2001
Copy link
Contributor Author

RachitSharma2001 commented Jan 21, 2023

That makes sense. Would it still make sense to keep the tests on json and yaml, or should those be removed too?

@potiuk
Copy link
Member

potiuk commented Jan 22, 2023

Keeping two tests is fine.

@RachitSharma2001 RachitSharma2001 force-pushed the airflow_json_loads branch 3 times, most recently from 8cdae66 to 8b38ee4 Compare January 27, 2023 23:55
Copy link
Member

@uranusjr uranusjr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a follow up, those noqa comments should be removed and replaced by an explicit whitelist.

@RachitSharma2001
Copy link
Contributor Author

RachitSharma2001 commented Feb 4, 2023

Hi everyone, it seems like this PR is approved. I was wondering if there is anything else that should be updated or changed? If so, let me know.

@uranusjr uranusjr changed the title Add json_loads and yaml_loads macro Make json and yaml available in templates Feb 5, 2023
@RachitSharma2001
Copy link
Contributor Author

Hi everyone, it seems like this PR has been approved and passes all the tests. I was wondering if there is anything else that should be updated or changed? If so, let me know.

@RachitSharma2001
Copy link
Contributor Author

RachitSharma2001 commented Feb 16, 2023

Hi everyone, I was wondering, since the changes on this branch have been approved and it seems to be passing all the tests, if the branch could be merged into main? Or if there is anything else that needs to be updated or changed, let me know.

@potiuk potiuk merged commit ffdc696 into apache:main Feb 19, 2023
@pierrejeambrun pierrejeambrun added this to the Airflow 2.6.0 milestone Feb 27, 2023
@pierrejeambrun pierrejeambrun added the type:new-feature Changelog: New Features label Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:new-feature Changelog: New Features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Option to deserialize JSON from last log line in BashOperator and DockerOperator before sending to XCom
6 participants