Search in sources :

Example 26 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class ConnectorSPIModelHelper method processAnnotations.

private static void processAnnotations(String pkgPath, ProgramFile programFile, AnnotatableNode annotatableNode) {
    final BMap bMap = getAnnotationVariable(pkgPath, programFile);
    final BValue map = bMap.get(annotatableNode.getAnnotationEntryKey());
    if (map == null || map.getType().getTag() != BTypes.typeMap.getTag()) {
        return;
    }
    BMap<String, BValue> annotationMap = (BMap<String, BValue>) map;
    for (String key : annotationMap.keySet()) {
        final BStruct annotationData = (BStruct) annotationMap.get(key);
        StructImpl struct = null;
        if (annotationData != null) {
            struct = new StructImpl(annotationData);
        }
        final String annotaionQName = key.split("\\$")[0];
        final String[] qNameParts = annotaionQName.split(":");
        final AnnotationImpl annotation = new AnnotationImpl(qNameParts[1], qNameParts[0], struct);
        annotatableNode.addAnnotation(annotaionQName, annotation);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BMap(org.ballerinalang.model.values.BMap) BValue(org.ballerinalang.model.values.BValue)

Example 27 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class BLangConnectorSPIUtil method getPackageEndpoint.

public static BStruct getPackageEndpoint(ProgramFile programFile, String pkgName, String endpointName) {
    final PackageInfo packageInfo = programFile.getPackageInfo(pkgName);
    if (packageInfo == null) {
        throw new BallerinaConnectorException("Incorrect package name");
    }
    final PackageVarInfo packageVarInfo = packageInfo.getPackageVarInfo(endpointName);
    if (packageVarInfo == null) {
        throw new BallerinaConnectorException("Can't locate " + endpointName + " endpoint variable");
    }
    return (BStruct) programFile.getGlobalMemoryBlock().getRefField(packageVarInfo.getGlobalMemIndex());
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) PackageVarInfo(org.ballerinalang.util.codegen.PackageVarInfo)

Example 28 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class BCompileUtil method createAndGetStruct.

public static BStruct createAndGetStruct(ProgramFile programFile, String packagePath, String structName) {
    PackageInfo structPackageInfo = programFile.getPackageInfo(packagePath);
    StructInfo structInfo = structPackageInfo.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 29 with BStruct

use of org.ballerinalang.model.values.BStruct 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 30 with BStruct

use of org.ballerinalang.model.values.BStruct 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

BStruct (org.ballerinalang.model.values.BStruct)460 BValue (org.ballerinalang.model.values.BValue)187 Test (org.testng.annotations.Test)161 BString (org.ballerinalang.model.values.BString)131 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)53 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)39 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)37 BInteger (org.ballerinalang.model.values.BInteger)33 BMap (org.ballerinalang.model.values.BMap)29 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)26 BStructType (org.ballerinalang.model.types.BStructType)25 IOException (java.io.IOException)23 BBoolean (org.ballerinalang.model.values.BBoolean)23 BJSON (org.ballerinalang.model.values.BJSON)22 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)21 StructInfo (org.ballerinalang.util.codegen.StructInfo)21 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)20 File (java.io.File)17 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)17 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)16