Search in sources :

Example 1 with VariableNode

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);
    }
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) RefModel(io.swagger.models.RefModel) HashMap(java.util.HashMap) BooleanProperty(io.swagger.models.properties.BooleanProperty) StringProperty(io.swagger.models.properties.StringProperty) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) VariableNode(org.ballerinalang.model.tree.VariableNode) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ModelImpl(io.swagger.models.ModelImpl)

Example 2 with VariableNode

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));
}
Also used : VariableNode(org.ballerinalang.model.tree.VariableNode) BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) StatementNode(org.ballerinalang.model.tree.statements.StatementNode) VariableSymbol(org.ballerinalang.model.symbols.VariableSymbol) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 3 with VariableNode

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);
}
Also used : VariableNode(org.ballerinalang.model.tree.VariableNode) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 4 with VariableNode

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);
}
Also used : BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) VariableNode(org.ballerinalang.model.tree.VariableNode)

Example 5 with VariableNode

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;
}
Also used : VariableNode(org.ballerinalang.model.tree.VariableNode) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) CodeGeneratorException(org.ballerinalang.code.generator.exception.CodeGeneratorException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Aggregations

VariableNode (org.ballerinalang.model.tree.VariableNode)5 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)2 Model (io.swagger.models.Model)1 ModelImpl (io.swagger.models.ModelImpl)1 RefModel (io.swagger.models.RefModel)1 BodyParameter (io.swagger.models.parameters.BodyParameter)1 PathParameter (io.swagger.models.parameters.PathParameter)1 BooleanProperty (io.swagger.models.properties.BooleanProperty)1 IntegerProperty (io.swagger.models.properties.IntegerProperty)1 StringProperty (io.swagger.models.properties.StringProperty)1 HashMap (java.util.HashMap)1 CodeGeneratorException (org.ballerinalang.code.generator.exception.CodeGeneratorException)1 VariableSymbol (org.ballerinalang.model.symbols.VariableSymbol)1 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)1 StatementNode (org.ballerinalang.model.tree.statements.StatementNode)1 BLangAnnotationAttachment (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment)1 BLangTransformer (org.wso2.ballerinalang.compiler.tree.BLangTransformer)1 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)1 BLangStatement (org.wso2.ballerinalang.compiler.tree.statements.BLangStatement)1 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)1