Search in sources :

Example 16 with BStructType

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();
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) StructField(org.ballerinalang.model.types.BStructType.StructField) JsonNode(org.ballerinalang.model.util.JsonNode) StringJoiner(java.util.StringJoiner)

Example 17 with BStructType

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);
    }
}
Also used : VarTypeCountAttributeInfo(org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo) BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct)

Example 18 with BStructType

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;
        }
    }
}
Also used : VarTypeCountAttributeInfo(org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BFunctionType(org.ballerinalang.model.types.BFunctionType) ConstantPoolEntry(org.ballerinalang.util.codegen.cpentries.ConstantPoolEntry) BStructType(org.ballerinalang.model.types.BStructType) BType(org.ballerinalang.model.types.BType)

Example 19 with BStructType

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);
    }
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry)

Example 20 with BStructType

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);
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Aggregations

BStructType (org.ballerinalang.model.types.BStructType)40 BStruct (org.ballerinalang.model.values.BStruct)24 BType (org.ballerinalang.model.types.BType)13 BString (org.ballerinalang.model.values.BString)11 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)11 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)7 BValue (org.ballerinalang.model.values.BValue)7 StructInfo (org.ballerinalang.util.codegen.StructInfo)7 BRefType (org.ballerinalang.model.values.BRefType)6 BBoolean (org.ballerinalang.model.values.BBoolean)5 BJSON (org.ballerinalang.model.values.BJSON)5 BTypeDescValue (org.ballerinalang.model.values.BTypeDescValue)5 Struct (java.sql.Struct)4 BFloat (org.ballerinalang.model.values.BFloat)4 BInteger (org.ballerinalang.model.values.BInteger)4 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)4 BigDecimal (java.math.BigDecimal)3 SQLException (java.sql.SQLException)3 BMapType (org.ballerinalang.model.types.BMapType)3 BMap (org.ballerinalang.model.values.BMap)3