Search in sources :

Example 21 with BStructType

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

the class AbstractExecute method createStruct.

BStruct createStruct(Context context, String structName) {
    PackageInfo httpPackageInfo = context.getProgramFile().getPackageInfo(PROTOCOL_STRUCT_PACKAGE_GRPC);
    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)

Example 22 with BStructType

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

the class MessageUtils method generateRequestStruct.

public static BValue generateRequestStruct(Message request, String fieldName, BType structType, Context context) {
    BValue bValue = null;
    int stringIndex = 0;
    int intIndex = 0;
    int floatIndex = 0;
    int boolIndex = 0;
    int refIndex = 0;
    if (structType instanceof BStructType) {
        BStruct requestStruct = createStruct(context, fieldName);
        for (BStructType.StructField structField : ((BStructType) structType).getStructFields()) {
            String structFieldName = structField.getFieldName();
            if (structField.getFieldType() instanceof BRefType) {
                BType bType = structField.getFieldType();
                if (MessageRegistry.getInstance().getMessageDescriptorMap().containsKey(bType.getName())) {
                    Message message = (Message) request.getFields().get(structFieldName);
                    requestStruct.setRefField(refIndex++, (BRefType) generateRequestStruct(message, structFieldName, structField.getFieldType(), context));
                }
            } else {
                if (request.getFields().containsKey(structFieldName)) {
                    String fieldType = structField.getFieldType().getName();
                    switch(fieldType) {
                        case STRING:
                            {
                                requestStruct.setStringField(stringIndex++, (String) request.getFields().get(structFieldName));
                                break;
                            }
                        case INT:
                            {
                                requestStruct.setIntField(intIndex++, (Long) request.getFields().get(structFieldName));
                                break;
                            }
                        case FLOAT:
                            {
                                Float value = (Float) request.getFields().get(structFieldName);
                                if (value != null) {
                                    requestStruct.setFloatField(floatIndex++, Double.parseDouble(value.toString()));
                                }
                                break;
                            }
                        case DOUBLE:
                            {
                                Double value = (Double) request.getFields().get(structFieldName);
                                if (value != null) {
                                    requestStruct.setFloatField(floatIndex++, Double.parseDouble(value.toString()));
                                }
                                break;
                            }
                        case BOOLEAN:
                            {
                                requestStruct.setBooleanField(boolIndex++, (Integer) request.getFields().get(structFieldName));
                                break;
                            }
                        default:
                            {
                                throw new UnsupportedFieldTypeException("Error while generating request struct. Field" + " type is not supported : " + fieldType);
                            }
                    }
                }
            }
        }
        bValue = requestStruct;
    } else {
        Map<String, Object> fields = request.getFields();
        if (fields.size() == 1 && fields.containsKey("value")) {
            fieldName = "value";
        }
        if (request.getFields().containsKey(fieldName)) {
            String fieldType = structType.getName();
            switch(fieldType) {
                case STRING:
                    {
                        bValue = new BString((String) request.getFields().get(fieldName));
                        break;
                    }
                case INT:
                    {
                        bValue = new BInteger((Long) request.getFields().get(fieldName));
                        break;
                    }
                case FLOAT:
                    {
                        Float value = (Float) request.getFields().get(fieldName);
                        if (value != null) {
                            bValue = new BFloat(Double.parseDouble(value.toString()));
                        }
                        break;
                    }
                case DOUBLE:
                    {
                        Double value = (Double) request.getFields().get(fieldName);
                        if (value != null) {
                            bValue = new BFloat(Double.parseDouble(value.toString()));
                        }
                        break;
                    }
                case BOOLEAN:
                    {
                        bValue = new BBoolean((Boolean) request.getFields().get(fieldName));
                        break;
                    }
                default:
                    {
                        throw new UnsupportedFieldTypeException("Error while generating request struct. Field " + "type is not supported : " + fieldType);
                    }
            }
        }
    }
    return bValue;
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BBoolean(org.ballerinalang.model.values.BBoolean) UnsupportedFieldTypeException(org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException) BString(org.ballerinalang.model.values.BString) BStructType(org.ballerinalang.model.types.BStructType) BInteger(org.ballerinalang.model.values.BInteger) BFloat(org.ballerinalang.model.values.BFloat) BType(org.ballerinalang.model.types.BType) BFloat(org.ballerinalang.model.values.BFloat)

Example 23 with BStructType

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

the class JSONUtils method jsonNodeToBArray.

/**
 * Convert a JSON node to an array.
 *
 * @param arrayNode JSON to convert
 * @param targetArrayType Type of the target array
 * @return If the provided JSON is of array type, this method will return a {@link BArrayType} containing the values
 *         of the JSON array. Otherwise the method will throw a {@link BallerinaException}.
 */
@SuppressWarnings("rawtypes")
private static BNewArray jsonNodeToBArray(JsonNode arrayNode, BArrayType targetArrayType) {
    if (!arrayNode.isArray()) {
        throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, getComplexObjectTypeName(Type.ARRAY), getTypeName(arrayNode));
    }
    BType elementType = targetArrayType.getElementType();
    BRefValueArray refValueArray;
    switch(elementType.getTag()) {
        case TypeTags.INT_TAG:
            return jsonNodeToBIntArray(arrayNode);
        case TypeTags.FLOAT_TAG:
            return jsonNodeToBFloatArray(arrayNode);
        case TypeTags.STRING_TAG:
            return jsonNodeToBStringArray(arrayNode);
        case TypeTags.BOOLEAN_TAG:
            return jsonNodeToBBooleanArray(arrayNode);
        case TypeTags.ANY_TAG:
            refValueArray = new BRefValueArray(elementType);
            for (int i = 0; i < arrayNode.size(); i++) {
                JsonNode element = arrayNode.get(i);
                refValueArray.add(i, (BRefType) getBValue(element));
            }
            return refValueArray;
        default:
            refValueArray = new BRefValueArray(elementType);
            for (int i = 0; i < arrayNode.size(); i++) {
                JsonNode element = arrayNode.get(i);
                if (elementType.getTag() == TypeTags.MAP_TAG) {
                    refValueArray.add(i, jsonNodeToBMap(element, (BMapType) elementType));
                } else if (elementType instanceof BStructType) {
                    refValueArray.add(i, convertJSONNodeToStruct(element, (BStructType) elementType));
                } else if (elementType instanceof BArrayType) {
                    refValueArray.add(i, jsonNodeToBArray(element, (BArrayType) elementType));
                } else {
                    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, elementType, getTypeName(element));
                }
            }
            return refValueArray;
    }
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BMapType(org.ballerinalang.model.types.BMapType) BArrayType(org.ballerinalang.model.types.BArrayType) BType(org.ballerinalang.model.types.BType) BRefValueArray(org.ballerinalang.model.values.BRefValueArray)

