Search in sources :

Example 16 with BLangRecordLiteral

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

the class KubernetesAnnotationProcessor method processServiceAnnotation.

/**
 * Process annotations and create service model object.
 *
 * @param endpointName   ballerina service name
 * @param attachmentNode annotation attachment node.
 * @return Service model object
 */
ServiceModel processServiceAnnotation(String endpointName, AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    ServiceModel serviceModel = new ServiceModel();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        ServiceConfiguration serviceConfiguration = ServiceConfiguration.valueOf(keyValue.getKey().toString());
        String annotationValue = resolveValue(keyValue.getValue().toString());
        switch(serviceConfiguration) {
            case name:
                serviceModel.setName(getValidName(annotationValue));
                break;
            case labels:
                serviceModel.setLabels(getMap(((BLangRecordLiteral) keyValue.valueExpr).keyValuePairs));
                break;
            case serviceType:
                serviceModel.setServiceType(annotationValue);
                break;
            case port:
                serviceModel.setPort(Integer.parseInt(annotationValue));
                break;
            default:
                break;
        }
    }
    if (serviceModel.getName() == null) {
        serviceModel.setName(getValidName(endpointName) + SVC_POSTFIX);
    }
    return serviceModel;
}
Also used : ServiceModel(org.ballerinax.kubernetes.models.ServiceModel) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)

Example 17 with BLangRecordLiteral

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

the class TypeChecker method visit.

public void visit(BLangRecordLiteral recordLiteral) {
    BType actualType = symTable.errType;
    int expTypeTag = expTypes.get(0).tag;
    if (expTypeTag == TypeTags.NONE || expTypeTag == TypeTags.ANY) {
        // var a = {}
        // Change the expected type to map
        expTypes = Lists.of(symTable.mapType);
    }
    List<BType> matchedTypeList = getRecordCompatibleType(expTypes.get(0));
    if (matchedTypeList.isEmpty()) {
        dlog.error(recordLiteral.pos, DiagnosticCode.INVALID_LITERAL_FOR_TYPE, expTypes.get(0));
    } else if (matchedTypeList.size() > 1) {
        dlog.error(recordLiteral.pos, DiagnosticCode.AMBIGUOUS_TYPES, expTypes.get(0));
    } else {
        recordLiteral.keyValuePairs.forEach(keyValuePair -> checkRecLiteralKeyValue(keyValuePair, matchedTypeList.get(0)));
        actualType = matchedTypeList.get(0);
        // TODO Following check can be moved the code analyzer.
        if (expTypeTag == TypeTags.STRUCT) {
            validateStructInitalizer(recordLiteral.pos);
        }
    }
    resultTypes = types.checkTypes(recordLiteral, Lists.of(actualType), expTypes);
}
Also used : Arrays(java.util.Arrays) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) BLangXMLTextLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral) Map(java.util.Map) BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference) BEnumType(org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr) Set(java.util.Set) SelectExpressionNode(org.ballerinalang.model.tree.clauses.SelectExpressionNode) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangLambdaFunction(org.wso2.ballerinalang.compiler.tree.expressions.BLangLambdaFunction) BLangSelectExpression(org.wso2.ballerinalang.compiler.tree.clauses.BLangSelectExpression) NamedArgNode(org.ballerinalang.model.tree.expressions.NamedArgNode) BLangXMLAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute) BLangNamedArgsExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangNamedArgsExpression) BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangXMLCommentLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLCommentLiteral) ArrayList(java.util.ArrayList) Flags(org.wso2.ballerinalang.util.Flags) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) TreeBuilder(org.ballerinalang.model.TreeBuilder) ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangXMLProcInsLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLProcInsLiteral) BLangForever(org.wso2.ballerinalang.compiler.tree.statements.BLangForever) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BLangOrderBy(org.wso2.ballerinalang.compiler.tree.clauses.BLangOrderBy) BLangSelectClause(org.wso2.ballerinalang.compiler.tree.clauses.BLangSelectClause) SymTag(org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangTableQuery(org.wso2.ballerinalang.compiler.tree.clauses.BLangTableQuery) BEndpointVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol) BLangBracedOrTupleExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr) BLangXMLAttributeAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttributeAccess) Lists(org.wso2.ballerinalang.util.Lists) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) OperatorKind(org.ballerinalang.model.tree.OperatorKind) BLangHaving(org.wso2.ballerinalang.compiler.tree.clauses.BLangHaving) BOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol) Names(org.wso2.ballerinalang.compiler.util.Names) BLangDiagnosticLog(org.wso2.ballerinalang.compiler.util.diagnotic.BLangDiagnosticLog) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) MultiReturnExpr(org.wso2.ballerinalang.compiler.tree.expressions.MultiReturnExpr) BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangRecordKey(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKey) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) Collectors(java.util.stream.Collectors) BLangFieldBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess) List(java.util.List) NodeKind(org.ballerinalang.model.tree.NodeKind) BLangTableQueryExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangTableQueryExpression) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) BLangStreamingInput(org.wso2.ballerinalang.compiler.tree.clauses.BLangStreamingInput) BLangNodeVisitor(org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor) BLangTypeofExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeofExpr) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) HashSet(java.util.HashSet) IterableKind(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind) BLangStringTemplateLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangStringTemplateLiteral) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BLangTernaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr) BFutureType(org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType) BLangIntRangeExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangIntRangeExpression) BLangRecordKeyValue(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue) XMLConstants(javax.xml.XMLConstants) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BTransformerSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTransformerSymbol) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) RecordKind(org.wso2.ballerinalang.compiler.semantics.analyzer.Types.RecordKind) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangGroupBy(org.wso2.ballerinalang.compiler.tree.clauses.BLangGroupBy) Type(org.ballerinalang.model.types.Type) Name(org.wso2.ballerinalang.compiler.util.Name) BLangJoinStreamingInput(org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinStreamingInput) BLangAwaitExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangAwaitExpr) BLangRestArgsExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangRestArgsExpression) BLangXMLElementLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral) FieldType(org.wso2.ballerinalang.compiler.util.FieldType) DiagnosticCode(org.ballerinalang.util.diagnostic.DiagnosticCode) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) Collections(java.util.Collections) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 18 with BLangRecordLiteral

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

