Search in sources :

Example 1 with DataType

use of io.siddhi.annotation.util.DataType in project siddhi by wso2.

the class InputParameterValidator method validateExpressionExecutors.

/**
 * The method which validates the extension specific parameters of siddhi App with the pattern specified in the
 * {@link ParameterOverload} annotation in the extension class
 *
 * @param objectHavingAnnotation       the object which has Extension annotation
 * @param attributeExpressionExecutors the executors of each function parameters
 * @throws SiddhiAppValidationException SiddhiAppValidation exception
 */
public static void validateExpressionExecutors(Object objectHavingAnnotation, ExpressionExecutor[] attributeExpressionExecutors) throws SiddhiAppValidationException {
    Extension annotation = objectHavingAnnotation.getClass().getAnnotation(Extension.class);
    if (annotation == null) {
        return;
    }
    ParameterOverload[] parameterOverloads = annotation.parameterOverloads();
    Parameter[] parameters = annotation.parameters();
    String key = AnnotationHelper.createAnnotationKey(annotation);
    // Count the mandatory number of parameters specified in @Extension
    int mandatoryCount = 0;
    Map<String, Parameter> parameterMap = new HashMap<>();
    for (Parameter parameter : parameters) {
        if (!parameter.optional()) {
            mandatoryCount++;
        }
        parameterMap.put(parameter.name(), parameter);
    }
    // Find the parameterOverLoad
    ParameterOverload parameterOverload = null;
    for (ParameterOverload aParameterOverload : parameterOverloads) {
        String[] overloadParameterNames = aParameterOverload.parameterNames();
        if (overloadParameterNames.length == attributeExpressionExecutors.length && (overloadParameterNames.length == 0 || !overloadParameterNames[overloadParameterNames.length - 1].equals(REPETITIVE_PARAMETER_NOTATION))) {
            boolean isExpectedParameterOverload = true;
            for (int i = 0; i < overloadParameterNames.length; i++) {
                String overloadParameterName = overloadParameterNames[i];
                Parameter 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 (isExpectedParameterOverload) {
                parameterOverload = aParameterOverload;
                break;
            }
        } else if (overloadParameterNames.length - 1 <= attributeExpressionExecutors.length && overloadParameterNames.length > 0 && overloadParameterNames[overloadParameterNames.length - 1].equals(REPETITIVE_PARAMETER_NOTATION)) {
            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 (isExpectedParameterOverload) {
                    parameterOverload = aParameterOverload;
                    break;
                }
            }
        }
    }
    if (parameterOverload == null) {
        if (parameterOverloads.length > 0) {
            List<Attribute.Type> returnTypes = new ArrayList<>();
            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.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 + " parameters, but found only " + attributeExpressionExecutors.length + " input parameters.");
            }
        }
    } else {
        String[] overloadParameterNames = parameterOverload.parameterNames();
        for (int i = 0; i < overloadParameterNames.length; i++) {
            String overloadParameterName = overloadParameterNames[i];
            Parameter parameter = parameterMap.get(overloadParameterName);
            if (parameter != null && !parameter.dynamic() && !(attributeExpressionExecutors[i] instanceof ConstantExpressionExecutor)) {
                throw new SiddhiAppValidationException("The '" + key + "' expects input parameter '" + parameter.name() + "' at position '" + i + "' to be static," + " but found a dynamic attribute.");
            }
        }
    }
}
Also used : ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) Extension(io.siddhi.annotation.Extension) DataType(io.siddhi.annotation.util.DataType) Parameter(io.siddhi.annotation.Parameter) DataType(io.siddhi.annotation.util.DataType) ParameterOverload(io.siddhi.annotation.ParameterOverload)

Aggregations

Extension (io.siddhi.annotation.Extension)1 Parameter (io.siddhi.annotation.Parameter)1 ParameterOverload (io.siddhi.annotation.ParameterOverload)1 DataType (io.siddhi.annotation.util.DataType)1 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)1 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)1 SiddhiAppValidationException (io.siddhi.query.api.exception.SiddhiAppValidationException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1