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

add json filter #37

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
* ternary
* b64encode
* b64decode
* [json_query](https://jmespath.org/)
* [playground](https://play.jmespath.org/)
* [github](additionals/github.py)
* get_user
* get_user_email
Expand All @@ -37,6 +35,11 @@
* listdir
* listdir_files
* listdir_dirs
* [json](additionals/json.py)
* [json_query](https://jmespath.org/)
* [playground](https://play.jmespath.org/)
* json_dumps
* json_loads
* [to_yaml](additionals/to_yaml.py)
* to_yaml
* to_nice_yaml
Expand Down Expand Up @@ -65,6 +68,8 @@
{%- for url in (github | json_query('[*.url,*.*.url,*.*.*.url] | [] | [] | []')) %}
* {{ url }}
{%- endfor %}
{{ 'test\n' | json_dumps }}
{{ (github.event | json_dumps | json_loads).repository.name }}

{{ github | to_nice_yaml }}
```
Expand Down
7 changes: 0 additions & 7 deletions additionals/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
)
from jinja2 import pass_context
import base64
import jmespath
import re


Expand Down Expand Up @@ -35,12 +34,6 @@ def b64decode(ctx, v, *, encoding='utf-8'):
return base64.b64decode(v).decode(encoding)


@as_filter
@pass_context
def json_query(ctx, v, query):
return jmespath.search(query, v)


@as_filter
@pass_context
def typeof(ctx, v):
Expand Down
24 changes: 24 additions & 0 deletions additionals/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from kamidana import (
as_filter,
)
from jinja2 import pass_context
import jmespath
import json


@as_filter
@pass_context
def json_query(ctx, v, query):
return jmespath.search(query, v)


@as_filter
@pass_context
def json_dumps(ctx, v):
return json.dumps(v)


@as_filter
@pass_context
def json_loads(ctx, v):
return json.loads(v)
7 changes: 5 additions & 2 deletions readme/README.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
* ternary
* b64encode
* b64decode
* [json_query](https://jmespath.org/)
* [playground](https://play.jmespath.org/)
* [github](additionals/github.py)
* get_user
* get_user_email
Expand All @@ -37,6 +35,11 @@
* listdir
* listdir_files
* listdir_dirs
* [json](additionals/json.py)
* [json_query](https://jmespath.org/)
* [playground](https://play.jmespath.org/)
* json_dumps
* json_loads
* [to_yaml](additionals/to_yaml.py)
* to_yaml
* to_nice_yaml
Expand Down
2 changes: 2 additions & 0 deletions testdata/default-example.j2
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
{%- for url in (github | json_query('[*.url,*.*.url,*.*.*.url] | [] | [] | []')) %}
* {{ url }}
{%- endfor %}
{{ 'test\n' | json_dumps }}
{{ (github.event | json_dumps | json_loads).repository.name }}

{{ github | to_nice_yaml }}