Search in sources :

Example 1 with AttributeInfo

use of org.ballerinalang.util.codegen.attributes.AttributeInfo 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(ResourceInfo resource, OperationAdaptor operationAdaptor) {
    if (!"get".equalsIgnoreCase(operationAdaptor.getHttpOperation())) {
        // Creating message body - required.
        ModelImpl messageModel = new ModelImpl();
        messageModel.setType("object");
        Map<String, Model> definitions = new HashMap<>();
        if (!definitions.containsKey("Message")) {
            definitions.put("Message", messageModel);
            this.swaggerDefinition.setDefinitions(definitions);
        }
        // Creating "Message m" parameter
        BodyParameter messageParameter = new BodyParameter();
        messageParameter.setName(resource.getParamNames()[0]);
        RefModel refModel = new RefModel();
        refModel.setReference("Message");
        messageParameter.setSchema(refModel);
        operationAdaptor.getOperation().addParameter(messageParameter);
    }
    // Creating query params and path params
    AttributeInfo attributeInfo = resource.getAttributeInfo(AttributeInfo.Kind.PARAMETER_ANNOTATIONS_ATTRIBUTE);
    if (attributeInfo instanceof ParamAnnotationAttributeInfo) {
        ParamAnnotationAttributeInfo paramAttributeInfo = (ParamAnnotationAttributeInfo) resource.getAttributeInfo(AttributeInfo.Kind.PARAMETER_ANNOTATIONS_ATTRIBUTE);
        ParamAnnAttachmentInfo[] attachmentInfoArray = paramAttributeInfo.getAttachmentInfoArray();
        for (ParamAnnAttachmentInfo paramAnnAttachmentInfo : attachmentInfoArray) {
            if (paramAnnAttachmentInfo.getAnnAttachmentInfos().length > 0) {
                AnnAttachmentInfo annAttachmentInfo = paramAnnAttachmentInfo.getAnnAttachmentInfos()[0];
                Map<String, AnnAttributeValue> paramAnnAttributeValueMap = SwaggerUtils.convertToAttributeMap(annAttachmentInfo);
                if (paramAnnAttributeValueMap.size() == 1 && null != paramAnnAttributeValueMap.get("value")) {
                    // Add query parameter
                    if (annAttachmentInfo.getName().equalsIgnoreCase("QueryParam")) {
                        QueryParameter queryParameter = new QueryParameter();
                        // Set in value.
                        queryParameter.setIn("query");
                        // Set parameter name
                        String parameterName = paramAnnAttributeValueMap.get("value").getStringValue();
                        if ((parameterName == null) || parameterName.isEmpty()) {
                            parameterName = resource.getParamNames()[paramAnnAttachmentInfo.getParamIdex()];
                        }
                        queryParameter.setName(parameterName);
                        // Note: 'description' to be added using annotations, hence skipped here.
                        // Setting false to required(as per swagger spec). This can be overridden while parsing
                        // annotations.
                        queryParameter.required(false);
                        // Note: 'allowEmptyValue' to be added using annotations, hence skipped here.
                        // Set type
                        String paramType = resource.getParamTypes()[paramAnnAttachmentInfo.getParamIdex()].getName();
                        if ("int".equals(paramType)) {
                            queryParameter.setType("integer");
                        } else {
                            queryParameter.setType(paramType);
                        }
                        // Note: 'format' to be added using annotations, hence skipped here.
                        operationAdaptor.getOperation().addParameter(queryParameter);
                    }
                    if (annAttachmentInfo.getName().equalsIgnoreCase("PathParam")) {
                        PathParameter pathParameter = new PathParameter();
                        // Set in value
                        pathParameter.setIn("path");
                        // Set parameter name
                        String parameterName = paramAnnAttributeValueMap.get("value").getStringValue();
                        if ((parameterName == null) || parameterName.isEmpty()) {
                            parameterName = resource.getParamNames()[paramAnnAttachmentInfo.getParamIdex()];
                        }
                        pathParameter.setName(parameterName);
                        // Note: 'description' to be added using annotations, hence skipped here.
                        // Note: 'allowEmptyValue' to be added using annotations, hence skipped here.
                        // Set type
                        String paramType = resource.getParamTypes()[paramAnnAttachmentInfo.getParamIdex()].getName();
                        if ("int".equals(paramType)) {
                            pathParameter.setType("integer");
                        } else {
                            pathParameter.setType(paramType);
                        }
                        // Note: 'format' to be added using annotations, hence skipped here.
                        operationAdaptor.getOperation().addParameter(pathParameter);
                    }
                }
            }
        }
    }
}
Also used : ParamAnnAttachmentInfo(org.ballerinalang.util.codegen.ParamAnnAttachmentInfo) AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) ParamAnnotationAttributeInfo(org.ballerinalang.util.codegen.attributes.ParamAnnotationAttributeInfo) HashMap(java.util.HashMap) ParamAnnAttachmentInfo(org.ballerinalang.util.codegen.ParamAnnAttachmentInfo) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) ParamAnnotationAttributeInfo(org.ballerinalang.util.codegen.attributes.ParamAnnotationAttributeInfo) AttributeInfo(org.ballerinalang.util.codegen.attributes.AttributeInfo) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) AnnAttributeValue(org.ballerinalang.util.codegen.AnnAttributeValue) ModelImpl(io.swagger.models.ModelImpl)

