Search in sources :

Example 11 with BType

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

the class BLangVMStructs method createBStruct.

/**
 * Create BStruct for given StructInfo and BValues.
 *
 * @param structInfo {@link StructInfo} of the BStruct
 * @param values     field values of the BStruct.
 * @return BStruct instance.
 */
public static BStruct createBStruct(StructInfo structInfo, Object... values) {
    BStructType structType = structInfo.getType();
    BStruct bStruct = new BStruct(structType);
    int[] indexes = new int[] { -1, -1, -1, -1, -1, -1 };
    BStructType.StructField[] structFields = structType.getStructFields();
    for (int i = 0; i < structFields.length; i++) {
        if (values.length < i + 1) {
            break;
        }
        BType paramType = structFields[i].getFieldType();
        setValue(bStruct, indexes, paramType.getTag(), values[i]);
    }
    return bStruct;
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) BType(org.ballerinalang.model.types.BType)

Example 12 with BType

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

the class BLangProgramRunner method getMainFunction.

public static FunctionInfo getMainFunction(PackageInfo mainPkgInfo) {
    String errorMsg = "main function not found in  '" + mainPkgInfo.getProgramFile().getProgramFilePath() + "'";
    FunctionInfo mainFuncInfo = mainPkgInfo.getFunctionInfo("main");
    if (mainFuncInfo == null) {
        throw new BallerinaException(errorMsg);
    }
    BType[] paramTypes = mainFuncInfo.getParamTypes();
    BType[] retParamTypes = mainFuncInfo.getRetParamTypes();
    BArrayType argsType = new BArrayType(BTypes.typeString);
    if (paramTypes.length != 1 || !paramTypes[0].equals(argsType) || retParamTypes.length != 0) {
        throw new BallerinaException(errorMsg);
    }
    return mainFuncInfo;
}
Also used : BArrayType(org.ballerinalang.model.types.BArrayType) BType(org.ballerinalang.model.types.BType) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 13 with BType

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

the class JSONUtils method convertJSON.

private static Object convertJSON(JsonNode jsonValue, BType targetType) {
    switch(targetType.getTag()) {
        case TypeTags.INT_TAG:
            return jsonNodeToInt(jsonValue);
        case TypeTags.FLOAT_TAG:
            return jsonNodeToFloat(jsonValue);
        case TypeTags.STRING_TAG:
            if (jsonValue.isString()) {
                return jsonValue.stringValue();
            } else {
                return jsonValue.toString();
            }
        case TypeTags.BOOLEAN_TAG:
            return jsonNodeToBool(jsonValue);
        case TypeTags.UNION_TAG:
            BUnionType type = (BUnionType) targetType;
            if (jsonValue.isNull() && type.isNullable()) {
                return null;
            }
            List<BType> matchingTypes = type.getMemberTypes().stream().filter(memberType -> memberType != BTypes.typeNull).collect(Collectors.toList());
            if (matchingTypes.size() == 1) {
                return convertJSON(jsonValue, matchingTypes.get(0));
            }
            break;
        case TypeTags.STRUCT_TAG:
            return convertJSONNodeToStruct(jsonValue, (BStructType) targetType);
        case TypeTags.ANY_TAG:
        case TypeTags.JSON_TAG:
            if (jsonValue.isNull()) {
                return null;
            }
            return new BJSON(jsonValue);
        case TypeTags.ARRAY_TAG:
            return jsonNodeToBArray(jsonValue, (BArrayType) targetType);
        case TypeTags.MAP_TAG:
            return jsonNodeToBMap(jsonValue, (BMapType) targetType);
        case TypeTags.NULL_TAG:
            if (jsonValue.isNull()) {
                return null;
            }
            break;
        default:
            break;
    }
    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, targetType, getTypeName(jsonValue));
}
Also used : BUnionType(org.ballerinalang.model.types.BUnionType) BUnionType(org.ballerinalang.model.types.BUnionType) BFloat(org.ballerinalang.model.values.BFloat) BType(org.ballerinalang.model.types.BType) BBoolean(org.ballerinalang.model.values.BBoolean) OMText(org.apache.axiom.om.OMText) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BMapType(org.ballerinalang.model.types.BMapType) RuntimeErrors(org.ballerinalang.util.exceptions.RuntimeErrors) ArrayList(java.util.ArrayList) BJSON(org.ballerinalang.model.values.BJSON) BNewArray(org.ballerinalang.model.values.BNewArray) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) OMNamespace(org.apache.axiom.om.OMNamespace) BString(org.ballerinalang.model.values.BString) TableJSONDataSource(org.ballerinalang.model.TableJSONDataSource) BInteger(org.ballerinalang.model.values.BInteger) BXML(org.ballerinalang.model.values.BXML) BTypes(org.ballerinalang.model.types.BTypes) BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) BMap(org.ballerinalang.model.values.BMap) OMElement(org.apache.axiom.om.OMElement) BFloatArray(org.ballerinalang.model.values.BFloatArray) StructFieldInfo(org.ballerinalang.util.codegen.StructFieldInfo) Iterator(java.util.Iterator) Type(org.ballerinalang.model.util.JsonNode.Type) Set(java.util.Set) BStructType(org.ballerinalang.model.types.BStructType) BXMLItem(org.ballerinalang.model.values.BXMLItem) BLangExceptionHelper(org.ballerinalang.util.exceptions.BLangExceptionHelper) TypeTags(org.ballerinalang.model.types.TypeTags) Collectors(java.util.stream.Collectors) BStringArray(org.ballerinalang.model.values.BStringArray) BIntArray(org.ballerinalang.model.values.BIntArray) BArrayType(org.ballerinalang.model.types.BArrayType) List(java.util.List) OMAbstractFactory(org.apache.axiom.om.OMAbstractFactory) BRefType(org.ballerinalang.model.values.BRefType) Entry(java.util.Map.Entry) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) OMFactory(org.apache.axiom.om.OMFactory) BTable(org.ballerinalang.model.values.BTable) BBooleanArray(org.ballerinalang.model.values.BBooleanArray) BValue(org.ballerinalang.model.values.BValue) BType(org.ballerinalang.model.types.BType) BJSON(org.ballerinalang.model.values.BJSON)

