-
-
Notifications
You must be signed in to change notification settings - Fork 779
Implement new JSON Jinja filters #3766
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6bef102
Initial work on some additional JSON Jinja filters
nmaludy dd7894a
Fix unit tests for new JSON jinja filters
nmaludy 8f25276
Added unit and integration tests for new JSON jinja filters
nmaludy e095999
Added integration tests for jmespath_query_str
nmaludy b7490fa
Removed jmespath_query_str since it was not necessary, instead we can…
nmaludy 8dc040f
Fix flake8 errors
nmaludy f3abc88
Fixed function name in from_yaml_string integration test
nmaludy 7cac87c
Add ci-mistral tests to travis
nmaludy 07794c1
Removed ci-mistral tests from tavis (weren't enabled before) and extr…
nmaludy 1e15a38
Merge branch 'master' into feature/json-jinja-filters
nmaludy 568340e
Migrated from JMESPath to JSONPath in the new jinja filters
nmaludy 23a8310
Merge remote-tracking branch 'origin/feature/json-jinja-filters' into…
nmaludy 4a57ae1
Merge branch 'master' into feature/json-jinja-filters
nmaludy a52c07b
Fixed a bugs in jsonpath query found during unit testing.
nmaludy 8c8018f
Fixed jsonpath import
nmaludy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
contrib/examples/actions/workflows/tests/mistral-test-func-from-json-string.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| version: '2.0' | ||
|
|
||
| examples.mistral-test-func-from-json-string: | ||
| description: A workflow for testing from_json_string custom filter in mistral | ||
| type: direct | ||
| input: | ||
| - input_str | ||
| output: | ||
| result_jinja: <% $.result_jinja %> | ||
| result_yaql: <% $.result_yaql %> | ||
| tasks: | ||
| task1: | ||
| action: std.noop | ||
| publish: | ||
| result_jinja: "{{ from_json_string(_.input_str) }}" | ||
| result_yaql: '<% from_json_string($.input_str) %>' |
16 changes: 16 additions & 0 deletions
16
contrib/examples/actions/workflows/tests/mistral-test-func-from-yaml-string.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| version: '2.0' | ||
|
|
||
| examples.mistral-test-func-from-yaml-string: | ||
| description: A workflow for testing from_yaml_string custom filter in mistral | ||
| type: direct | ||
| input: | ||
| - input_str | ||
| output: | ||
| result_jinja: <% $.result_jinja %> | ||
| result_yaql: <% $.result_yaql %> | ||
| tasks: | ||
| task1: | ||
| action: std.noop | ||
| publish: | ||
| result_jinja: "{{ from_yaml_string(_.input_str) }}" | ||
| result_yaql: '<% from_yaml_string($.input_str) %>' |
18 changes: 18 additions & 0 deletions
18
contrib/examples/actions/workflows/tests/mistral-test-func-jsonpath-query.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| version: '2.0' | ||
|
|
||
| examples.mistral-test-func-jsonpath-query: | ||
| description: A workflow for testing jsonpath_query custom filter in mistral | ||
| type: direct | ||
| input: | ||
| - input_obj | ||
| - input_query | ||
| output: | ||
| result_jinja: <% $.result_jinja %> | ||
| result_yaql: <% $.result_yaql %> | ||
| tasks: | ||
|
|
||
| task2: | ||
| action: std.noop | ||
| publish: | ||
| result_jinja: '{{ jsonpath_query(_.input_obj, _.input_query) }}' | ||
| result_yaql: '<% jsonpath_query($.input_obj, $.input_query) %>' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Licensed to the StackStorm, Inc ('StackStorm') under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import jsonpath_rw | ||
|
|
||
| __all__ = [ | ||
| 'jsonpath_query', | ||
| ] | ||
|
|
||
|
|
||
| def jsonpath_query(value, query): | ||
| """Extracts data from an object `value` using a JSONPath `query`. | ||
| :link: https://github.com/kennknowles/python-jsonpath-rw | ||
| :param value: a object (dict, array, etc) to query | ||
| :param query: a JSONPath query expression (string) | ||
| :returns: the result of the query executed on the value | ||
| :rtype: dict, array, int, string, bool | ||
| """ | ||
| expr = jsonpath_rw.parse(query) | ||
| matches = [match.value for match in expr.find(value)] | ||
| if not matches: | ||
| return None | ||
| return matches |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
st2common/tests/unit/test_jinja_render_jsonpath_query_filters.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Licensed to the StackStorm, Inc ('StackStorm') under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| import unittest2 | ||
|
|
||
| from st2common.util import jinja as jinja_utils | ||
|
|
||
|
|
||
| class JinjaUtilsJsonpathQueryTestCase(unittest2.TestCase): | ||
|
|
||
| def test_jsonpath_query_static(self): | ||
| env = jinja_utils.get_jinja_environment() | ||
| obj = {'people': [{'first': 'James', 'last': 'd'}, | ||
| {'first': 'Jacob', 'last': 'e'}, | ||
| {'first': 'Jayden', 'last': 'f'}, | ||
| {'missing': 'different'}], | ||
| 'foo': {'bar': 'baz'}} | ||
|
|
||
| template = '{{ obj | jsonpath_query("people[*].first") }}' | ||
| actual_str = env.from_string(template).render({'obj': obj}) | ||
| actual = eval(actual_str) | ||
| expected = ['James', 'Jacob', 'Jayden'] | ||
| self.assertEqual(actual, expected) | ||
|
|
||
| def test_jsonpath_query_dynamic(self): | ||
| env = jinja_utils.get_jinja_environment() | ||
| obj = {'people': [{'first': 'James', 'last': 'd'}, | ||
| {'first': 'Jacob', 'last': 'e'}, | ||
| {'first': 'Jayden', 'last': 'f'}, | ||
| {'missing': 'different'}], | ||
| 'foo': {'bar': 'baz'}} | ||
| query = "people[*].last" | ||
|
|
||
| template = '{{ obj | jsonpath_query(query) }}' | ||
| actual_str = env.from_string(template).render({'obj': obj, | ||
| 'query': query}) | ||
| actual = eval(actual_str) | ||
| expected = ['d', 'e', 'f'] | ||
| self.assertEqual(actual, expected) | ||
|
|
||
| def test_jsonpath_query_no_results(self): | ||
| env = jinja_utils.get_jinja_environment() | ||
| obj = {'people': [{'first': 'James', 'last': 'd'}, | ||
| {'first': 'Jacob', 'last': 'e'}, | ||
| {'first': 'Jayden', 'last': 'f'}, | ||
| {'missing': 'different'}], | ||
| 'foo': {'bar': 'baz'}} | ||
| query = "query_returns_no_results" | ||
|
|
||
| template = '{{ obj | jsonpath_query(query) }}' | ||
| actual_str = env.from_string(template).render({'obj': obj, | ||
| 'query': query}) | ||
| actual = eval(actual_str) | ||
| expected = None | ||
| self.assertEqual(actual, expected) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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_stringis reading values out of the datastore. Commonly in ouractionmetadata 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
stringformat, 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_objectst2kv.set_objector something along those lines) and could maybe also work for some of your use cases.