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

ParamOverload annotation related improvements & bug fixes #1375

Merged
merged 3 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -24,6 +24,7 @@
import io.siddhi.annotation.util.DataType;
import io.siddhi.core.executor.ConstantExpressionExecutor;
import io.siddhi.core.executor.ExpressionExecutor;
import io.siddhi.core.util.SiddhiConstants;
import io.siddhi.core.util.parser.helper.AnnotationHelper;
import io.siddhi.query.api.definition.Attribute;
import io.siddhi.query.api.exception.SiddhiAppValidationException;
Expand All @@ -32,6 +33,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.stream.Collectors;

import static io.siddhi.core.util.SiddhiConstants.REPETITIVE_PARAMETER_NOTATION;

Expand Down Expand Up @@ -102,33 +105,35 @@ public static void validateExpressionExecutors(Object objectHavingAnnotation,
}
} else if (overloadParameterNames.length > 0 &&
overloadParameterNames[overloadParameterNames.length - 1].equals(REPETITIVE_PARAMETER_NOTATION)) {
boolean isExpectedParameterOverload = true;
for (int i = 0; i < attributeExpressionExecutors.length; i++) {
Parameter parameter = null;
String overloadParameterName = null;
if (i < overloadParameterNames.length - 1) {
overloadParameterName = overloadParameterNames[i];
} else {
overloadParameterName = overloadParameterNames[overloadParameterNames.length - 2];
}
parameter = parameterMap.get(overloadParameterName);
boolean supportedReturnType = false;
for (DataType type : parameter.type()) {
if (attributeExpressionExecutors[i].getReturnType().toString().
equalsIgnoreCase(type.toString())) {
supportedReturnType = true;
if (attributeExpressionExecutors.length > 0) {
boolean isExpectedParameterOverload = true;
for (int i = 0; i < attributeExpressionExecutors.length; i++) {
Parameter parameter = null;
String overloadParameterName = null;
if (i < overloadParameterNames.length - 1) {
overloadParameterName = overloadParameterNames[i];
} else {
overloadParameterName = overloadParameterNames[overloadParameterNames.length - 2];
}
parameter = parameterMap.get(overloadParameterName);
boolean supportedReturnType = false;
for (DataType type : parameter.type()) {
if (attributeExpressionExecutors[i].getReturnType().toString().
equalsIgnoreCase(type.toString())) {
supportedReturnType = true;
break;
}
}
if (!supportedReturnType) {
isExpectedParameterOverload = false;
break;
}
}
if (!supportedReturnType) {
isExpectedParameterOverload = false;
if (isExpectedParameterOverload) {
parameterOverload = aParameterOverload;
break;
}
}
if (isExpectedParameterOverload) {
parameterOverload = aParameterOverload;
break;
}
}
}

Expand All @@ -138,8 +143,11 @@ public static void validateExpressionExecutors(Object objectHavingAnnotation,
for (ExpressionExecutor expressionExecutor : attributeExpressionExecutors) {
returnTypes.add(expressionExecutor.getReturnType());
}
String formattedParamOverloadString = getSupportedParamOverloads(parameterMap, parameterOverloads);
throw new SiddhiAppValidationException("There is no parameterOverload for '" + key +
"' that matches attribute types '" + returnTypes + "'.");
"' that matches attribute types '" + returnTypes.stream()
.map(String::valueOf).collect(Collectors.joining(", ", "<", ">")) +
"'. Supported parameter overloads are " + formattedParamOverloadString + ".");
} else {
if (mandatoryCount > attributeExpressionExecutors.length) {
throw new SiddhiAppValidationException("The '" + key + "' expects at least " + mandatoryCount +
Expand All @@ -162,5 +170,39 @@ public static void validateExpressionExecutors(Object objectHavingAnnotation,
}
}
}

private static String getSupportedParamOverloads(Map<String, Parameter> parameterMap,
ParameterOverload[] parameterOverloads) {
StringJoiner stringJoiner = new StringJoiner(", ");
for (ParameterOverload parameterOverload : parameterOverloads) {
String[] parameterNames = parameterOverload.parameterNames();
if (parameterNames.length != 0) {
StringJoiner paramOverloadStringJoiner = new StringJoiner(", ", "(", ")");
for (int i = 0; i < parameterNames.length; i++) {
StringBuilder stringBuilder = new StringBuilder();
if (!SiddhiConstants.REPETITIVE_PARAMETER_NOTATION.equals(parameterNames[i])) {
stringBuilder.append(getFormattedStringForDataType(parameterMap.
get(parameterNames[i]).type()));
stringBuilder.append(" ").append(parameterNames[i]);
} else {
stringBuilder.append(getFormattedStringForDataType(parameterMap.
get(parameterNames[i - 1]).type()));
stringBuilder.append(" ").append(SiddhiConstants.REPETITIVE_PARAMETER_NOTATION);
}
paramOverloadStringJoiner.add(stringBuilder);
}
stringJoiner.add(paramOverloadStringJoiner.toString());
}
}
return stringJoiner.toString();
}

private static String getFormattedStringForDataType(DataType[] dataTypes) {
StringJoiner stringJoiner = new StringJoiner("|", "<", ">");
for (DataType dataType : dataTypes) {
stringJoiner.add(dataType.name());
}
return stringJoiner.toString();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public void delayWindowTest1() {
} catch (SiddhiAppCreationException e) {
error = false;
AssertJUnit.assertEquals("There is no parameterOverload for 'delay' that matches attribute types " +
"'[INT, INT]'.", e.getCause().getMessage());
"'<INT, INT>'. Supported parameter overloads are (<INT|LONG|TIME> window.delay).",
e.getCause().getMessage());
throw e;
} finally {
if (siddhiAppRuntime != null) {
Expand All @@ -106,7 +107,8 @@ public void delayWindowTest2() {
} catch (SiddhiAppCreationException e) {
error = false;
AssertJUnit.assertEquals("There is no parameterOverload for 'delay' that matches attribute types " +
"'[STRING]'.", e.getCause().getMessage());
"'<STRING>'. Supported parameter overloads are (<INT|LONG|TIME> window.delay).",
e.getCause().getMessage());
throw e;
} finally {
if (siddhiAppRuntime != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ public void testSessionWindow1() {

} catch (SiddhiAppCreationException e) {
AssertJUnit.assertEquals("There is no parameterOverload for 'session' that matches attribute types " +
"'[LONG, STRING, LONG, INT]'.", e.getCause().getMessage());
"'<LONG, STRING, LONG, INT>'. Supported parameter overloads are " +
"(<INT|LONG|TIME> window.session), " +
"(<INT|LONG|TIME> window.session, <STRING> window.key), (<INT|LONG|TIME> window.session, " +
"<STRING> window.key, <INT|LONG|TIME> window.allowed.latency).", e.getCause().getMessage());
throw e;
} finally {
if (siddhiAppRuntime != null) {
Expand Down Expand Up @@ -150,7 +153,9 @@ public void testSessionWindow3() {

} catch (SiddhiAppCreationException e) {
AssertJUnit.assertEquals("There is no parameterOverload for 'session' that matches attribute types " +
"'[STRING, STRING, LONG]'.", e.getCause().getMessage());
"'<STRING, STRING, LONG>'. Supported parameter overloads are (<INT|LONG|TIME> window.session), " +
"(<INT|LONG|TIME> window.session, <STRING> window.key), (<INT|LONG|TIME> window.session, " +
"<STRING> window.key, <INT|LONG|TIME> window.allowed.latency).", e.getCause().getMessage());
throw e;
} finally {
if (siddhiAppRuntime != null) {
Expand Down Expand Up @@ -215,7 +220,9 @@ public void testSessionWindow5() {

} catch (SiddhiAppCreationException e) {
AssertJUnit.assertEquals("There is no parameterOverload for 'session' that matches attribute types " +
"'[LONG, INT, LONG]'.", e.getCause().getMessage());
"'<LONG, INT, LONG>'. Supported parameter overloads are (<INT|LONG|TIME> window.session), " +
"(<INT|LONG|TIME> window.session, <STRING> window.key), (<INT|LONG|TIME> window.session, " +
"<STRING> window.key, <INT|LONG|TIME> window.allowed.latency).", e.getCause().getMessage());
throw e;
} finally {
if (siddhiAppRuntime != null) {
Expand Down Expand Up @@ -279,7 +286,10 @@ public void testSessionWindow7() {

} catch (SiddhiAppCreationException e) {
AssertJUnit.assertEquals("There is no parameterOverload for 'session' that matches attribute " +
"types '[LONG, STRING, STRING]'.", e.getCause().getMessage());
"types '<LONG, STRING, STRING>'. Supported parameter overloads are " +
"(<INT|LONG|TIME> window.session), (<INT|LONG|TIME> window.session, <STRING> window.key), " +
"(<INT|LONG|TIME> window.session, <STRING> window.key, <INT|LONG|TIME> window.allowed.latency).",
e.getCause().getMessage());
throw e;
} finally {
if (siddhiAppRuntime != null) {
Expand Down Expand Up @@ -312,7 +322,9 @@ public void testSessionWindow8() {

} catch (SiddhiAppCreationException e) {
AssertJUnit.assertEquals("There is no parameterOverload for 'session' that matches attribute types" +
" '[LONG, INT]'.", e.getCause().getMessage());
" '<LONG, INT>'. Supported parameter overloads are (<INT|LONG|TIME> window.session), " +
"(<INT|LONG|TIME> window.session, <STRING> window.key), (<INT|LONG|TIME> window.session, " +
"<STRING> window.key, <INT|LONG|TIME> window.allowed.latency).", e.getCause().getMessage());
throw e;
} finally {
if (siddhiAppRuntime != null) {
Expand Down Expand Up @@ -377,7 +389,9 @@ public void testSessionWindow10() {

} catch (SiddhiAppCreationException e) {
AssertJUnit.assertEquals("There is no parameterOverload for 'session' that matches attribute types " +
"'[LONG, LONG]'.", e.getCause().getMessage());
"'<LONG, LONG>'. Supported parameter overloads are (<INT|LONG|TIME> window.session), " +
"(<INT|LONG|TIME> window.session, <STRING> window.key), (<INT|LONG|TIME> window.session, " +
"<STRING> window.key, <INT|LONG|TIME> window.allowed.latency).", e.getCause().getMessage());
throw e;
} finally {
if (siddhiAppRuntime != null) {
Expand Down