-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Scripting: JSON parsing and writing in watcher #63278
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
16 commits
Select commit
Hold shift + click to select a range
ad2cc26
Json support in painless
honzakral 115b2a4
WIP JSON spi
stu-elastic 99288fd
Json SPI for watcher
stu-elastic 9af7d0a
Merge branch 'master' into honza-painless-json
jdconrad 42a864a
Merge branch 'master' of github.com:elastic/elasticsearch into honza-…
stu-elastic 722baec
add unit test to painless and rest tests to watcher
jdconrad 8a32b5d
Merge branch 'honza-painless-json' of github.com:stu-elastic/elastics…
stu-elastic d737de7
Update context docs
stu-elastic c014e1b
Revert asciidoc changes
stu-elastic 0c880a5
Merge revert asciidoc
stu-elastic 7feb0d9
Remove rt field asciidoc stuff
stu-elastic cfc9f0a
Merge branch 'master' of github.com:elastic/elasticsearch into honza-…
stu-elastic ba6a145
Javadocs for json class
stu-elastic 02e786f
Merge branch 'master' of github.com:elastic/elasticsearch into honza-…
stu-elastic f444ec7
Merge branch 'master' of github.com:elastic/elasticsearch into honza-…
stu-elastic f5111da
Merge hash
stu-elastic 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
75 changes: 75 additions & 0 deletions
75
modules/lang-painless/src/main/java/org/elasticsearch/painless/api/Json.java
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,75 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch 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. | ||
| */ | ||
|
|
||
| package org.elasticsearch.painless.api; | ||
|
|
||
| import org.elasticsearch.common.xcontent.DeprecationHandler; | ||
| import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.common.xcontent.json.JsonXContent; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class Json { | ||
| /** | ||
| * Load a string as the Java version of a JSON type, either List (JSON array), Map (JSON object), Number, Boolean or String | ||
| */ | ||
| public static Object load(String json) throws IOException{ | ||
| XContentParser parser = JsonXContent.jsonXContent.createParser( | ||
| NamedXContentRegistry.EMPTY, | ||
| DeprecationHandler.THROW_UNSUPPORTED_OPERATION, | ||
| json); | ||
|
|
||
| switch (parser.nextToken()) { | ||
| case START_ARRAY: | ||
| return parser.list(); | ||
| case START_OBJECT: | ||
| return parser.map(); | ||
| case VALUE_NUMBER: | ||
| return parser.numberValue(); | ||
| case VALUE_BOOLEAN: | ||
| return parser.booleanValue(); | ||
| case VALUE_STRING: | ||
| return parser.text(); | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Write a JSON representable type as a string | ||
| */ | ||
| public static String dump(Object data) throws IOException { | ||
| return dump(data, false); | ||
| } | ||
|
|
||
| /** | ||
| * Write a JSON representable type as a string, optionally pretty print it by spanning multiple lines and indenting | ||
| */ | ||
| public static String dump(Object data, boolean pretty) throws IOException { | ||
| XContentBuilder builder = JsonXContent.contentBuilder(); | ||
| if (pretty) { | ||
| builder.prettyPrint(); | ||
| } | ||
| builder.value(data); | ||
| builder.flush(); | ||
| return builder.getOutputStream().toString(); | ||
| } | ||
| } | ||
50 changes: 50 additions & 0 deletions
50
modules/lang-painless/src/test/java/org/elasticsearch/painless/JsonTests.java
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,50 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch 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. | ||
| */ | ||
|
|
||
| package org.elasticsearch.painless; | ||
|
|
||
| import static java.util.Collections.singletonList; | ||
| import static java.util.Collections.singletonMap; | ||
|
|
||
| public class JsonTests extends ScriptTestCase { | ||
|
|
||
| public void testDump() { | ||
| // simple object dump | ||
| Object output = exec("Json.dump(params.data)", singletonMap("data", singletonMap("hello", "world")), true); | ||
| assertEquals("{\"hello\":\"world\"}", output); | ||
|
|
||
| output = exec("Json.dump(params.data)", singletonMap("data", singletonList(42)), true); | ||
| assertEquals("[42]", output); | ||
|
|
||
| // pretty print | ||
| output = exec("Json.dump(params.data, true)", singletonMap("data", singletonMap("hello", "world")), true); | ||
| assertEquals("{\n \"hello\" : \"world\"\n}", output); | ||
| } | ||
|
|
||
| public void testLoad() { | ||
| String json = "{\"hello\":\"world\"}"; | ||
| Object output = exec("Json.load(params.json)", singletonMap("json", json), true); | ||
| assertEquals(singletonMap("hello", "world"), output); | ||
|
|
||
| json = "[42]"; | ||
| output = exec("Json.load(params.json)", singletonMap("json", json), true); | ||
| assertEquals(singletonList(42), output); | ||
| } | ||
|
|
||
| } |
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
58 changes: 58 additions & 0 deletions
58
...tcher/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/painless/70_json_in_watch.yml
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,58 @@ | ||
| --- | ||
| "Test json in watch": | ||
|
|
||
| - do: | ||
| cluster.health: | ||
| wait_for_status: green | ||
|
|
||
| - do: | ||
| watcher.put_watch: | ||
| id: "my_json_watch" | ||
| body: > | ||
| { | ||
| "trigger" : { | ||
| "schedule" : { "cron" : "0 0 0 1 * ? 2099" } | ||
| }, | ||
| "input" : { | ||
| "simple" : { | ||
| "count" : 1 | ||
| } | ||
| }, | ||
| "condition" : { | ||
| "script" : { | ||
| "source" : "String s = Json.dump(ctx.payload); Map m = Json.load(s); m.count == 1;", | ||
| "lang" : "painless" | ||
| } | ||
| }, | ||
| "transform" : { | ||
| "script": "String s = Json.dump(ctx.payload); Map m = Json.load(s); return ['test': m];" | ||
| }, | ||
| "actions" : { | ||
| "logging" : { | ||
| "logging" : { | ||
| "text" : "{{ctx.payload.test.count}}" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| - match: { _id: "my_json_watch" } | ||
|
|
||
| - do: | ||
| watcher.execute_watch: | ||
| id: "my_json_watch" | ||
|
|
||
| - match: { "watch_record.watch_id": "my_json_watch" } | ||
| - match: { "watch_record.state": "executed" } | ||
| - match: { "watch_record.result.input.type": "simple" } | ||
| - match: { "watch_record.result.input.status": "success" } | ||
| - match: { "watch_record.result.input.payload.count": 1 } | ||
| - match: { "watch_record.result.condition.type": "script" } | ||
| - match: { "watch_record.result.condition.status": "success" } | ||
| - match: { "watch_record.result.condition.met": true } | ||
| - match: { "watch_record.result.transform.type": "script" } | ||
| - match: { "watch_record.result.transform.status": "success" } | ||
| - match: { "watch_record.result.transform.payload.test.count": 1 } | ||
| - match: { "watch_record.result.actions.0.id" : "logging" } | ||
| - match: { "watch_record.result.actions.0.type" : "logging" } | ||
| - match: { "watch_record.result.actions.0.status" : "success" } | ||
| - match: { "watch_record.result.actions.0.logging.logged_text" : "1" } |
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.
Uh oh!
There was an error while loading. Please reload this page.