the class ServiceProtoUtils method isServerStreaming.

private static boolean isServerStreaming(ResourceNode resourceNode) {
    boolean serverStreaming = false;
    for (AnnotationAttachmentNode annotationNode : resourceNode.getAnnotationAttachments()) {
        if (!ANN_RESOURCE_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;
                if (ANN_ATTR_RESOURCE_SERVER_STREAM.equals(attributeName)) {
                    serverStreaming = attributeValue != null && Boolean.parseBoolean(attributeValue);
                }
            }
        }
    }
    return serverStreaming;
}
Also used : BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 19 with BLangRecordLiteral

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

the class ASTBuilderUtil method createEmptyRecordLiteral.

static BLangRecordLiteral createEmptyRecordLiteral(DiagnosticPos pos, BType type) {
    final BLangRecordLiteral recordLiteralNode = (BLangRecordLiteral) TreeBuilder.createRecordLiteralNode();
    recordLiteralNode.pos = pos;
    recordLiteralNode.type = type;
    return recordLiteralNode;
}
Also used : BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)

Example 20 with BLangRecordLiteral

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

the class AnnotationDesugar method createAnnotationMapEntryVar.

private BLangVariable createAnnotationMapEntryVar(String key, BLangVariable annotationMapVar, BLangBlockStmt target, BSymbol parentSymbol) {
    // create: map key = {};
    final BLangRecordLiteral recordLiteralNode = ASTBuilderUtil.createEmptyRecordLiteral(target.pos, symTable.mapType);
    BLangVariable entryVariable = ASTBuilderUtil.createVariable(target.pos, key, recordLiteralNode.type);
    entryVariable.expr = recordLiteralNode;
    ASTBuilderUtil.defineVariable(entryVariable, parentSymbol, names);
    ASTBuilderUtil.createVariableDefStmt(target.pos, target).var = entryVariable;
    // create: annotationMapVar["key"] = key;
    BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(target.pos, target);
    assignmentStmt.expr = ASTBuilderUtil.createVariableRef(target.pos, entryVariable.symbol);
    BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
    indexAccessNode.pos = target.pos;
    indexAccessNode.indexExpr = ASTBuilderUtil.createLiteral(target.pos, symTable.stringType, key);
    indexAccessNode.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationMapVar.symbol);
    indexAccessNode.type = recordLiteralNode.type;
    assignmentStmt.varRefs.add(indexAccessNode);
    return entryVariable;
}
Also used : BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) 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