Search in sources :

Example 1 with BLangRecordLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method processPersistentVolumeClaim.

/**
 * Process PersistentVolumeClaim annotations.
 *
 * @param attachmentNode Attachment Node
 * @return Set of @{@link ConfigMapModel} objects
 */
Set<PersistentVolumeClaimModel> processPersistentVolumeClaim(AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    Set<PersistentVolumeClaimModel> volumeClaimModels = new HashSet<>();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        List<BLangExpression> secretAnnotation = ((BLangArrayLiteral) keyValue.valueExpr).exprs;
        for (BLangExpression bLangExpression : secretAnnotation) {
            PersistentVolumeClaimModel claimModel = new PersistentVolumeClaimModel();
            List<BLangRecordLiteral.BLangRecordKeyValue> annotationValues = ((BLangRecordLiteral) bLangExpression).getKeyValuePairs();
            for (BLangRecordLiteral.BLangRecordKeyValue annotation : annotationValues) {
                VolumeClaimConfig volumeMountConfig = VolumeClaimConfig.valueOf(annotation.getKey().toString());
                String annotationValue = resolveValue(annotation.getValue().toString());
                switch(volumeMountConfig) {
                    case name:
                        claimModel.setName(getValidName(annotationValue));
                        break;
                    case mountPath:
                        claimModel.setMountPath(annotationValue);
                        break;
                    case accessMode:
                        claimModel.setAccessMode(annotationValue);
                        break;
                    case volumeClaimSize:
                        claimModel.setVolumeClaimSize(annotationValue);
                        break;
                    case readOnly:
                        claimModel.setReadOnly(Boolean.parseBoolean(annotationValue));
                        break;
                    default:
                        break;
                }
            }
            volumeClaimModels.add(claimModel);
        }
    }
    return volumeClaimModels;
}
Also used : PersistentVolumeClaimModel(org.ballerinax.kubernetes.models.PersistentVolumeClaimModel) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) HashSet(java.util.HashSet)

Example 2 with BLangRecordLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method processConfigMap.

/**
 * Process ConfigMap annotations.
 *
 * @param attachmentNode Attachment Node
 * @return Set of @{@link ConfigMapModel} objects
 */
Set<ConfigMapModel> processConfigMap(AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    Set<ConfigMapModel> configMapModels = new HashSet<>();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        List<BLangExpression> configAnnotation = ((BLangArrayLiteral) keyValue.valueExpr).exprs;
        for (BLangExpression bLangExpression : configAnnotation) {
            ConfigMapModel configMapModel = new ConfigMapModel();
            List<BLangRecordLiteral.BLangRecordKeyValue> annotationValues = ((BLangRecordLiteral) bLangExpression).getKeyValuePairs();
            for (BLangRecordLiteral.BLangRecordKeyValue annotation : annotationValues) {
                VolumeMountConfig volumeMountConfig = VolumeMountConfig.valueOf(annotation.getKey().toString());
                String annotationValue = resolveValue(annotation.getValue().toString());
                switch(volumeMountConfig) {
                    case name:
                        configMapModel.setName(getValidName(annotationValue));
                        break;
                    case mountPath:
                        configMapModel.setMountPath(annotationValue);
                        break;
                    case isBallerinaConf:
                        configMapModel.setBallerinaConf(Boolean.parseBoolean(annotationValue));
                        break;
                    case data:
                        List<BLangExpression> data = ((BLangArrayLiteral) annotation.valueExpr).exprs;
                        configMapModel.setData(getDataForConfigMap(data));
                        break;
                    case readOnly:
                        configMapModel.setReadOnly(Boolean.parseBoolean(annotationValue));
                        break;
                    default:
                        break;
                }
            }
            configMapModels.add(configMapModel);
        }
    }
    return configMapModels;
}
Also used : ConfigMapModel(org.ballerinax.kubernetes.models.ConfigMapModel) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) HashSet(java.util.HashSet)

Example 3 with BLangRecordLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method processPodAutoscalerAnnotation.

/**
 * Process annotations and create service model object.
 *
 * @param attachmentNode annotation attachment node.
 * @return Service model object
 */
PodAutoscalerModel processPodAutoscalerAnnotation(AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    PodAutoscalerModel podAutoscalerModel = new PodAutoscalerModel();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        PodAutoscalerConfiguration podAutoscalerConfiguration = PodAutoscalerConfiguration.valueOf(keyValue.getKey().toString());
        String annotationValue = resolveValue(keyValue.getValue().toString());
        switch(podAutoscalerConfiguration) {
            case name:
                podAutoscalerModel.setName(getValidName(annotationValue));
                break;
            case labels:
                podAutoscalerModel.setLabels(getMap(((BLangRecordLiteral) keyValue.valueExpr).keyValuePairs));
                break;
            case cpuPercentage:
                podAutoscalerModel.setCpuPercentage(Integer.parseInt(annotationValue));
                break;
            case minReplicas:
                podAutoscalerModel.setMinReplicas(Integer.parseInt(annotationValue));
                break;
            case maxReplicas:
                podAutoscalerModel.setMaxReplicas(Integer.parseInt(annotationValue));
                break;
            default:
                break;
        }
    }
    return podAutoscalerModel;
}
Also used : BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) PodAutoscalerModel(org.ballerinax.kubernetes.models.PodAutoscalerModel)

Example 4 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 5 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)

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