use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class BJSON method stringValue.
@Override
public String stringValue() {
JsonNode node = this.value();
if (node.isValueNode()) {
return this.value().asText();
} else if (!node.isObject()) {
return node.toString();
}
BStructType constrainedType = (BStructType) ((BJSONType) this.type).getConstrainedType();
if (constrainedType == null) {
return node.toString();
}
// If constrained JSON, print the only the fields in the constrained type.
StringJoiner sj = new StringJoiner(",", "{", "}");
for (StructField field : constrainedType.getStructFields()) {
String key = field.fieldName;
String stringValue = this.value().get(key).toString();
sj.add("\"" + key + "\":" + stringValue);
}
return sj.toString();
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class ProgramFile method addAttributeInfo.
@Override
public void addAttributeInfo(AttributeInfo.Kind attributeKind, AttributeInfo attributeInfo) {
attributeInfoMap.put(attributeKind, attributeInfo);
if (attributeKind == AttributeInfo.Kind.VARIABLE_TYPE_COUNT_ATTRIBUTE) {
// TODO Move this out of the program file to a program context.. Runtime representation of a program.
// TODO ProgramFile is the static program data.
VarTypeCountAttributeInfo varTypeCountAttribInfo = (VarTypeCountAttributeInfo) attributeInfo;
int[] globalVarCount = varTypeCountAttribInfo.getVarTypeCount();
// TODO Introduce an abstraction for memory blocks
// Initialize global memory block
BStructType dummyType = new BStructType(null, "", "", 0);
dummyType.setFieldTypeCount(globalVarCount);
this.globalMemoryBlock = new BStruct(dummyType);
}
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class ProgramFileReader method resolveUserDefinedTypes.
private void resolveUserDefinedTypes(PackageInfo packageInfo) {
// TODO Improve this. We should be able to this in a single pass.
StructInfo[] structInfoEntries = packageInfo.getStructInfoEntries();
for (StructInfo structInfo : structInfoEntries) {
StructFieldInfo[] fieldInfoEntries = structInfo.getFieldInfoEntries();
BStructType structType = structInfo.getType();
BStructType.StructField[] structFields = new BStructType.StructField[fieldInfoEntries.length];
for (int i = 0; i < fieldInfoEntries.length; i++) {
// Get the BType from the type descriptor
StructFieldInfo fieldInfo = fieldInfoEntries[i];
String typeDesc = fieldInfo.getTypeDescriptor();
BType fieldType = getBTypeFromDescriptor(typeDesc);
fieldInfo.setFieldType(fieldType);
// Create the StructField in the BStructType. This is required for the type equivalence algorithm
BStructType.StructField structField = new BStructType.StructField(fieldType, fieldInfo.getName(), fieldInfo.flags);
structFields[i] = structField;
}
VarTypeCountAttributeInfo attributeInfo = (VarTypeCountAttributeInfo) structInfo.getAttributeInfo(AttributeInfo.Kind.VARIABLE_TYPE_COUNT_ATTRIBUTE);
structType.setFieldTypeCount(attributeInfo.getVarTypeCount());
structType.setStructFields(structFields);
// Resolve attached function signature
int attachedFuncCount = structInfo.funcInfoEntries.size();
BStructType.AttachedFunction[] attachedFunctions = new BStructType.AttachedFunction[attachedFuncCount];
int count = 0;
for (AttachedFunctionInfo attachedFuncInfo : structInfo.funcInfoEntries.values()) {
BFunctionType funcType = getFunctionType(attachedFuncInfo.typeSignature, packageInfo);
BStructType.AttachedFunction attachedFunction = new BStructType.AttachedFunction(attachedFuncInfo.name, funcType, attachedFuncInfo.flags);
attachedFunctions[count++] = attachedFunction;
if (structInfo.initializer == attachedFuncInfo) {
structType.initializer = attachedFunction;
} else if (structInfo.defaultsValuesInitFunc == attachedFuncInfo) {
structType.defaultsValuesInitFunc = attachedFunction;
}
}
structType.setAttachedFunctions(attachedFunctions);
}
for (ConstantPoolEntry cpEntry : unresolvedCPEntries) {
switch(cpEntry.getEntryType()) {
case CP_ENTRY_TYPE_REF:
TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) cpEntry;
String typeSig = typeRefCPEntry.getTypeSig();
BType bType = getBTypeFromDescriptor(typeSig);
typeRefCPEntry.setType(bType);
break;
default:
break;
}
}
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class ProgramFileReader method readStructInfoEntries.
private void readStructInfoEntries(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
int structCount = dataInStream.readShort();
for (int i = 0; i < structCount; i++) {
// Create struct info entry
int structNameCPIndex = dataInStream.readInt();
int flags = dataInStream.readInt();
UTF8CPEntry structNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(structNameCPIndex);
String structName = structNameUTF8Entry.getValue();
StructInfo structInfo = new StructInfo(packageInfo.getPkgNameCPIndex(), packageInfo.getPkgPath(), structNameCPIndex, structName, flags);
packageInfo.addStructInfo(structName, structInfo);
// Set struct type
BStructType bStructType = new BStructType(structInfo, structName, packageInfo.getPkgPath(), flags);
structInfo.setType(bStructType);
// Read struct field info entries
int structFiledCount = dataInStream.readShort();
for (int j = 0; j < structFiledCount; j++) {
// Read field name
int fieldNameCPIndex = dataInStream.readInt();
UTF8CPEntry fieldNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(fieldNameCPIndex);
// Read field type signature
int fieldTypeSigCPIndex = dataInStream.readInt();
UTF8CPEntry fieldTypeSigUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(fieldTypeSigCPIndex);
int fieldFlags = dataInStream.readInt();
StructFieldInfo fieldInfo = new StructFieldInfo(fieldNameCPIndex, fieldNameUTF8Entry.getValue(), fieldTypeSigCPIndex, fieldTypeSigUTF8Entry.getValue(), fieldFlags);
structInfo.addFieldInfo(fieldInfo);
readAttributeInfoEntries(dataInStream, packageInfo, fieldInfo);
}
String defaultInit = structName + INIT_FUNCTION_SUFFIX;
// Read attached function info entries
int attachedFuncCount = dataInStream.readShort();
for (int j = 0; j < attachedFuncCount; j++) {
// Read function name
int nameCPIndex = dataInStream.readInt();
UTF8CPEntry nameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(nameCPIndex);
String attachedFuncName = nameUTF8Entry.getValue();
// Read function type signature
int typeSigCPIndex = dataInStream.readInt();
UTF8CPEntry typeSigUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(typeSigCPIndex);
int funcFlags = dataInStream.readInt();
AttachedFunctionInfo functionInfo = new AttachedFunctionInfo(nameCPIndex, attachedFuncName, typeSigCPIndex, typeSigUTF8Entry.getValue(), funcFlags);
structInfo.funcInfoEntries.put(functionInfo.name, functionInfo);
// Setting the initializer function info, if any.
if (structName.equals(attachedFuncName)) {
structInfo.initializer = functionInfo;
}
// Setting the default initializer function info
if (defaultInit.equals(attachedFuncName)) {
structInfo.defaultsValuesInitFunc = functionInfo;
}
}
// Read attributes of the struct info
readAttributeInfoEntries(dataInStream, packageInfo, structInfo);
}
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class AbstractHTTPAction method createStruct.
/**
* Creates a ballerina struct.
*
* @param context ballerina context
* @param structName name of the struct
* @param protocolPackage package name
* @return the ballerina struct
*/
protected BStruct createStruct(Context context, String structName, String protocolPackage) {
PackageInfo httpPackageInfo = context.getProgramFile().getPackageInfo(protocolPackage);
StructInfo structInfo = httpPackageInfo.getStructInfo(structName);
BStructType structType = structInfo.getType();
return new BStruct(structType);
}
Aggregations