Example 14 with BType

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

the class JSONUtils method convertJSONNodeToStruct.

/**
 * Convert a BJSON to a user defined struct.
 *
 * @param jsonNode   JSON to convert
 * @param structType Type (definition) of the target struct
 * @return If the provided JSON is of object-type, this method will return a {@link BStruct} containing the values
 * of the JSON object. Otherwise the method will throw a {@link BallerinaException}.
 */
public static BStruct convertJSONNodeToStruct(JsonNode jsonNode, BStructType structType) {
    if (!jsonNode.isObject()) {
        throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, getComplexObjectTypeName(Type.OBJECT), getTypeName(jsonNode));
    }
    int longRegIndex = -1;
    int doubleRegIndex = -1;
    int stringRegIndex = -1;
    int booleanRegIndex = -1;
    int refRegIndex = -1;
    int blobRegIndex = -1;
    BStruct bStruct = new BStruct(structType);
    StructInfo structInfo = structType.structInfo;
    boolean fieldExists;
    for (StructFieldInfo fieldInfo : structInfo.getFieldInfoEntries()) {
        BType fieldType = fieldInfo.getFieldType();
        String fieldName = fieldInfo.getName();
        try {
            fieldExists = jsonNode.has(fieldName);
            JsonNode jsonValue = jsonNode.get(fieldName);
            switch(fieldType.getTag()) {
                case TypeTags.INT_TAG:
                    longRegIndex++;
                    if (fieldExists) {
                        bStruct.setIntField(longRegIndex, jsonNodeToInt(jsonValue));
                    }
                    break;
                case TypeTags.FLOAT_TAG:
                    doubleRegIndex++;
                    if (fieldExists) {
                        bStruct.setFloatField(doubleRegIndex, jsonNodeToFloat(jsonValue));
                    }
                    break;
                case TypeTags.STRING_TAG:
                    stringRegIndex++;
                    String stringVal;
                    if (!fieldExists) {
                        stringVal = "";
                    } else if (jsonValue.isString()) {
                        stringVal = jsonValue.stringValue();
                    } else {
                        stringVal = jsonValue.toString();
                    }
                    bStruct.setStringField(stringRegIndex, stringVal);
                    break;
                case TypeTags.BOOLEAN_TAG:
                    booleanRegIndex++;
                    if (fieldExists) {
                        bStruct.setBooleanField(booleanRegIndex, jsonNodeToBool(jsonValue) ? 1 : 0);
                    }
                    break;
                case TypeTags.UNION_TAG:
                case TypeTags.STRUCT_TAG:
                case TypeTags.ANY_TAG:
                case TypeTags.JSON_TAG:
                case TypeTags.ARRAY_TAG:
                case TypeTags.MAP_TAG:
                case TypeTags.NULL_TAG:
                    refRegIndex++;
                    if (fieldExists) {
                        bStruct.setRefField(refRegIndex, (BRefType<?>) convertJSON(jsonValue, fieldType));
                    }
                    break;
                case TypeTags.FUNCTION_POINTER_TAG:
                    if (fieldExists) {
                        throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, fieldName, getTypeName(jsonValue));
                    }
                    // TODO: set the default value
                    bStruct.setRefField(++refRegIndex, null);
                    break;
                case TypeTags.BLOB_TAG:
                    bStruct.setBlobField(++blobRegIndex, new byte[0]);
                    break;
                default:
                    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, fieldName, getTypeName(jsonValue));
            }
        } catch (Exception e) {
            handleError(e, fieldName);
        }
    }
    return bStruct;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) BType(org.ballerinalang.model.types.BType) StructFieldInfo(org.ballerinalang.util.codegen.StructFieldInfo) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 15 with BType

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

