Search in sources :

Example 6 with BLangRecordLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral in project ballerina by ballerina-lang.

the class Generator method annotFieldAnnotation.

/**
 * Get description annotation of the annotation attribute.
 * @param annotationNode parent node.
 * @param annotAttribute annotation attribute.
 * @return description of the annotation attribute.
 */
private static String annotFieldAnnotation(BLangAnnotation annotationNode, BLangAnnotAttribute annotAttribute) {
    List<? extends AnnotationAttachmentNode> annotationAttachments = getAnnotationAttachments(annotationNode);
    for (AnnotationAttachmentNode annotation : annotationAttachments) {
        if ("Field".equals(annotation.getAnnotationName().getValue())) {
            BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
            BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
            String value = bLangLiteral.toString();
            if (value.startsWith(annotAttribute.getName().getValue())) {
                String[] valueParts = value.split(":");
                return valueParts.length == 2 ? valueParts[1] : valueParts[0];
            }
        }
    }
    return "";
}
Also used : BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 7 with BLangRecordLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral in project ballerina by ballerina-lang.

the class ServiceProtoUtils method getServiceConfiguration.

static ServiceConfiguration getServiceConfiguration(ServiceNode serviceNode) {
    String rpcEndpoint = null;
    boolean clientStreaming = false;
    boolean serverStreaming = false;
    boolean generateClientConnector = false;
    for (AnnotationAttachmentNode annotationNode : serviceNode.getAnnotationAttachments()) {
        if (!ServiceProtoConstants.ANN_SERVICE_CONFIG.equals(annotationNode.getAnnotationName().getValue())) {
            continue;
        }
        if (annotationNode.getExpression() instanceof BLangRecordLiteral) {
            List<BLangRecordLiteral.BLangRecordKeyValue> attributes = ((BLangRecordLiteral) annotationNode.getExpression()).getKeyValuePairs();
            for (BLangRecordLiteral.BLangRecordKeyValue attributeNode : attributes) {
                String attributeName = attributeNode.getKey().toString();
                String attributeValue = attributeNode.getValue() != null ? attributeNode.getValue().toString() : null;
                switch(attributeName) {
                    case ServiceProtoConstants.SERVICE_CONFIG_RPC_ENDPOINT:
                        {
                            rpcEndpoint = attributeValue != null ? attributeValue : null;
                            break;
                        }
                    case ServiceProtoConstants.SERVICE_CONFIG_CLIENT_STREAMING:
                        {
                            clientStreaming = attributeValue != null && Boolean.parseBoolean(attributeValue);
                            break;
                        }
                    case ServiceProtoConstants.SERVICE_CONFIG_SERVER_STREAMING:
                        {
                            serverStreaming = attributeValue != null && Boolean.parseBoolean(attributeValue);
                            break;
                        }
                    case ServiceProtoConstants.SERVICE_CONFIG_GENERATE_CLIENT:
                        {
                            generateClientConnector = attributeValue != null && Boolean.parseBoolean(attributeValue);
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }
        }
    }
    return new ServiceConfiguration(rpcEndpoint, clientStreaming, serverStreaming, generateClientConnector);
}
Also used : ServiceConfiguration(org.ballerinalang.net.grpc.config.ServiceConfiguration) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 8 with BLangRecordLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral in project ballerina by ballerina-lang.

the class SignatureHelpUtil method getSignatureInfoModel.

/**
 * Get the required signature information filled model.
 *
 * @param bInvokableSymbol                  Invokable symbol
 * @param signatureContext                  Signature operation context
 * @return {@link SignatureInfoModel}       SignatureInfoModel containing signature information
 */
private static SignatureInfoModel getSignatureInfoModel(BInvokableSymbol bInvokableSymbol, TextDocumentServiceContext signatureContext) {
    Map<String, String> paramDescMap = new HashMap<>();
    SignatureInfoModel signatureInfoModel = new SignatureInfoModel();
    List<ParameterInfoModel> paramModels = new ArrayList<>();
    String functionName = signatureContext.get(SignatureKeys.CALLABLE_ITEM_NAME);
    CompilerContext compilerContext = signatureContext.get(DocumentServiceKeys.COMPILER_CONTEXT_KEY);
    BLangPackage bLangPackage = signatureContext.get(DocumentServiceKeys.LS_PACKAGE_CACHE_KEY).findPackage(compilerContext, bInvokableSymbol.pkgID);
    BLangFunction blangFunction = bLangPackage.getFunctions().stream().filter(bLangFunction -> bLangFunction.getName().getValue().equals(functionName)).findFirst().orElse(null);
    if (!blangFunction.getDocumentationAttachments().isEmpty()) {
        // Get the first documentation attachment
        BLangDocumentation bLangDocumentation = blangFunction.getDocumentationAttachments().get(0);
        signatureInfoModel.setSignatureDescription(bLangDocumentation.documentationText.trim());
        bLangDocumentation.attributes.forEach(attribute -> {
            if (attribute.docTag.equals(DocTag.PARAM)) {
                paramDescMap.put(attribute.documentationField.getValue(), attribute.documentationText.trim());
            }
        });
    } else {
        // TODO: Should be deprecated in due course
        // Iterate over the attachments list and extract the attachment Description Map
        blangFunction.getAnnotationAttachments().forEach(annotationAttachment -> {
            BLangExpression expr = annotationAttachment.expr;
            if (expr instanceof BLangRecordLiteral) {
                List<BLangRecordLiteral.BLangRecordKeyValue> recordKeyValues = ((BLangRecordLiteral) expr).keyValuePairs;
                for (BLangRecordLiteral.BLangRecordKeyValue recordKeyValue : recordKeyValues) {
                    BLangExpression key = recordKeyValue.key.expr;
                    BLangExpression value = recordKeyValue.getValue();
                    if (key instanceof BLangSimpleVarRef && ((BLangSimpleVarRef) key).getVariableName().getValue().equals("value") && value instanceof BLangLiteral) {
                        String annotationValue = ((BLangLiteral) value).getValue().toString();
                        if (annotationAttachment.getAnnotationName().getValue().equals("Param")) {
                            String paramName = annotationValue.substring(0, annotationValue.indexOf(UtilSymbolKeys.PKG_DELIMITER_KEYWORD));
                            String annotationDesc = annotationValue.substring(annotationValue.indexOf(UtilSymbolKeys.PKG_DELIMITER_KEYWORD) + 1).trim();
                            paramDescMap.put(paramName, annotationDesc);
                        } else if (annotationAttachment.getAnnotationName().getValue().equals("Description")) {
                            signatureInfoModel.setSignatureDescription(annotationValue);
                        }
                    }
                }
            }
        });
    }
    bInvokableSymbol.getParameters().forEach(bVarSymbol -> {
        ParameterInfoModel parameterInfoModel = new ParameterInfoModel();
        parameterInfoModel.setParamType(bVarSymbol.getType().toString());
        parameterInfoModel.setParamValue(bVarSymbol.getName().getValue());
        parameterInfoModel.setDescription(paramDescMap.get(bVarSymbol.getName().getValue()));
        paramModels.add(parameterInfoModel);
    });
    signatureInfoModel.setParameterInfoModels(paramModels);
    return signatureInfoModel;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) HashMap(java.util.HashMap) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 9 with BLangRecordLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral in project ballerina by ballerina-lang.

the class TestAnnotationProcessor method process.

@Override
public void process(FunctionNode functionNode, List<AnnotationAttachmentNode> annotations) {
    // to avoid processing those, we have to have below check.
    if (!suite.getSuiteName().equals(functionNode.getPosition().getSource().getPackageName())) {
        return;
    }
    // traverse through the annotations of this function
    for (AnnotationAttachmentNode attachmentNode : annotations) {
        String annotationName = attachmentNode.getAnnotationName().getValue();
        String functionName = functionNode.getName().getValue();
        if (BEFORE_SUITE_ANNOTATION_NAME.equals(annotationName)) {
            suite.addBeforeSuiteFunction(functionName);
        } else if (AFTER_SUITE_ANNOTATION_NAME.equals(annotationName)) {
            suite.addAfterSuiteFunction(functionName);
        } else if (BEFORE_EACH_ANNOTATION_NAME.equals(annotationName)) {
            suite.addBeforeEachFunction(functionName);
        } else if (AFTER_EACH_ANNOTATION_NAME.equals(annotationName)) {
            suite.addAfterEachFunction(functionName);
        } else if (MOCK_ANNOTATION_NAME.equals(annotationName)) {
            String[] vals = new String[2];
            // If package property not present the package is .
            // TODO: when default values are supported in annotation struct we can remove this
            vals[0] = ".";
            if (attachmentNode.getExpression() instanceof BLangRecordLiteral) {
                List<BLangRecordLiteral.BLangRecordKeyValue> attributes = ((BLangRecordLiteral) attachmentNode.getExpression()).getKeyValuePairs();
                attributes.forEach(attributeNode -> {
                    String name = attributeNode.getKey().toString();
                    String value = attributeNode.getValue().toString();
                    if (PACKAGE.equals(name)) {
                        vals[0] = value;
                    } else if (FUNCTION.equals(name)) {
                        vals[1] = value;
                    }
                });
                suite.addMockFunction(vals[0] + MOCK_ANNOTATION_DELIMITER + vals[1], functionName);
            }
        } else if (TEST_ANNOTATION_NAME.equals(annotationName)) {
            Test test = new Test();
            test.setTestName(functionName);
            AtomicBoolean shouldSkip = new AtomicBoolean();
            AtomicBoolean groupsFound = new AtomicBoolean();
            List<String> groups = registry.getGroups();
            boolean shouldIncludeGroups = registry.shouldIncludeGroups();
            if (attachmentNode.getExpression() instanceof BLangRecordLiteral) {
                List<BLangRecordLiteral.BLangRecordKeyValue> attributes = ((BLangRecordLiteral) attachmentNode.getExpression()).getKeyValuePairs();
                attributes.forEach(attributeNode -> {
                    String name = attributeNode.getKey().toString();
                    // Check if enable property is present in the annotation
                    if (TEST_ENABLE_ANNOTATION_NAME.equals(name) && "false".equals(attributeNode.getValue().toString())) {
                        // If enable is false, disable the test, no further processing is needed
                        shouldSkip.set(true);
                        return;
                    }
                    // Check whether user has provided a group list
                    if (groups != null && !groups.isEmpty()) {
                        // check if groups attribute is present in the annotation
                        if (GROUP_ANNOTATION_NAME.equals(name)) {
                            if (attributeNode.getValue() instanceof BLangArrayLiteral) {
                                BLangArrayLiteral values = (BLangArrayLiteral) attributeNode.getValue();
                                boolean isGroupPresent = isGroupAvailable(groups, values.exprs.stream().map(node -> node.toString()).collect(Collectors.toList()));
                                if (shouldIncludeGroups) {
                                    // include only if the test belong to one of these groups
                                    if (!isGroupPresent) {
                                        // skip the test if this group is not defined in this test
                                        shouldSkip.set(true);
                                        return;
                                    }
                                } else {
                                    // exclude only if the test belong to one of these groups
                                    if (isGroupPresent) {
                                        // skip if this test belongs to one of the excluded groups
                                        shouldSkip.set(true);
                                        return;
                                    }
                                }
                                groupsFound.set(true);
                            }
                        }
                    }
                    if (VALUE_SET_ANNOTATION_NAME.equals(name)) {
                        test.setDataProvider(attributeNode.getValue().toString());
                    }
                    if (BEFORE_FUNCTION.equals(name)) {
                        test.setBeforeTestFunction(attributeNode.getValue().toString());
                    }
                    if (AFTER_FUNCTION.equals(name)) {
                        test.setAfterTestFunction(attributeNode.getValue().toString());
                    }
                    if (DEPENDS_ON_FUNCTIONS.equals(name)) {
                        if (attributeNode.getValue() instanceof BLangArrayLiteral) {
                            BLangArrayLiteral values = (BLangArrayLiteral) attributeNode.getValue();
                            values.exprs.stream().map(node -> node.toString()).forEach(test::addDependsOnTestFunction);
                        }
                    }
                });
            }
            if (groups != null && !groups.isEmpty() && !groupsFound.get() && shouldIncludeGroups) {
                // if the user has asked to run only a specific list of groups and this test doesn't have
                // that group, we should skip the test
                shouldSkip.set(true);
            }
            if (!shouldSkip.get()) {
                suite.addTests(test);
            }
        } else {
        // disregard this annotation
        }
    }
}
Also used : Arrays(java.util.Arrays) PackageNode(org.ballerinalang.model.tree.PackageNode) BType(org.ballerinalang.model.types.BType) ProgramFile(org.ballerinalang.util.codegen.ProgramFile) TestSuite(org.ballerinalang.testerina.core.entity.TestSuite) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) DiagnosticLog(org.ballerinalang.util.diagnostic.DiagnosticLog) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) ArrayList(java.util.ArrayList) SupportedAnnotationPackages(org.ballerinalang.compiler.plugins.SupportedAnnotationPackages) AbstractCompilerPlugin(org.ballerinalang.compiler.plugins.AbstractCompilerPlugin) Vector(java.util.Vector) Map(java.util.Map) TesterinaFunction(org.ballerinalang.testerina.core.entity.TesterinaFunction) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) LinkedList(java.util.LinkedList) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) FunctionNode(org.ballerinalang.model.tree.FunctionNode) TypeTags(org.ballerinalang.model.types.TypeTags) Collectors(java.util.stream.Collectors) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) BArrayType(org.ballerinalang.model.types.BArrayType) List(java.util.List) Instruction(org.ballerinalang.util.codegen.Instruction) Test(org.ballerinalang.testerina.core.entity.Test) Queue(java.util.Queue) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.ballerinalang.testerina.core.entity.Test) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 10 with BLangRecordLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral in project ballerina by ballerina-lang.

