use of org.ballerinalang.model.tree.VariableNode in project ballerina by ballerina-lang.
the class SwaggerResourceMapper method addResourceParameters.
/**
* Creates parameters in the swagger operation using the parameters in the ballerina resource definition.
* @param resource The ballerina resource definition.
* @param operationAdaptor The swagger operation.
*/
private void addResourceParameters(ResourceNode resource, OperationAdaptor operationAdaptor) {
if (!"get".equalsIgnoreCase(operationAdaptor.getHttpOperation())) {
// Creating request body - required.
ModelImpl messageModel = new ModelImpl();
messageModel.setType("object");
Map<String, Model> definitions = new HashMap<>();
if (!definitions.containsKey("Request")) {
definitions.put("Request", messageModel);
this.swaggerDefinition.setDefinitions(definitions);
}
// Creating "Request rq" parameter
BodyParameter messageParameter = new BodyParameter();
messageParameter.setName(resource.getParameters().get(0).getName().getValue());
RefModel refModel = new RefModel();
refModel.setReference("Request");
messageParameter.setSchema(refModel);
operationAdaptor.getOperation().addParameter(messageParameter);
}
for (int i = 2; i < resource.getParameters().size(); i++) {
VariableNode parameterDef = resource.getParameters().get(i);
String typeName = parameterDef.getTypeNode().toString().toLowerCase(Locale.getDefault());
PathParameter pathParameter = new PathParameter();
// Set in value
pathParameter.setIn("path");
// Set parameter name
String parameterName = parameterDef.getName().getValue();
pathParameter.setName(parameterName);
// Set type
if (typeName.contains("[]")) {
pathParameter.setType("array");
switch(typeName.replace("[]", "").trim()) {
case "string":
pathParameter.items(new StringProperty());
break;
case "int":
pathParameter.items(new IntegerProperty());
break;
case "boolean":
pathParameter.items(new BooleanProperty());
break;
default:
break;
}
} else if ("int".equals(typeName)) {
pathParameter.setType("integer");
} else {
pathParameter.setType(typeName);
}
// Note: 'format' to be added using annotations, hence skipped here.
operationAdaptor.getOperation().addParameter(pathParameter);
}
}
use of org.ballerinalang.model.tree.VariableNode in project ballerina by ballerina-lang.
the class SiddhiQueryBuilder method visit.
public void visit(BLangForever foreverStatement) {
siddhiQuery = new StringBuilder();
streamDefinitionQuery = new StringBuilder();
streamIds = new HashSet<>();
inStreamRefs = new ArrayList<>();
outStreamRefs = new ArrayList<>();
inTableRefs = new ArrayList<>();
outTableRefs = new ArrayList<>();
binaryExpr = null;
setExpr = null;
orderByClause = null;
whereClause = null;
windowClause = null;
joinStreamingInputClause = null;
streamingInputClause = null;
selectExprClause = null;
selectExpr = null;
setAssignmentClause = null;
groupByClause = null;
havingClause = null;
patternStreamingClause = null;
streamActionClause = null;
intRangeExpr = null;
List<VariableNode> globalVariables = foreverStatement.getGlobalVariables();
if (globalVariables != null) {
for (VariableNode variable : globalVariables) {
((BLangVariable) variable).accept(this);
}
}
List<VariableSymbol> functionVariables = foreverStatement.getFunctionVariables();
if (functionVariables != null) {
for (VariableSymbol variable : functionVariables) {
getStreamDefintionForFuntionVariable((BVarSymbol) variable);
}
}
List<? extends StatementNode> statementNodes = foreverStatement.gettreamingQueryStatements();
for (StatementNode statementNode : statementNodes) {
((BLangStatement) statementNode).accept(this);
}
foreverStatement.setSiddhiQuery(this.getSiddhiQuery());
foreverStatement.setStreamIdsAsString(String.join(",", streamIds));
}
use of org.ballerinalang.model.tree.VariableNode in project ballerina by ballerina-lang.
the class BLangPackageBuilder method addDefaultableParam.
public void addDefaultableParam(DiagnosticPos pos, Set<Whitespace> ws) {
BLangVariableDef defaultableParam = (BLangVariableDef) TreeBuilder.createVariableDefinitionNode();
defaultableParam.pos = pos;
defaultableParam.addWS(ws);
List<VariableNode> params = this.varListStack.peek();
BLangVariable var = (BLangVariable) params.remove(params.size() - 1);
var.expr = (BLangExpression) this.exprNodeStack.pop();
defaultableParam.var = var;
this.defaultableParamsList.add(defaultableParam);
}
use of org.ballerinalang.model.tree.VariableNode in project ballerina by ballerina-lang.
the class BLangPackageBuilder method endTransformerDef.
public void endTransformerDef(DiagnosticPos pos, Set<Whitespace> ws, boolean publicFunc, String name, boolean paramsAvailable) {
BLangTransformer transformer = (BLangTransformer) this.invokableNodeStack.pop();
transformer.pos = pos;
transformer.addWS(ws);
transformer.setName(this.createIdentifier(name));
if (paramsAvailable) {
this.varListStack.pop().forEach(variableNode -> {
((BLangVariable) variableNode).docTag = DocTag.PARAM;
transformer.addParameter(variableNode);
});
}
// get the source and the target params
List<VariableNode> mappingParams = this.varListStack.pop();
// set the first mapping-param as the source for transformer
VariableNode source = mappingParams.remove(0);
((BLangVariable) source).docTag = DocTag.RECEIVER;
transformer.setSource(source);
mappingParams.forEach(variableNode -> {
((BLangVariable) variableNode).docTag = DocTag.RECEIVER;
transformer.addReturnParameter(variableNode);
});
if (publicFunc) {
transformer.flagSet.add(Flag.PUBLIC);
}
this.compUnit.addTopLevelNode(transformer);
}
use of org.ballerinalang.model.tree.VariableNode in project ballerina by ballerina-lang.
the class ResourceContextHolder method buildContext.
public static ResourceContextHolder buildContext(ResourceNode resource) throws CodeGeneratorException {
ResourceContextHolder context = new ResourceContextHolder();
context.name = resource.getName().getValue();
context.parameters = new ArrayList<>();
for (VariableNode node : resource.getParameters()) {
ParameterContextHolder parameter = ParameterContextHolder.buildContext(node);
if (parameter != null) {
context.parameters.add(parameter);
}
}
// Iterate through all resource level annotations and find out resource configuration information
AnnotationAttachmentNode ann = GeneratorUtils.getAnnotationFromList(GeneratorConstants.RES_CONFIG_ANNOTATION, GeneratorConstants.HTTP_PKG_ALIAS, resource.getAnnotationAttachments());
if (ann == null) {
throw new CodeGeneratorException("Incomplete resource configuration found");
}
BLangRecordLiteral bLiteral = ((BLangRecordLiteral) ((BLangAnnotationAttachment) ann).getExpression());
List<BLangRecordLiteral.BLangRecordKeyValue> list = bLiteral.getKeyValuePairs();
Map<String, String[]> attrs = GeneratorUtils.getKeyValuePairAsMap(list);
// We don't expect multiple http methods to be supported by single action
// We only consider first content type for a single resource
context.method = attrs.get(GeneratorConstants.ATTR_METHODS) != null ? attrs.get(GeneratorConstants.ATTR_METHODS)[0] : null;
context.contentType = attrs.get(GeneratorConstants.ATTR_CONSUMES) != null ? attrs.get(GeneratorConstants.ATTR_CONSUMES)[0] : null;
String path = attrs.get(GeneratorConstants.ATTR_PATH) != null ? attrs.get(GeneratorConstants.ATTR_PATH)[0] : null;
context.path = context.getTemplatePath(path);
return context;
}
Aggregations