Search in sources :

Example 16 with Operation

use of org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation in project wso2-dss-connectors by wso2-attic.

the class MongoDBDataSource method decodeQuery.

private Object[] decodeQuery(String query) throws DataServiceFault {
    int i1 = query.indexOf('.');
    if (i1 == -1) {
        throw new DataServiceFault("The MongoDB Collection not specified in the query '" + query + "'");
    }
    String collection = query.substring(0, i1).trim();
    int i2 = query.indexOf('(', i1);
    if (i2 == -1 || i2 - i1 <= 1) {
        throw new DataServiceFault("Invalid MongoDB operation in the query '" + query + "'");
    }
    String operation = query.substring(i1 + 1, i2).trim();
    int i3 = query.lastIndexOf(')');
    if (i3 == -1) {
        throw new DataServiceFault("Invalid MongoDB operation in the query '" + query + "'");
    }
    String opQuery = null;
    if (i3 - i2 > 1) {
        opQuery = query.substring(i2 + 1, i3).trim();
    }
    MongoOperation mongoOp = this.convertToMongoOp(operation);
    if (mongoOp == MongoOperation.UPDATE) {
        List<Object> result = new ArrayList<Object>();
        result.add(collection);
        result.add(mongoOp);
        result.addAll(parseInsertQuery(opQuery));
        return result.toArray();
    } else {
        return new Object[] { collection, mongoOp, this.checkAndCleanOpQuery(opQuery) };
    }
}
Also used : MongoOperation(org.wso2.dss.connectors.mongodb.MongoDBDSConstants.MongoOperation) DataServiceFault(org.wso2.carbon.dataservices.core.DataServiceFault) ArrayList(java.util.ArrayList) DBObject(com.mongodb.DBObject)

Example 17 with Operation

use of org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation in project ballerina by ballerina-lang.

the class IterableAnalyzer method calculatedGivenOutputArgs.

private List<BType> calculatedGivenOutputArgs(Operation operation) {
    final List<BType> givenRetTypes;
    if (operation.lambdaType.getReturnTypes().isEmpty()) {
        givenRetTypes = Collections.emptyList();
        operation.outputType = symTable.voidType;
    } else {
        final BType returnType = operation.outputType = operation.lambdaType.getReturnTypes().get(0);
        if (returnType.tag == TypeTags.TUPLE) {
            givenRetTypes = ((BTupleType) returnType).tupleTypes;
        } else {
            givenRetTypes = operation.lambdaType.getReturnTypes();
        }
    }
    return givenRetTypes;
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 18 with Operation

use of org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation in project ballerina by ballerina-lang.

the class IterableAnalyzer method handlerIterableOperation.

public void handlerIterableOperation(BLangInvocation iExpr, BType expectedType, SymbolEnv env) {
    final IterableContext context;
    if (iExpr.expr.type.tag != TypeTags.INTERMEDIATE_COLLECTION) {
        // This is a new iteration chain.
        context = new IterableContext(iExpr.expr, env);
    } else {
        // Get context from previous invocation.
        context = ((BLangInvocation) iExpr.expr).iContext;
    }
    iExpr.iContext = context;
    final IterableKind iterableKind = IterableKind.getFromString(iExpr.name.value);
    final Operation iOperation = new Operation(iterableKind, iExpr, expectedType);
    iExpr.iContext.addOperation(iOperation);
    if (iterableKind.isLambdaRequired()) {
        handleLambdaBasedIterableOperation(context, iOperation);
    } else {
        handleSimpleTerminalOperations(iOperation);
    }
    validateIterableContext(context);
    if (iOperation.resultType != symTable.errType && context.foreachTypes.isEmpty()) {
        calculateForeachTypes(context);
    }
}
Also used : IterableContext(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext) IterableKind(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind) Operation(org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation)

Example 19 with Operation

use of org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation in project ballerina by ballerina-lang.

the class IterableAnalyzer method validateIterableContext.

public void validateIterableContext(IterableContext context) {
    final Operation lastOperation = context.operations.getLast();
    final BType expectedType = lastOperation.expectedType;
    final BType outputType = lastOperation.resultType;
    if (expectedType.tag == TypeTags.VOID && outputType.tag == TypeTags.VOID) {
        context.resultType = symTable.noType;
        return;
    }
    if (expectedType.tag == TypeTags.VOID) {
        // This error already logged.
        return;
    }
    if (expectedType == symTable.errType) {
        context.resultType = symTable.errType;
        return;
    }
    if (outputType.tag == TypeTags.VOID) {
        dlog.error(lastOperation.pos, DiagnosticCode.DOES_NOT_RETURN_VALUE, lastOperation.kind);
        context.resultType = symTable.errType;
        return;
    }
    // Calculate expected type, if this is an chained iterable operation.
    if (outputType.tag == TypeTags.INTERMEDIATE_COLLECTION) {
        BIntermediateCollectionType collectionType = (BIntermediateCollectionType) outputType;
        final BTupleType tupleType = collectionType.tupleType;
        if (expectedType.tag == TypeTags.ARRAY && tupleType.tupleTypes.size() == 1) {
            // Convert result into an array.
            context.resultType = new BArrayType(tupleType.tupleTypes.get(0));
            return;
        } else if (expectedType.tag == TypeTags.MAP && tupleType.tupleTypes.size() == 2 && tupleType.tupleTypes.get(0).tag == TypeTags.STRING) {
            // Convert result into a map.
            context.resultType = new BMapType(TypeTags.MAP, tupleType.tupleTypes.get(1), null);
            return;
        } else if (expectedType.tag == TypeTags.TABLE) {
            // 3. Whether the returned struct is compatible with the constraint struct of the expected type(table)
            if (tupleType.getTupleTypes().size() == 1 && tupleType.getTupleTypes().get(0).tag == TypeTags.STRUCT && types.isAssignable(tupleType.getTupleTypes().get(0), ((BTableType) expectedType).constraint)) {
                context.resultType = symTable.tableType;
            } else {
                context.resultType = types.checkType(lastOperation.pos, outputType, ((BTableType) expectedType).constraint, DiagnosticCode.INCOMPATIBLE_TYPES);
            }
            return;
        } else if (expectedType.tag == TypeTags.ANY) {
            context.resultType = symTable.errType;
            dlog.error(lastOperation.pos, DiagnosticCode.ITERABLE_RETURN_TYPE_MISMATCH, lastOperation.kind);
            return;
        } else if (expectedType.tag == TypeTags.NONE) {
            context.resultType = symTable.noType;
            return;
        }
    }
    // Validate compatibility with calculated and expected type.
    context.resultType = types.checkType(lastOperation.pos, outputType, expectedType, DiagnosticCode.INCOMPATIBLE_TYPES);
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) Operation(org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation) BIntermediateCollectionType(org.wso2.ballerinalang.compiler.semantics.model.types.BIntermediateCollectionType) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)