Example 2 with AttributeInfo

use of org.ballerinalang.util.codegen.attributes.AttributeInfo in project ballerina by ballerina-lang.

the class ProgramFileReader method getAttributeInfo.

private AttributeInfo getAttributeInfo(DataInputStream dataInStream, ConstantPool constantPool) throws IOException {
    int attribNameCPIndex = dataInStream.readInt();
    UTF8CPEntry attribNameCPEntry = (UTF8CPEntry) constantPool.getCPEntry(attribNameCPIndex);
    AttributeInfo.Kind attribKind = AttributeInfo.Kind.fromString(attribNameCPEntry.getValue());
    if (attribKind == null) {
        throw new ProgramFileFormatException("unknown attribute kind " + attribNameCPEntry.getValue());
    }
    switch(attribKind) {
        case CODE_ATTRIBUTE:
            CodeAttributeInfo codeAttributeInfo = new CodeAttributeInfo();
            codeAttributeInfo.setAttributeNameIndex(attribNameCPIndex);
            codeAttributeInfo.setCodeAddrs(dataInStream.readInt());
            codeAttributeInfo.setMaxLongLocalVars(dataInStream.readUnsignedShort());
            codeAttributeInfo.setMaxDoubleLocalVars(dataInStream.readShort());
            codeAttributeInfo.setMaxStringLocalVars(dataInStream.readShort());
            codeAttributeInfo.setMaxIntLocalVars(dataInStream.readShort());
            codeAttributeInfo.setMaxByteLocalVars(dataInStream.readShort());
            codeAttributeInfo.setMaxRefLocalVars(dataInStream.readShort());
            codeAttributeInfo.setMaxLongRegs(dataInStream.readShort());
            codeAttributeInfo.setMaxDoubleRegs(dataInStream.readShort());
            codeAttributeInfo.setMaxStringRegs(dataInStream.readShort());
            codeAttributeInfo.setMaxIntRegs(dataInStream.readShort());
            codeAttributeInfo.setMaxByteRegs(dataInStream.readShort());
            codeAttributeInfo.setMaxRefRegs(dataInStream.readShort());
            return codeAttributeInfo;
        case VARIABLE_TYPE_COUNT_ATTRIBUTE:
            VarTypeCountAttributeInfo varCountAttributeInfo = new VarTypeCountAttributeInfo(attribNameCPIndex);
            varCountAttributeInfo.setMaxLongVars(dataInStream.readShort());
            varCountAttributeInfo.setMaxDoubleVars(dataInStream.readShort());
            varCountAttributeInfo.setMaxStringVars(dataInStream.readShort());
            varCountAttributeInfo.setMaxIntVars(dataInStream.readShort());
            varCountAttributeInfo.setMaxByteVars(dataInStream.readShort());
            varCountAttributeInfo.setMaxRefVars(dataInStream.readShort());
            return varCountAttributeInfo;
        case ERROR_TABLE:
            ErrorTableAttributeInfo tableAttributeInfo = new ErrorTableAttributeInfo(attribNameCPIndex);
            int tableEntryCount = dataInStream.readShort();
            for (int i = 0; i < tableEntryCount; i++) {
                int ipFrom = dataInStream.readInt();
                int ipTo = dataInStream.readInt();
                int ipTarget = dataInStream.readInt();
                int priority = dataInStream.readInt();
                int errorStructCPIndex = dataInStream.readInt();
                ErrorTableEntry tableEntry = new ErrorTableEntry(ipFrom, ipTo, ipTarget, priority, errorStructCPIndex);
                if (errorStructCPIndex != -1) {
                    StructureRefCPEntry structureRefCPEntry = (StructureRefCPEntry) constantPool.getCPEntry(errorStructCPIndex);
                    tableEntry.setError((StructInfo) structureRefCPEntry.getStructureTypeInfo());
                }
                tableAttributeInfo.addErrorTableEntry(tableEntry);
            }
            return tableAttributeInfo;
        case LOCAL_VARIABLES_ATTRIBUTE:
            LocalVariableAttributeInfo localVarAttrInfo = new LocalVariableAttributeInfo(attribNameCPIndex);
            int localVarInfoCount = dataInStream.readShort();
            for (int i = 0; i < localVarInfoCount; i++) {
                LocalVariableInfo localVariableInfo = getLocalVariableInfo(dataInStream, constantPool);
                localVarAttrInfo.addLocalVarInfo(localVariableInfo);
            }
            return localVarAttrInfo;
        case LINE_NUMBER_TABLE_ATTRIBUTE:
            LineNumberTableAttributeInfo lnNoTblAttrInfo = new LineNumberTableAttributeInfo(attribNameCPIndex);
            int lineNoInfoCount = dataInStream.readShort();
            for (int i = 0; i < lineNoInfoCount; i++) {
                LineNumberInfo lineNumberInfo = getLineNumberInfo(dataInStream, constantPool);
                lnNoTblAttrInfo.addLineNumberInfo(lineNumberInfo);
            }
            return lnNoTblAttrInfo;
        case DEFAULT_VALUE_ATTRIBUTE:
            DefaultValue defaultValue = getDefaultValue(dataInStream, constantPool);
            DefaultValueAttributeInfo defaultValAttrInfo = new DefaultValueAttributeInfo(attribNameCPIndex, defaultValue);
            return defaultValAttrInfo;
        case PARAMETER_DEFAULTS_ATTRIBUTE:
            ParamDefaultValueAttributeInfo paramDefaultValAttrInfo = new ParamDefaultValueAttributeInfo(attribNameCPIndex);
            int paramDefaultsInfoCount = dataInStream.readShort();
            for (int i = 0; i < paramDefaultsInfoCount; i++) {
                DefaultValue paramDefaultValue = getDefaultValue(dataInStream, constantPool);
                paramDefaultValAttrInfo.addParamDefaultValueInfo(paramDefaultValue);
            }
            return paramDefaultValAttrInfo;
        default:
            throw new ProgramFileFormatException("unsupported attribute kind " + attribNameCPEntry.getValue());
    }
}
Also used : VarTypeCountAttributeInfo(org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo) LineNumberTableAttributeInfo(org.ballerinalang.util.codegen.attributes.LineNumberTableAttributeInfo) ProgramFileFormatException(org.ballerinalang.util.exceptions.ProgramFileFormatException) UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) ParamDefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.ParamDefaultValueAttributeInfo) LocalVariableAttributeInfo(org.ballerinalang.util.codegen.attributes.LocalVariableAttributeInfo) ErrorTableAttributeInfo(org.ballerinalang.util.codegen.attributes.ErrorTableAttributeInfo) LineNumberTableAttributeInfo(org.ballerinalang.util.codegen.attributes.LineNumberTableAttributeInfo) CodeAttributeInfo(org.ballerinalang.util.codegen.attributes.CodeAttributeInfo) DefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.DefaultValueAttributeInfo) VarTypeCountAttributeInfo(org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo) AttributeInfo(org.ballerinalang.util.codegen.attributes.AttributeInfo) ParamDefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.ParamDefaultValueAttributeInfo) LocalVariableAttributeInfo(org.ballerinalang.util.codegen.attributes.LocalVariableAttributeInfo) ParamDefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.ParamDefaultValueAttributeInfo) DefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.DefaultValueAttributeInfo) ErrorTableAttributeInfo(org.ballerinalang.util.codegen.attributes.ErrorTableAttributeInfo) CodeAttributeInfo(org.ballerinalang.util.codegen.attributes.CodeAttributeInfo) StructureRefCPEntry(org.ballerinalang.util.codegen.cpentries.StructureRefCPEntry)