Example 24 with BStructType

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

the class JSONUtils method convertStructToJSON.

/**
 * Convert {@link BStruct} to {@link BJSON}.
 *
 * @param struct {@link BStruct} to be converted to {@link BJSON}
 * @return JSON representation of the provided array
 */
@SuppressWarnings("unchecked")
public static BJSON convertStructToJSON(BStruct struct) {
    BJSON bjson = new BJSON(new JsonNode(Type.OBJECT));
    JsonNode jsonNode = bjson.value();
    BStructType structType = (BStructType) struct.getType();
    int longRegIndex = -1;
    int doubleRegIndex = -1;
    int stringRegIndex = -1;
    int booleanRegIndex = -1;
    int refRegIndex = -1;
    for (BStructType.StructField structField : structType.getStructFields()) {
        String key = structField.getFieldName();
        BType fieldType = structField.getFieldType();
        try {
            switch(fieldType.getTag()) {
                case TypeTags.INT_TAG:
                    jsonNode.set(key, struct.getIntField(++longRegIndex));
                    break;
                case TypeTags.FLOAT_TAG:
                    jsonNode.set(key, struct.getFloatField(++doubleRegIndex));
                    break;
                case TypeTags.STRING_TAG:
                    jsonNode.set(key, struct.getStringField(++stringRegIndex));
                    break;
                case TypeTags.BOOLEAN_TAG:
                    jsonNode.set(key, struct.getBooleanField(++booleanRegIndex) == 1);
                    break;
                case TypeTags.BLOB_TAG:
                    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, BTypes.typeJSON, BTypes.typeBlob);
                default:
                    BValue value = struct.getRefField(++refRegIndex);
                    if (value == null) {
                        jsonNode.set(key, new BJSON(NULL).value());
                    } else if (value.getType().getTag() == TypeTags.MAP_TAG) {
                        jsonNode.set(key, convertMapToJSON((BMap<String, BValue>) value).value());
                    } else if (value instanceof BJSON) {
                        jsonNode.set(key, ((BJSON) value).value());
                    } else if (value instanceof BNewArray) {
                        jsonNode.set(key, convertArrayToJSON((BNewArray) value).value());
                    } else if (value instanceof BStruct) {
                        jsonNode.set(key, convertStructToJSON((BStruct) value).value());
                    } else {
                        throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, BTypes.typeJSON, value.getType());
                    }
            }
        } catch (Exception e) {
            handleError(e, key);
        }
    }
    return bjson;
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) BNewArray(org.ballerinalang.model.values.BNewArray) BMap(org.ballerinalang.model.values.BMap) BType(org.ballerinalang.model.types.BType) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 25 with BStructType

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

