Skip to content

Commit

Permalink
Watcher: Remove deprecated params.ctx
Browse files Browse the repository at this point in the history
When the script contexts were created in 6, the use of params.ctx was
deprecated. This commit cleans up that code and ensures that params.ctx
is null in both watcher script contexts.

Relates: elastic#34059
  • Loading branch information
hub-cap committed Dec 19, 2018
1 parent 667c06d commit 35ed56d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,14 @@
* A script to determine whether a watch should be run.
*/
public abstract class WatcherConditionScript {
public static final String[] PARAMETERS = {};

private static final Map<String, String> DEPRECATIONS;

static {
Map<String, String> deprecations = new HashMap<>();
deprecations.put(
"ctx",
"Accessing variable [ctx] via [params.ctx] from within a watcher_condition script " +
"is deprecated in favor of directly accessing [ctx]."
);
DEPRECATIONS = Collections.unmodifiableMap(deprecations);
}

private final Map<String, Object> params;
// TODO: ctx should have its members extracted into execute parameters, but it needs to be a member for bwc access in params
private final Map<String, Object> ctx;

public WatcherConditionScript(Map<String, Object> params, WatchExecutionContext watcherContext) {
Map<String, Object> paramsWithCtx = new HashMap<>(params);
Map<String, Object> ctx = Variables.createCtx(watcherContext, watcherContext.payload());
paramsWithCtx.put("ctx", ctx);
this.params = new ParameterMap(Collections.unmodifiableMap(paramsWithCtx), DEPRECATIONS);
this.ctx = ctx;
this.params = params;
this.ctx = Variables.createCtx(watcherContext, watcherContext.payload());;
}

public abstract boolean execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,14 @@
* A script to transform the results of a watch execution.
*/
public abstract class WatcherTransformScript {
public static final String[] PARAMETERS = {};

private static final Map<String, String> DEPRECATIONS;

static {
Map<String, String> deprecations = new HashMap<>();
deprecations.put(
"ctx",
"Accessing variable [ctx] via [params.ctx] from within a watcher_transform script " +
"is deprecated in favor of directly accessing [ctx]."
);
DEPRECATIONS = Collections.unmodifiableMap(deprecations);
}

private final Map<String, Object> params;
// TODO: ctx should have its members extracted into execute parameters, but it needs to be a member bwc access in params
private final Map<String, Object> ctx;

public WatcherTransformScript(Map<String, Object> params, WatchExecutionContext watcherContext, Payload payload) {
Map<String, Object> paramsWithCtx = new HashMap<>(params);
Map<String, Object> ctx = Variables.createCtx(watcherContext, payload);
paramsWithCtx.put("ctx", ctx);
this.params = new ParameterMap(Collections.unmodifiableMap(paramsWithCtx), DEPRECATIONS);
this.ctx = ctx;
this.params = params;
this.ctx = Variables.createCtx(watcherContext, payload);
}

public abstract Object execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void testScriptConditionAccessCtx() throws Exception {
assertThat(condition.execute(ctx).met(), is(true));
}

public void testParamsCtxDeprecated() throws Exception {
public void testParamsCtxNull() throws Exception {
WatchExecutionContext watcherContext = mock(WatchExecutionContext.class);
when(watcherContext.id()).thenReturn(mock(Wid.class));
when(watcherContext.watch()).thenReturn(mock(Watch.class));
Expand All @@ -216,13 +216,11 @@ public void testParamsCtxDeprecated() throws Exception {
WatcherConditionScript watcherScript = new WatcherConditionScript(Collections.emptyMap(), watcherContext) {
@Override
public boolean execute() {
assertThat(getParams().get("ctx"), is(getCtx()));
assertNull(getParams().get("ctx"));
return true;
}
};
watcherScript.execute();
assertWarnings("Accessing variable [ctx] via [params.ctx] from within a watcher_condition script " +
"is deprecated in favor of directly accessing [ctx].");
assertTrue(watcherScript.execute());
}

private static XContentBuilder createConditionContent(String script, String scriptLang, ScriptType scriptType) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,7 @@ public Object execute() {
return getParams().get("ctx");
}
};
assertThat(watcherScript.execute(), is(watcherScript.getCtx()));
assertWarnings("Accessing variable [ctx] via [params.ctx] from within a watcher_transform script " +
"is deprecated in favor of directly accessing [ctx].");
assertNull(watcherScript.execute());
}

static String scriptTypeField(ScriptType type) {
Expand Down

0 comments on commit 35ed56d

Please sign in to comment.