Example 3 with AttributeInfo

use of org.ballerinalang.util.codegen.attributes.AttributeInfo in project ballerina by ballerina-lang.

the class ProgramFileReader method readAttributeInfoEntries.

private void readAttributeInfoEntries(DataInputStream dataInStream, ConstantPool constantPool, AttributeInfoPool attributeInfoPool) throws IOException {
    int attributesCount = dataInStream.readShort();
    for (int k = 0; k < attributesCount; k++) {
        AttributeInfo attributeInfo = getAttributeInfo(dataInStream, constantPool);
        attributeInfoPool.addAttributeInfo(attributeInfo.getKind(), attributeInfo);
    }
}
Also used : ParamDefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.ParamDefaultValueAttributeInfo) LocalVariableAttributeInfo(org.ballerinalang.util.codegen.attributes.LocalVariableAttributeInfo) ErrorTableAttributeInfo(org.ballerinalang.util.codegen.attributes.ErrorTableAttributeInfo) LineNumberTableAttributeInfo(org.ballerinalang.util.codegen.attributes.LineNumberTableAttributeInfo) CodeAttributeInfo(org.ballerinalang.util.codegen.attributes.CodeAttributeInfo) DefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.DefaultValueAttributeInfo) VarTypeCountAttributeInfo(org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo) AttributeInfo(org.ballerinalang.util.codegen.attributes.AttributeInfo)