the class ProgramFileReader method readFunctionInfo.

private void readFunctionInfo(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
    int funcNameCPIndex = dataInStream.readInt();
    UTF8CPEntry funcNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(funcNameCPIndex);
    String funcName = funcNameUTF8Entry.getValue();
    FunctionInfo functionInfo = new FunctionInfo(packageInfo.getPkgNameCPIndex(), packageInfo.getPkgPath(), funcNameCPIndex, funcName);
    functionInfo.setPackageInfo(packageInfo);
    int funcSigCPIndex = dataInStream.readInt();
    UTF8CPEntry funcSigUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(funcSigCPIndex);
    setCallableUnitSignature(functionInfo, funcSigUTF8Entry.getValue(), packageInfo);
    int flags = dataInStream.readInt();
    boolean nativeFunc = Flags.isFlagOn(flags, Flags.NATIVE);
    functionInfo.setNative(nativeFunc);
    String uniqueFuncName;
    boolean attached = Flags.isFlagOn(flags, Flags.ATTACHED);
    if (attached) {
        int attachedToTypeCPIndex = dataInStream.readInt();
        functionInfo.attachedToTypeCPIndex = attachedToTypeCPIndex;
        TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) packageInfo.getCPEntry(attachedToTypeCPIndex);
        functionInfo.attachedToType = typeRefCPEntry.getType();
        uniqueFuncName = AttachedFunctionInfo.getUniqueFuncName(typeRefCPEntry.getType().getName(), funcName);
        packageInfo.addFunctionInfo(uniqueFuncName, functionInfo);
        // Update the attachedFunctionInfo
        if (typeRefCPEntry.getType().getTag() == TypeTags.STRUCT_TAG) {
            BStructType structType = (BStructType) typeRefCPEntry.getType();
            AttachedFunctionInfo attachedFuncInfo = structType.structInfo.funcInfoEntries.get(funcName);
            attachedFuncInfo.functionInfo = functionInfo;
        }
    } else {
        uniqueFuncName = funcName;
        packageInfo.addFunctionInfo(uniqueFuncName, functionInfo);
    }
    int workerDataChannelsLength = dataInStream.readShort();
    for (int i = 0; i < workerDataChannelsLength; i++) {
        readWorkerDataChannelEntries(dataInStream, packageInfo, functionInfo);
    }
    // Read worker info entries
    readWorkerInfoEntries(dataInStream, packageInfo, functionInfo);
    if (nativeFunc) {
        NativeCallableUnit nativeFunction = NativeUnitLoader.getInstance().loadNativeFunction(functionInfo.getPkgPath(), uniqueFuncName);
        if (nativeFunction == null) {
            throw new BLangRuntimeException("native function not available " + functionInfo.getPkgPath() + ":" + uniqueFuncName);
        }
        functionInfo.setNativeCallableUnit(nativeFunction);
    }
    // Read attributes
    readAttributeInfoEntries(dataInStream, packageInfo, functionInfo);
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit)

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