Search in sources :

Example 21 with StructInfo

use of org.ballerinalang.util.codegen.StructInfo 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 22 with StructInfo

use of org.ballerinalang.util.codegen.StructInfo in project ballerina by ballerina-lang.

the class HttpUtil method getHttpConnectorError.

public static BStruct getHttpConnectorError(Context context, Throwable throwable) {
    PackageInfo httpPackageInfo = context.getProgramFile().getPackageInfo(HttpConstants.PROTOCOL_PACKAGE_HTTP);
    StructInfo errorStructInfo = httpPackageInfo.getStructInfo(HttpConstants.HTTP_CONNECTOR_ERROR);
    BStruct httpConnectorError = new BStruct(errorStructInfo.getType());
    if (throwable.getMessage() == null) {
        httpConnectorError.setStringField(0, IO_EXCEPTION_OCCURED);
    } else {
        httpConnectorError.setStringField(0, throwable.getMessage());
    }
    return httpConnectorError;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 23 with StructInfo

use of org.ballerinalang.util.codegen.StructInfo 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)

Example 24 with StructInfo

use of org.ballerinalang.util.codegen.StructInfo 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 25 with StructInfo

use of org.ballerinalang.util.codegen.StructInfo in project ballerina by ballerina-lang.

the class InitEndpoint method getConnectorError.

private static BStruct getConnectorError(Context context, Throwable throwable) {
    PackageInfo grpcPackageInfo = context.getProgramFile().getPackageInfo(PROTOCOL_PACKAGE_GRPC);
    StructInfo errorStructInfo = grpcPackageInfo.getStructInfo(CONNECTOR_ERROR);
    BStruct grpcConnectorError = new BStruct(errorStructInfo.getType());
    if (throwable.getMessage() == null) {
        grpcConnectorError.setStringField(0, "Service Initialization error.");
    } else {
        grpcConnectorError.setStringField(0, throwable.getMessage());
    }
    return grpcConnectorError;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Aggregations

StructInfo (org.ballerinalang.util.codegen.StructInfo)40 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)35 BStruct (org.ballerinalang.model.values.BStruct)21 BStructType (org.ballerinalang.model.types.BStructType)6 BString (org.ballerinalang.model.values.BString)4 BValue (org.ballerinalang.model.values.BValue)4 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)4 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)3 BType (org.ballerinalang.model.types.BType)2 BInteger (org.ballerinalang.model.values.BInteger)2 BMap (org.ballerinalang.model.values.BMap)2 SocketIOChannel (org.ballerinalang.nativeimpl.io.channels.SocketIOChannel)2 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)2 StructFieldInfo (org.ballerinalang.util.codegen.StructFieldInfo)2 Test (org.testng.annotations.Test)2 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 URL (java.net.URL)1 ByteChannel (java.nio.channels.ByteChannel)1 SocketChannel (java.nio.channels.SocketChannel)1