Aggregations

AttributeInfo (org.ballerinalang.util.codegen.attributes.AttributeInfo)3 CodeAttributeInfo (org.ballerinalang.util.codegen.attributes.CodeAttributeInfo)2 DefaultValueAttributeInfo (org.ballerinalang.util.codegen.attributes.DefaultValueAttributeInfo)2 ErrorTableAttributeInfo (org.ballerinalang.util.codegen.attributes.ErrorTableAttributeInfo)2 LineNumberTableAttributeInfo (org.ballerinalang.util.codegen.attributes.LineNumberTableAttributeInfo)2 LocalVariableAttributeInfo (org.ballerinalang.util.codegen.attributes.LocalVariableAttributeInfo)2 ParamDefaultValueAttributeInfo (org.ballerinalang.util.codegen.attributes.ParamDefaultValueAttributeInfo)2 VarTypeCountAttributeInfo (org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo)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 QueryParameter (io.swagger.models.parameters.QueryParameter)1 HashMap (java.util.HashMap)1 AnnAttachmentInfo (org.ballerinalang.util.codegen.AnnAttachmentInfo)1 AnnAttributeValue (org.ballerinalang.util.codegen.AnnAttributeValue)1 ParamAnnAttachmentInfo (org.ballerinalang.util.codegen.ParamAnnAttachmentInfo)1 ParamAnnotationAttributeInfo (org.ballerinalang.util.codegen.attributes.ParamAnnotationAttributeInfo)1 StructureRefCPEntry (org.ballerinalang.util.codegen.cpentries.StructureRefCPEntry)1