-
-
Notifications
You must be signed in to change notification settings - Fork 749
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
Implement new JSON Jinja filters #3766
Conversation
… simply chain filters together
…a newline in changelog
st2common/in-requirements.txt
Outdated
@@ -3,6 +3,7 @@ apscheduler | |||
python-dateutil | |||
eventlet | |||
jinja2 | |||
jmespath |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already use jsonpath-rw
dependency and JSONPath in other places.
I think it would be a good idea to also use that for your new filter, if possible, to avoid additional dependencies in the standard distribution.
I would be fine with this dependency if it was part of a custom filter which is not shipped with the default distribution.
Thanks. I have one concern about additional dependency, but besides that I don't see any other blockers. One thing I still don't like is just copy and pasting code over to Mistral. I know we did this in the past, but I already wasn't a big fan of it back then. Imo, we should split those filters in a common package (e.g. |
|
||
# IMPORTANT NOTE - these filters were recently duplicated in st2mistral so that | ||
# they are also available in Mistral workflows. Please ensure any additions you | ||
# make here are also made there so that feature parity is maintained. | ||
return { | ||
'decrypt_kv': crypto.decrypt_kv, | ||
'from_json_string': data.from_json_string, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually already have pack actions for that, but I guess I still see some value in having those operations available as filters as well.
Having said that, we need to be careful down the road and come up with some rules / conventions on what goes in a filter and what in an action.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.
My use case for from_json_string
is reading values out of the datastore. Commonly in our action
metadata files we have default values set to Jinja expressions that read from datastore and contain JSON strings. In order for us to utilize this data effectively we need to parse the JSON out into objects.
Part of this problem stems from the fact that the datastore only stores information in string
format, so you're forced to serialize on either end. If typed loading/unloading was supported, then the need for this filter (in our use case) would go away.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a related note - we actually have action for that which handles serialization / de-serialization (st2kv.get_object
st2kv.set_object
or something along those lines) and could maybe also work for some of your use cases.
@Kami I'm looking into JSONPath, honestly didn't see it when i was coding this. It might be a good alternative since it's already in the distribution. Keeping the dependencies low is always a good thing! Also, i 100% agree that the right thing to do is have an external |
… feature/json-jinja-filters
@Kami i took your advice and ported the code to use JSONPath. Any other comments are appreciated! One thing to note that's lacking in the implementation of JSONPath we currently have included The same expression in JMESPath would be |
Cool. At a glance, I wonder if this would make it so I don't need StackStorm-Exchange/stackstorm-ansible/pull/14 to get complex data passing working. |
Awesome stuff here! 🥇 StackStorm is definitely missing more useful Jinja filters. |
Thanks - looks good to me. I'm still not fan of thise copy and paste between st2 and mistral repo so let's try to come up with a solution for that soon (would appreciate if you can open issue for that so we can track it). |
This PR implements several new Jinja filters:
from_json_str
Parses a JSON string into a dict/list/string/int/bool, just doesjson.loads
under the hood. This is useful, and really necessary, when storing complex data structures in the datastore. Since everything in the datastore is a string, in order to use the data it needs to be parsed out. Now we can have an expression like:{{ st2kv.system.my_complex_value | from_json_str }}
and be able to use the result like a regular object.from_yaml_str
Parses a YAML string into a dict/list/string/int/bool, just doesyaml.safe_load
under the hood.jsonpath_query
Similar to Ansible'sjson_query
filter, except this version uses JSONPath where ansible uses JMESPath. This allows complex data queries in a straight forward syntax that would otherwise require complex and hard to read chains of jinja filters.TODO
st2mistral
- DONE Implement new JSON Jinja filters st2mistral#33st2docs
- DONE Implement new JSON Jinja filters st2docs#617JMESPath
toJSONPath
(thanks @Kami !)