Search in sources :

Example 6 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class LoadToTable method getStruct.

private static BStruct getStruct(String[] fields, final BStructType structType) {
    BStructType.StructField[] internalStructFields = structType.getStructFields();
    int fieldLength = internalStructFields.length;
    BStruct struct = null;
    if (fields.length > 0) {
        if (internalStructFields.length != fields.length) {
            String msg = "Record row fields count and the give struct's fields count are mismatch";
            throw new BallerinaIOException(msg);
        }
        struct = new BStruct(structType);
        int longRegIndex = -1;
        int doubleRegIndex = -1;
        int stringRegIndex = -1;
        int booleanRegIndex = -1;
        for (int i = 0; i < fieldLength; i++) {
            String value = fields[i];
            final BStructType.StructField internalStructField = internalStructFields[i];
            final int type = internalStructField.getFieldType().getTag();
            switch(type) {
                case TypeTags.INT_TAG:
                    struct.setIntField(++longRegIndex, Long.parseLong(value));
                    break;
                case TypeTags.FLOAT_TAG:
                    struct.setFloatField(++doubleRegIndex, Double.parseDouble(value));
                    break;
                case TypeTags.STRING_TAG:
                    struct.setStringField(++stringRegIndex, value);
                    break;
                case TypeTags.BOOLEAN_TAG:
                    struct.setBooleanField(++booleanRegIndex, (Boolean.parseBoolean(value)) ? 1 : 0);
                    break;
                default:
                    throw new BallerinaIOException("Type casting support only for int, float, boolean and string. " + "Invalid value for the struct field: " + value);
            }
        }
    }
    return struct;
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct)

Example 7 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class LoadToTable method getbTable.