the class TableIterator method generateNext.

@Override
public BStruct generateNext() {
    BStruct bStruct = new BStruct(type);
    int longRegIndex = -1;
    int doubleRegIndex = -1;
    int stringRegIndex = -1;
    int booleanRegIndex = -1;
    int refRegIndex = -1;
    int blobRegIndex = -1;
    int index = 0;
    try {
        BStructType.StructField[] structFields = type.getStructFields();
        for (BStructType.StructField sf : structFields) {
            BType type = sf.getFieldType();
            ++index;
            switch(type.getTag()) {
                case TypeTags.INT_TAG:
                    long iValue = rs.getInt(index);
                    bStruct.setIntField(++longRegIndex, iValue);
                    break;
                case TypeTags.STRING_TAG:
                    String sValue = rs.getString(index);
                    bStruct.setStringField(++stringRegIndex, sValue);
                    break;
                case TypeTags.FLOAT_TAG:
                    double dalue = rs.getDouble(index);
                    bStruct.setFloatField(++doubleRegIndex, dalue);
                    break;
                case TypeTags.BOOLEAN_TAG:
                    boolean boolValue = rs.getBoolean(index);
                    bStruct.setBooleanField(++booleanRegIndex, boolValue ? 1 : 0);
                    break;
                case TypeTags.JSON_TAG:
                    String jsonValue = rs.getString(index);
                    bStruct.setRefField(++refRegIndex, new BJSON(jsonValue));
                    break;
                case TypeTags.XML_TAG:
                    String xmlValue = rs.getString(index);
                    bStruct.setRefField(++refRegIndex, new BXMLItem(xmlValue));
                    break;
                case TypeTags.BLOB_TAG:
                    Blob blobValue = rs.getBlob(index);
                    bStruct.setBlobField(++blobRegIndex, blobValue.getBytes(1L, (int) blobValue.length()));
                    break;
                case TypeTags.ARRAY_TAG:
                    Array arrayValue = rs.getArray(index);
                    bStruct.setRefField(++refRegIndex, getDataArray(arrayValue));
                    break;
            }
        }
    } catch (SQLException e) {
        throw new BallerinaException("error in generating next row of data :" + e.getMessage());
    }
    return bStruct;
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BStruct(org.ballerinalang.model.values.BStruct) Blob(java.sql.Blob) SQLException(java.sql.SQLException) BJSON(org.ballerinalang.model.values.BJSON) BStructType(org.ballerinalang.model.types.BStructType) BFloatArray(org.ballerinalang.model.values.BFloatArray) BStringArray(org.ballerinalang.model.values.BStringArray) Array(java.sql.Array) BIntArray(org.ballerinalang.model.values.BIntArray) BNewArray(org.ballerinalang.model.values.BNewArray) BBooleanArray(org.ballerinalang.model.values.BBooleanArray) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Aggregations

BType (org.ballerinalang.model.types.BType)48 BStructType (org.ballerinalang.model.types.BStructType)16 BString (org.ballerinalang.model.values.BString)16 BStruct (org.ballerinalang.model.values.BStruct)15 BValue (org.ballerinalang.model.values.BValue)13 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)12 BBoolean (org.ballerinalang.model.values.BBoolean)11 BArrayType (org.ballerinalang.model.types.BArrayType)7 BFloat (org.ballerinalang.model.values.BFloat)6 BInteger (org.ballerinalang.model.values.BInteger)6 BMapType (org.ballerinalang.model.types.BMapType)4 BRefType (org.ballerinalang.model.values.BRefType)4 SQLException (java.sql.SQLException)3 Struct (java.sql.Struct)3 ArrayList (java.util.ArrayList)3 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)3 BBlob (org.ballerinalang.model.values.BBlob)3 Message (org.ballerinalang.net.grpc.Message)3 StructFieldInfo (org.ballerinalang.util.codegen.StructFieldInfo)3 StructInfo (org.ballerinalang.util.codegen.StructInfo)3