This is an automated email from the ASF dual-hosted git repository.
zihaoxiang pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git
The following commit(s) were added to refs/heads/dev by this push:
new b97f6fcb26 [Improvement-16534][Master] Switch task support includes
method (#16594)
b97f6fcb26 is described below
commit b97f6fcb2651d2561b1b108d8c34d4a42aad4db1
Author: Terry Tao <[email protected]>
AuthorDate: Fri Sep 27 09:10:26 2024 +0800
[Improvement-16534][Master] Switch task support includes method (#16594)
---
.../server/master/utils/SwitchTaskUtils.java | 29 ++++++++++++++++++++++
.../server/master/utils/SwitchTaskUtilsTest.java | 12 +++++++++
2 files changed, 41 insertions(+)
diff --git
a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java
b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java
index 1676df7e01..6ee509a1db 100644
---
a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java
+++
b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java
@@ -40,9 +40,38 @@ public class SwitchTaskUtils {
private static final NashornSandbox sandbox;
private static final String rgex = "['\"]*\\$\\{(.*?)\\}['\"]*";
+ public static final String NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES =
+ "if (!Array.prototype.includes) {" +
+ " Object.defineProperty(Array.prototype, 'includes', {" +
+ " value: function(valueToFind, fromIndex) {" +
+ " if (this == null) {" +
+ " throw new TypeError('\"this\" is null or
not defined');" +
+ " }" +
+ " var o = Object(this);" +
+ " var len = o.length >>> 0;" +
+ " if (len === 0) { return false; }" +
+ " var n = fromIndex | 0;" +
+ " var k = Math.max(n >= 0 ? n : len -
Math.abs(n), 0);" +
+ " function sameValueZero(x, y) {" +
+ " return x === y || (typeof x === 'number'
&& " +
+ " typeof y === 'number' && isNaN(x) &&
isNaN(y));" +
+ " }" +
+ " while (k < len) {" +
+ " if (sameValueZero(o[k], valueToFind)) {
return true; }" +
+ " k++;" +
+ " }" +
+ " return false;" +
+ " }" +
+ " });" +
+ "}";
static {
sandbox = NashornSandboxes.create();
+ try {
+ sandbox.eval(NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES);
+ } catch (ScriptException e) {
+ log.error("failed to load Nashorn polyfill", e);
+ }
}
public static boolean evaluate(String expression) throws ScriptException {
diff --git
a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtilsTest.java
b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtilsTest.java
index 34785ada47..81b8b1bc9c 100644
---
a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtilsTest.java
+++
b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtilsTest.java
@@ -71,4 +71,16 @@ public class SwitchTaskUtilsTest {
});
}
+
+ @Test
+ public void testIncludes() throws ScriptException {
+ String content = "['abc','def'].includes('abc')";
+ boolean result = SwitchTaskUtils.evaluate(content);
+ Assertions.assertTrue(result);
+
+
SwitchTaskUtils.evaluate(SwitchTaskUtils.NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES);
+ result = SwitchTaskUtils.evaluate(content);
+ Assertions.assertTrue(result);
+ }
+
}