the class Desugar method visit.

@Override
public void visit(BLangAssignment assignNode) {
    if (assignNode.expr.type.tag == TypeTags.STREAM && assignNode.varRefs.get(0) instanceof BLangSimpleVarRef) {
        ((BLangRecordLiteral) assignNode.expr).name = ((BLangSimpleVarRef) assignNode.varRefs.get(0)).variableName;
    }
    assignNode.varRefs = rewriteExprs(assignNode.varRefs);
    assignNode.expr = rewriteExpr(assignNode.expr);
    result = assignNode;
    if (!assignNode.safeAssignment) {
        return;
    }
    // Desugar the =? operator with the match statement
    // 
    // e.g.
    // File f;
    // .....
    // f =? openFile("/tmp/foo.txt"); // openFile: () -> (File | error)
    // 
    // {
    // match openFile("/tmp/foo.txt") {
    // File _$_f1 => f = _$_f1;
    // error e => throw e | return e
    // }
    // }
    BLangBlockStmt safeAssignmentBlock = ASTBuilderUtil.createBlockStmt(assignNode.pos, new ArrayList<>());
    BLangExpression lhsExpr = assignNode.varRefs.get(0);
    BLangMatchStmtPatternClause patternSuccessCase;
    if (assignNode.declaredWithVar) {
        BVarSymbol varSymbol = ((BLangSimpleVarRef) lhsExpr).symbol;
        BLangVariable variable = ASTBuilderUtil.createVariable(assignNode.pos, "", lhsExpr.type, null, varSymbol);
        BLangVariableDef variableDef = ASTBuilderUtil.createVariableDef(assignNode.pos, variable);
        safeAssignmentBlock.stmts.add(variableDef);
        patternSuccessCase = getSafeAssignSuccessPattern(assignNode.pos, lhsExpr.type, true, varSymbol, null);
    } else {
        patternSuccessCase = getSafeAssignSuccessPattern(assignNode.pos, lhsExpr.type, false, null, lhsExpr);
    }
    // Create the pattern to match the success case
    BLangMatchStmtPatternClause patternErrorCase = getSafeAssignErrorPattern(assignNode.pos, this.env.enclInvokable.symbol);
    // Create the match statement
    BLangMatch matchStmt = ASTBuilderUtil.createMatchStatement(assignNode.pos, assignNode.expr, new ArrayList<BLangMatchStmtPatternClause>() {

        {
            add(patternSuccessCase);
            add(patternErrorCase);
        }
    });
    // var f =? foo() -> var f;
    assignNode.expr = null;
    assignNode.safeAssignment = false;
    safeAssignmentBlock.stmts.add(matchStmt);
    result = rewrite(safeAssignmentBlock, this.env);
}
Also used : BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) BLangMatchStmtPatternClause(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Aggregations

BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)25 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)10 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)8 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)5 BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)5 HashSet (java.util.HashSet)4 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)4 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 SecretModel (org.ballerinax.kubernetes.models.SecretModel)2 ServiceModel (org.ballerinax.kubernetes.models.ServiceModel)2 BLangAnnotationAttachment (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment)2 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)2 BLangIndexBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)2 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)2 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)2 Collections (java.util.Collections)1