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

Scripting: Change keys for inline/stored scripts to source/id #25127

Merged
merged 10 commits into from
Jun 9, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void writeTo(StreamOutput out) throws IOException {
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
source.writeTo(out);
} else {
out.writeString(source.getCode());
out.writeString(source.getSource());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ public RestResponse buildResponse(GetStoredScriptResponse response, XContentBuil
if (lang == null) {
builder.startObject(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName());
builder.field(StoredScriptSource.LANG_PARSE_FIELD.getPreferredName(), source.getLang());
builder.field(StoredScriptSource.CODE_PARSE_FIELD.getPreferredName(), source.getCode());
builder.field(StoredScriptSource.CODE_PARSE_FIELD.getPreferredName(), source.getSource());

if (source.getOptions().isEmpty() == false) {
builder.field(StoredScriptSource.OPTIONS_PARSE_FIELD.getPreferredName(), source.getOptions());
}

builder.endObject();
} else {
builder.field(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName(), source.getCode());
builder.field(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName(), source.getSource());
}
}

Expand Down
30 changes: 21 additions & 9 deletions core/src/main/java/org/elasticsearch/script/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public final class Script implements ToXContentObject, Writeable {
*/
public static final ParseField SCRIPT_PARSE_FIELD = new ParseField("script");

/**
* Standard {@link ParseField} for source on the inner level.
*/
public static final ParseField SOURCE_PARSE_FIELD = new ParseField("source");

/**
* Standard {@link ParseField} for lang on the inner level.
*/
Expand Down Expand Up @@ -299,7 +304,10 @@ public static Script parse(XContentParser parser) throws IOException {
*
* {@code
* {
* "<type (inline, stored, file)>" : "<idOrCode>",
* // Exactly one of "id" or "source" must be specified
* "id" : "<id>",
* // OR
* "source": "<source>",
* "lang" : "<lang>",
* "options" : {
* "option0" : "<option0>",
Expand All @@ -317,7 +325,7 @@ public static Script parse(XContentParser parser) throws IOException {
* Example:
* {@code
* {
* "inline" : "return Math.log(doc.popularity) * params.multiplier",
* "source" : "return Math.log(doc.popularity) * params.multiplier",
* "lang" : "painless",
* "params" : {
* "multiplier" : 100.0
Expand All @@ -330,7 +338,7 @@ public static Script parse(XContentParser parser) throws IOException {
*
* {@code
* {
* "inline" : { "query" : ... },
* "source" : { "query" : ... },
* "lang" : "<lang>",
* "options" : {
* "option0" : "<option0>",
Expand Down Expand Up @@ -567,7 +575,7 @@ public void writeTo(StreamOutput out) throws IOException {
*
* {@code
* {
* "<type (inline, stored, file)>" : "<idOrCode>",
* "<(id, source)>" : "<idOrCode>",
* "lang" : "<lang>",
* "options" : {
* "option0" : "<option0>",
Expand All @@ -585,7 +593,7 @@ public void writeTo(StreamOutput out) throws IOException {
* Example:
* {@code
* {
* "inline" : "return Math.log(doc.popularity) * params.multiplier;",
* "source" : "return Math.log(doc.popularity) * params.multiplier;",
* "lang" : "painless",
* "params" : {
* "multiplier" : 100.0
Expand All @@ -600,7 +608,7 @@ public void writeTo(StreamOutput out) throws IOException {
*
* {@code
* {
* "inline" : { "query" : ... },
* "source" : { "query" : ... },
* "lang" : "<lang>",
* "options" : {
* "option0" : "<option0>",
Expand All @@ -621,10 +629,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params builderParams)

String contentType = options == null ? null : options.get(CONTENT_TYPE_OPTION);

if (type == ScriptType.INLINE && contentType != null && builder.contentType().mediaType().equals(contentType)) {
builder.rawField(type.getParseField().getPreferredName(), new BytesArray(idOrCode));
if (type == ScriptType.INLINE) {
if (contentType != null && builder.contentType().mediaType().equals(contentType)) {
builder.rawField(SOURCE_PARSE_FIELD.getPreferredName(), new BytesArray(idOrCode));
} else {
builder.field(SOURCE_PARSE_FIELD.getPreferredName(), idOrCode);
}
} else {
builder.field(type.getParseField().getPreferredName(), idOrCode);
builder.field("id", idOrCode);
}

if (lang != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public ScriptMetaData(StreamInput in) throws IOException {
throw new IllegalArgumentException("illegal stored script id [" + id + "], does not contain lang");
} else {
source = new StoredScriptSource(in);
source = new StoredScriptSource(id.substring(0, split), source.getCode(), Collections.emptyMap());
source = new StoredScriptSource(id.substring(0, split), source.getSource(), Collections.emptyMap());
}
// Version 5.3+ can just be parsed normally using StoredScriptSource.
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryTyp
// the script has been updated since the last compilation
StoredScriptSource source = getScriptFromClusterState(id, lang);
lang = source.getLang();
idOrCode = source.getCode();
idOrCode = source.getSource();
options = source.getOptions();
}

Expand Down Expand Up @@ -429,7 +429,7 @@ public void putStoredScript(ClusterService clusterService, PutStoredScriptReques
if (context == null) {
throw new IllegalArgumentException("Unknown context [" + request.context() + "]");
}
scriptEngine.compile(request.id(), source.getCode(), context, Collections.emptyMap());
scriptEngine.compile(request.id(), source.getSource(), context, Collections.emptyMap());
}
} catch (ScriptException good) {
throw good;
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/org/elasticsearch/script/ScriptType.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.common.io.stream.Writeable;

import java.io.IOException;
import java.util.Locale;

/**
* ScriptType represents the way a script is stored and retrieved from the {@link ScriptService}.
Expand All @@ -40,7 +41,7 @@ public enum ScriptType implements Writeable {
* (Groovy and others), but can be overridden by the specific {@link ScriptEngine}
* if the language is naturally secure (Painless, Mustache, and Expressions).
*/
INLINE ( 0 , new ParseField("inline") , false ),
INLINE ( 0 , new ParseField("source", "inline") , false ),

/**
* STORED scripts are saved as part of the {@link org.elasticsearch.cluster.ClusterState}
Expand All @@ -49,7 +50,7 @@ public enum ScriptType implements Writeable {
* (Groovy and others), but can be overridden by the specific {@link ScriptEngine}
* if the language is naturally secure (Painless, Mustache, and Expressions).
*/
STORED ( 1 , new ParseField("stored", "id") , false );
STORED ( 1 , new ParseField("id", "stored") , false );

/**
* Reads an int from the input stream and converts it to a {@link ScriptType}.
Expand Down Expand Up @@ -101,7 +102,7 @@ public int getId() {
* @return The unique name for this {@link ScriptType} based on the {@link ParseField}.
*/
public String getName() {
return parseField.getPreferredName();
return name().toLowerCase(Locale.ROOT);
}

/**
Expand Down
Loading