Search in sources :

Example 6 with DefaultValue

use of org.wso2.ballerinalang.programfile.DefaultValue in project ballerina by ballerina-lang.

the class CodeGenerator method getDefaultValue.

private DefaultValue getDefaultValue(BLangLiteral literalExpr) {
    String desc = literalExpr.type.getDesc();
    int typeDescCPIndex = addUTF8CPEntry(currentPkgInfo, desc);
    DefaultValue defaultValue = new DefaultValue(typeDescCPIndex, desc);
    int typeTag = literalExpr.type.tag;
    switch(typeTag) {
        case TypeTags.INT:
            defaultValue.intValue = (Long) literalExpr.value;
            defaultValue.valueCPIndex = currentPkgInfo.addCPEntry(new IntegerCPEntry(defaultValue.intValue));
            break;
        case TypeTags.FLOAT:
            defaultValue.floatValue = (Double) literalExpr.value;
            defaultValue.valueCPIndex = currentPkgInfo.addCPEntry(new FloatCPEntry(defaultValue.floatValue));
            break;
        case TypeTags.STRING:
            defaultValue.stringValue = (String) literalExpr.value;
            defaultValue.valueCPIndex = currentPkgInfo.addCPEntry(new UTF8CPEntry(defaultValue.stringValue));
            break;
        case TypeTags.BOOLEAN:
            defaultValue.booleanValue = (Boolean) literalExpr.value;
            break;
        default:
            defaultValue = null;
    }
    return defaultValue;
}
Also used : DefaultValue(org.wso2.ballerinalang.programfile.DefaultValue) UTF8CPEntry(org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) IntegerCPEntry(org.wso2.ballerinalang.programfile.cpentries.IntegerCPEntry) FloatCPEntry(org.wso2.ballerinalang.programfile.cpentries.FloatCPEntry) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 7 with DefaultValue

use of org.wso2.ballerinalang.programfile.DefaultValue in project ballerina by ballerina-lang.

the class CodeGenerator method generateNamedArgs.

private int generateNamedArgs(BLangInvocation iExpr, Operand[] operands, int currentIndex) {
    if (iExpr.namedArgs.isEmpty()) {
        return currentIndex;
    }
    PackageInfo pkgInfo = programFile.packageInfoMap.get(iExpr.symbol.pkgID.bvmAlias());
    CallableUnitInfo callableUnitInfo;
    if (iExpr.symbol.kind == SymbolKind.FUNCTION) {
        callableUnitInfo = pkgInfo.functionInfoMap.get(iExpr.symbol.name.value);
    } else if (iExpr.symbol.kind == SymbolKind.ACTION) {
        ConnectorInfo connectorInfo = pkgInfo.connectorInfoMap.get(iExpr.symbol.owner.name.value);
        callableUnitInfo = connectorInfo.actionInfoMap.get(iExpr.symbol.name.value);
    } else {
        throw new IllegalStateException("Unsupported callable unit");
    }
    ParamDefaultValueAttributeInfo defaultValAttrInfo = (ParamDefaultValueAttributeInfo) callableUnitInfo.getAttributeInfo(AttributeInfo.Kind.PARAMETER_DEFAULTS_ATTRIBUTE);
    for (int i = 0; i < iExpr.namedArgs.size(); i++) {
        BLangExpression argExpr = iExpr.namedArgs.get(i);
        // at this point. If so, get the default value for that parameter from the function info.
        if (argExpr == null) {
            DefaultValue defaultVal = defaultValAttrInfo.getDefaultValueInfo()[i];
            argExpr = getDefaultValExpr(defaultVal);
        }
        operands[currentIndex++] = genNode(argExpr, this.env).regIndex;
    }
    return currentIndex;
}
Also used : DefaultValue(org.wso2.ballerinalang.programfile.DefaultValue) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) ConnectorInfo(org.wso2.ballerinalang.programfile.ConnectorInfo) ImportPackageInfo(org.wso2.ballerinalang.programfile.ImportPackageInfo) PackageInfo(org.wso2.ballerinalang.programfile.PackageInfo) CallableUnitInfo(org.wso2.ballerinalang.programfile.CallableUnitInfo) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 8 with DefaultValue

use of org.wso2.ballerinalang.programfile.DefaultValue in project ballerina by ballerina-lang.

the class Generator method createDocForNode.

/**
 * Create documentation for structs.
 * @param structNode ballerina struct node.
 * @return documentation for structs.
 */
public static StructDoc createDocForNode(BLangStruct structNode) {
    String structName = structNode.getName().value;
    // Check if its an anonymous struct
    if (structName.contains(ANONYMOUS_STRUCT)) {
        structName = "Anonymous Struct";
    }
    List<Field> fields = new ArrayList<>();
    // Iterate through the struct fields
    if (structNode.getFields().size() > 0) {
        for (BLangVariable param : structNode.getFields()) {
            String dataType = type(param);
            String desc = fieldAnnotation(structNode, param);
            String defaultValue = "";
            if (null != param.getInitialExpression()) {
                defaultValue = param.getInitialExpression().toString();
            }
            Field variable = new Field(param.getName().value, dataType, desc, defaultValue);
            fields.add(variable);
        }
    }
    return new StructDoc(structName, description(structNode), new ArrayList<>(), fields);
}
Also used : Field(org.ballerinalang.docgen.model.Field) ArrayList(java.util.ArrayList) StructDoc(org.ballerinalang.docgen.model.StructDoc) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 9 with DefaultValue

use of org.wso2.ballerinalang.programfile.DefaultValue in project ballerina by ballerina-lang.

the class HtmlDocTest method testStructDefaultValues.

@Test(description = "Tests whether default values are collected.")
public void testStructDefaultValues() {
    BLangPackage bLangPackage = createPackage("package x.y; " + "public struct Person {" + "  string id;" + "  string address = \"20,Palm Grove\";" + "}");
    Page page = generatePage(bLangPackage);
    Assert.assertEquals(page.constructs.size(), 1);
    Assert.assertTrue(page.constructs.get(0) instanceof StructDoc, "Documentable of type StructDoc expected.");
    StructDoc personStructDoc = (StructDoc) page.constructs.get(0);
    Assert.assertEquals(personStructDoc.fields.size(), 2, "2 fields are expected.");
    Assert.assertEquals(personStructDoc.fields.get(0).name, "id", "Field \"id\" expected.");
    Assert.assertEquals(personStructDoc.fields.get(1).name, "address", "Field \"address\" expected.");
    Assert.assertEquals(personStructDoc.fields.get(1).defaultValue, "20,Palm Grove", "Unexpected address value found.");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Page(org.ballerinalang.docgen.model.Page) StructDoc(org.ballerinalang.docgen.model.StructDoc) Test(org.testng.annotations.Test)

Aggregations

BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)4 DefaultValue (org.wso2.ballerinalang.programfile.DefaultValue)4 ParamDefaultValueAttributeInfo (org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo)4 StructDoc (org.ballerinalang.docgen.model.StructDoc)2 DefaultValueAttributeInfo (org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo)2 UTF8CPEntry (org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry)2 ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1 Struct (org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Struct)1 StructField (org.ballerinalang.composer.service.ballerina.parser.service.model.lang.StructField)1 Field (org.ballerinalang.docgen.model.Field)1 Page (org.ballerinalang.docgen.model.Page)1 Test (org.testng.annotations.Test)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)1 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)1 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)1 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)1 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)1