private static BTable getbTable(Context context, List records) throws BallerinaIOException {
    BTypeDescValue type = (BTypeDescValue) context.getRefArgument(0);
    BTable table = new BTable(new BTableType(type.value()));
    BStructType structType = (BStructType) type.value();
    boolean skipHeaderLine = context.getBooleanArgument(0);
    if (skipHeaderLine && !records.isEmpty()) {
        records.remove(0);
    }
    for (Object obj : records) {
        String[] fields = (String[]) obj;
        final BStruct struct = getStruct(fields, structType);
        if (struct != null) {
            table.addData(struct);
        }
    }
    return table;
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BTypeDescValue(org.ballerinalang.model.values.BTypeDescValue) BTable(org.ballerinalang.model.values.BTable) BStruct(org.ballerinalang.model.values.BStruct) BTableType(org.ballerinalang.model.types.BTableType)

Example 8 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class CPU method checkJSONEquivalency.

private static boolean checkJSONEquivalency(JsonNode json, BJSONType sourceType, BJSONType targetType) {
    BStructType sourceConstrainedType = (BStructType) sourceType.getConstrainedType();
    BStructType targetConstrainedType = (BStructType) targetType.getConstrainedType();
    // Casting to an unconstrained JSON
    if (targetConstrainedType == null) {
        // ideally we should't reach here. This is checked from typeChecker
        return true;
    }
    // Casting from constrained JSON to constrained JSON
    if (sourceConstrainedType != null) {
        if (sourceConstrainedType.equals(targetConstrainedType)) {
            return true;
        }
        return checkStructEquivalency(sourceConstrainedType, targetConstrainedType);
    }
    // Casting from unconstrained JSON to constrained JSON
    BStructType.StructField[] tFields = targetConstrainedType.getStructFields();
    for (int i = 0; i < tFields.length; i++) {
        String fieldName = tFields[i].getFieldName();
        if (!json.has(fieldName)) {
            return false;
        }
        if (!checkJSONCast(json.get(fieldName), sourceType, tFields[i].getFieldType())) {
            return false;
        }
    }
    return true;
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BString(org.ballerinalang.model.values.BString)

Example 9 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class CPU method convertJSONToStruct.

private static void convertJSONToStruct(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
    int i = operands[0];
    int cpIndex = operands[1];
    int j = operands[2];
    TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
    BJSON bjson = (BJSON) sf.refRegs[i];
    if (bjson == null) {
        handleNullRefError(ctx);
        return;
    }
    try {
        sf.refRegs[j] = JSONUtils.convertJSONToStruct(bjson, (BStructType) typeRefCPEntry.getType());
    } catch (Exception e) {
        String errorMsg = "cannot convert '" + TypeConstants.JSON_TNAME + "' to type '" + typeRefCPEntry.getType() + "': " + e.getMessage();
        handleTypeConversionError(ctx, sf, j, errorMsg);
    }
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 10 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class CPU method convertMapToStruct.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void convertMapToStruct(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
    int i = operands[0];
    int cpIndex = operands[1];
    int j = operands[2];
    TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
    BMap<String, BValue> bMap = (BMap<String, BValue>) sf.refRegs[i];
    if (bMap == null) {
        handleNullRefError(ctx);
        return;
    }
    int longRegIndex = -1;
    int doubleRegIndex = -1;
    int stringRegIndex = -1;
    int booleanRegIndex = -1;
    int blobRegIndex = -1;
    int refRegIndex = -1;
    BStructType structType = (BStructType) typeRefCPEntry.getType();
    BStruct bStruct = new BStruct(structType);
    StructInfo structInfo = ctx.callableUnitInfo.getPackageInfo().getStructInfo(structType.getName());
    Set<String> keys = bMap.keySet();
    for (StructFieldInfo fieldInfo : structInfo.getFieldInfoEntries()) {
        String key = fieldInfo.getName();
        BType fieldType = fieldInfo.getFieldType();
        BValue mapVal = null;
        try {
            boolean containsField = keys.contains(key);
            DefaultValueAttributeInfo defaultValAttrInfo = null;
            if (containsField) {
                mapVal = bMap.get(key);
                if (mapVal == null && BTypes.isValueType(fieldType)) {
                    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_FIELD_TYPE_FOR_CASTING, key, fieldType, null);
                }
                if (mapVal != null && !checkCast(mapVal, fieldType)) {
                    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_FIELD_TYPE_FOR_CASTING, key, fieldType, mapVal.getType());
                }
            } else {
                defaultValAttrInfo = (DefaultValueAttributeInfo) getAttributeInfo(fieldInfo, AttributeInfo.Kind.DEFAULT_VALUE_ATTRIBUTE);
            }
            switch(fieldType.getTag()) {
                case TypeTags.INT_TAG:
                    longRegIndex++;
                    if (containsField) {
                        bStruct.setIntField(longRegIndex, ((BInteger) mapVal).intValue());
                    } else if (defaultValAttrInfo != null) {
                        bStruct.setIntField(longRegIndex, defaultValAttrInfo.getDefaultValue().getIntValue());
                    }
                    break;
                case TypeTags.FLOAT_TAG:
                    doubleRegIndex++;
                    if (containsField) {
                        bStruct.setFloatField(doubleRegIndex, ((BFloat) mapVal).floatValue());
                    } else if (defaultValAttrInfo != null) {
                        bStruct.setFloatField(doubleRegIndex, defaultValAttrInfo.getDefaultValue().getFloatValue());
                    }
                    break;
                case TypeTags.STRING_TAG:
                    stringRegIndex++;
                    if (containsField) {
                        bStruct.setStringField(stringRegIndex, ((BString) mapVal).stringValue());
                    } else if (defaultValAttrInfo != null) {
                        bStruct.setStringField(stringRegIndex, defaultValAttrInfo.getDefaultValue().getStringValue());
                    }
                    break;
                case TypeTags.BOOLEAN_TAG:
                    booleanRegIndex++;
                    if (containsField) {
                        bStruct.setBooleanField(booleanRegIndex, ((BBoolean) mapVal).booleanValue() ? 1 : 0);
                    } else if (defaultValAttrInfo != null) {
                        bStruct.setBooleanField(booleanRegIndex, defaultValAttrInfo.getDefaultValue().getBooleanValue() ? 1 : 0);
                    }
                    break;
                case TypeTags.BLOB_TAG:
                    blobRegIndex++;
                    if (containsField && mapVal != null) {
                        bStruct.setBlobField(blobRegIndex, ((BBlob) mapVal).blobValue());
                    }
                    break;
                default:
                    bStruct.setRefField(++refRegIndex, (BRefType) mapVal);
            }
        } catch (BallerinaException e) {
            sf.refRegs[j] = null;
            String errorMsg = "cannot convert '" + bMap.getType() + "' to type '" + structType + ": " + e.getMessage();
            handleTypeConversionError(ctx, sf, j, errorMsg);
            return;
        }
    }
    sf.refRegs[j] = bStruct;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BMap(org.ballerinalang.model.values.BMap) StructInfo(org.ballerinalang.util.codegen.StructInfo) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BValue(org.ballerinalang.model.values.BValue) BBoolean(org.ballerinalang.model.values.BBoolean) StructFieldInfo(org.ballerinalang.util.codegen.StructFieldInfo) BString(org.ballerinalang.model.values.BString) BStructType(org.ballerinalang.model.types.BStructType) DefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.DefaultValueAttributeInfo) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

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