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;
}
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);
}
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;
}
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;
}
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;
}
Aggregations