Search in sources :

Example 36 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.

the class ParserUtils method createNewStruct.

/**
 * Create new struct.
 *
 * @param name   name of the struct
 * @param fields field definiton statements
 * @return {Function} function
 */
private static Struct createNewStruct(String name, List<BLangVariable> fields, String fileName) {
    Struct struct = new Struct(name);
    fields.forEach((field) -> {
        String defaultValue = null;
        if (field.getInitialExpression() != null) {
            defaultValue = ((BLangLiteral) field.getInitialExpression()).getValue().toString();
        }
        StructField structField = createNewStructField(field.getName().getValue(), field.getTypeNode().type.toString(), defaultValue);
        struct.addStructField(structField);
    });
    struct.setFileName(fileName);
    return struct;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) StructField(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.StructField) Struct(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Struct) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct)

Example 37 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.

the class ParserUtils method createNewFunction.

/**
 * Create new function.
 *
 * @param name         name of the function
 * @param annotations  list of annotations
 * @param params       list of parameters
 * @param returnParams list of return params
 * @return {Function} function
 */
private static Function createNewFunction(String name, List<AnnotationAttachment> annotations, List<Parameter> params, List<Parameter> returnParams, String receiverType, boolean isPublic, String fileName) {
    Function function = new Function();
    function.setName(name);
    function.setAnnotations(annotations);
    function.setParameters(params);
    function.setReturnParams(returnParams);
    function.setReceiverType(receiverType);
    function.setPublic(isPublic);
    function.setFileName(fileName);
    return function;
}
Also used : Function(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Function) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction)

Example 38 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.

the class ParserUtils method removeConstructsOfFile.

/**
 * Remove constructs from given file in given package from given package map.
 *
 * @param pkgName    Name of the package
 * @param fileName   Name of the File
 * @param packageMap Package constructs map
 */
public static void removeConstructsOfFile(String pkgName, String fileName, Map<String, ModelPackage> packageMap) {
    ModelPackage newPkg = new ModelPackage();
    newPkg.setName(pkgName);
    if (packageMap.containsKey(pkgName)) {
        ModelPackage currentPkg = packageMap.get(pkgName);
        currentPkg.getFunctions().forEach((Function func) -> {
            if (!func.getFileName().equals(fileName)) {
                newPkg.addFunctionsItem(func);
            }
        });
        currentPkg.getStructs().forEach((Struct struct) -> {
            if (!struct.getFileName().equals(fileName)) {
                newPkg.addStructsItem(struct);
            }
        });
        currentPkg.getAnnotations().forEach((AnnotationDef annotation) -> {
            if (!annotation.getFileName().equals(fileName)) {
                newPkg.addAnnotationsItem(annotation);
            }
        });
        currentPkg.getConnectors().forEach((Connector connector) -> {
            if (!connector.getFileName().equals(fileName)) {
                newPkg.addConnectorsItem(connector);
            }
        });
        packageMap.remove(pkgName);
        packageMap.put(pkgName, newPkg);
    }
}
Also used : Function(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Function) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Connector(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Connector) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage) Struct(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Struct) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) AnnotationDef(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.AnnotationDef)

Example 39 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.

the class SymbolEnter method addInitReturnStatement.

private void addInitReturnStatement(BLangBlockStmt bLangBlockStmt) {
    // Add return statement to the init function
    BLangReturn returnStmt = (BLangReturn) TreeBuilder.createReturnNode();
    returnStmt.pos = bLangBlockStmt.pos;
    bLangBlockStmt.addStatement(returnStmt);
}
Also used : BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)

Example 40 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.

the class SymbolEnter method validateFunctionsAttachedToStructs.

private void validateFunctionsAttachedToStructs(BLangFunction funcNode, BInvokableSymbol funcSymbol, SymbolEnv invokableEnv) {
    BInvokableType funcType = (BInvokableType) funcSymbol.type;
    BStructSymbol structSymbol = (BStructSymbol) funcNode.receiver.type.tsymbol;
    BSymbol symbol = symResolver.lookupMemberSymbol(funcNode.receiver.pos, structSymbol.scope, invokableEnv, names.fromIdNode(funcNode.name), SymTag.VARIABLE);
    if (symbol != symTable.notFoundSymbol) {
        dlog.error(funcNode.pos, DiagnosticCode.STRUCT_FIELD_AND_FUNC_WITH_SAME_NAME, funcNode.name.value, funcNode.receiver.type.toString());
        return;
    }
    BStructType structType = (BStructType) funcNode.receiver.type;
    BAttachedFunction attachedFunc = new BAttachedFunction(names.fromIdNode(funcNode.name), funcSymbol, funcType);
    structSymbol.attachedFuncs.add(attachedFunc);
    if (funcNode.name.value.equals(structType.tsymbol.name.value + Names.INIT_FUNCTION_SUFFIX.value)) {
        structSymbol.defaultsValuesInitFunc = attachedFunc;
        return;
    }
    // Check whether this attached function is a struct initializer.
    if (!structType.tsymbol.name.value.equals(funcNode.name.value)) {
        // Not a struct initializer.
        return;
    }
    if (!funcNode.requiredParams.isEmpty() || !funcNode.retParams.isEmpty()) {
        dlog.error(funcNode.pos, DiagnosticCode.INVALID_STRUCT_INITIALIZER_FUNCTION, funcNode.name.value, funcNode.receiver.type.toString());
    }
    structSymbol.initializerFunc = attachedFunc;
}
Also used : BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BAttachedFunction(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Aggregations

Test (org.testng.annotations.Test)94 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)49 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)44 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)39 ArrayList (java.util.ArrayList)37 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)35 BString (org.ballerinalang.model.values.BString)30 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)29 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)26 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)25 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)20 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)20 BJSON (org.ballerinalang.model.values.BJSON)19 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)19 List (java.util.List)18 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)18 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)16 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)15 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)14 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)14