From 5081132bd4cd473f08e82e6d1541d1bdc6a81b7a Mon Sep 17 00:00:00 2001 From: srz_zumix Date: Fri, 28 Jun 2024 11:23:54 +0900 Subject: [PATCH 1/2] add json filter --- additionals/filter.py | 7 ------- additionals/json.py | 24 ++++++++++++++++++++++++ readme/README.md.j2 | 7 +++++-- testdata/default-example.j2 | 2 ++ 4 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 additionals/json.py diff --git a/additionals/filter.py b/additionals/filter.py index f534790..89ae947 100644 --- a/additionals/filter.py +++ b/additionals/filter.py @@ -3,7 +3,6 @@ ) from jinja2 import pass_context import base64 -import jmespath import re @@ -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): diff --git a/additionals/json.py b/additionals/json.py new file mode 100644 index 0000000..a0e0d70 --- /dev/null +++ b/additionals/json.py @@ -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) diff --git a/readme/README.md.j2 b/readme/README.md.j2 index b224b67..91d8425 100644 --- a/readme/README.md.j2 +++ b/readme/README.md.j2 @@ -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 @@ -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 diff --git a/testdata/default-example.j2 b/testdata/default-example.j2 index 794ee1f..7792b79 100644 --- a/testdata/default-example.j2 +++ b/testdata/default-example.j2 @@ -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 }} From 36eef39f8add757a0ce86aed8fc3e7c0b081f495 Mon Sep 17 00:00:00 2001 From: srz_zumix Date: Fri, 28 Jun 2024 12:27:16 +0900 Subject: [PATCH 2/2] update README --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index be25613..b74a8f7 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 }} ```