Example 20 with Operation

use of org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation in project ballerina by ballerina-lang.

the class IterableAnalyzer method assignOutputAndResultType.

private void assignOutputAndResultType(Operation op, List<BType> argTypes, List<BType> supportedRetTypes) {
    if (supportedRetTypes.isEmpty()) {
        op.outputType = op.resultType = symTable.voidType;
        return;
    }
    if (op.kind.isTerminal()) {
        op.outputType = op.resultType = supportedRetTypes.get(0);
        return;
    }
    if (op.kind == IterableKind.FILTER) {
        op.outputType = new BTupleType(argTypes);
        op.resultType = new BIntermediateCollectionType((BTupleType) op.outputType);
        return;
    }
    if (supportedRetTypes.size() == 1) {
        op.outputType = supportedRetTypes.get(0);
    } else {
        op.outputType = new BTupleType(supportedRetTypes);
    }
    op.resultType = new BIntermediateCollectionType(new BTupleType(supportedRetTypes));
}
Also used : BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) BIntermediateCollectionType(org.wso2.ballerinalang.compiler.semantics.model.types.BIntermediateCollectionType)

Aggregations

Test (org.testng.annotations.Test)34 HttpResponse (org.wso2.carbon.automation.test.utils.http.client.HttpResponse)29 HashMap (java.util.HashMap)19 ArrayList (java.util.ArrayList)16 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)15 HumanTaskRuntimeException (org.wso2.carbon.humantask.core.engine.runtime.api.HumanTaskRuntimeException)12 QueryVariable (org.wso2.carbon.bpmn.rest.engine.variable.QueryVariable)11 List (java.util.List)10 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)10 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)9 Map (java.util.Map)8 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)8 Operation (io.swagger.models.Operation)7 Attribute (org.wso2.charon3.core.attributes.Attribute)7 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)7 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)7 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)7 Operation (org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation)6 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 AttributeSchema (org.wso2.charon3.core.schema.AttributeSchema)6