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