Search in sources :

Example 6 with BNewArray

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

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

the class AbstractSQLAction method createProcessedQueryString.

/**
 * If there are any arrays of parameter for types other than sql array, the given query is expanded by adding "?" s
 * to match with the array size.
 */
private String createProcessedQueryString(String query, BRefValueArray parameters) {
    String currentQuery = query;
    if (parameters != null) {
        int start = 0;
        Object[] vals;
        int count;
        int paramCount = (int) parameters.size();
        for (int i = 0; i < paramCount; i++) {
            BStruct paramValue = (BStruct) parameters.get(i);
            if (paramValue != null) {
                String sqlType = getSQLType(paramValue);
                BValue value = paramValue.getRefField(1);
                if (value != null && value.getType().getTag() == TypeTags.ARRAY_TAG && !Constants.SQLDataTypes.ARRAY.equalsIgnoreCase(sqlType)) {
                    count = (int) ((BNewArray) value).size();
                } else {
                    count = 1;
                }
                vals = this.expandQuery(start, count, currentQuery);
                start = (Integer) vals[0];
                currentQuery = (String) vals[1];
            }
        }
    }
    return currentQuery;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BNewArray(org.ballerinalang.model.values.BNewArray) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString)

Example 8 with BNewArray

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

the class VariableDTO method getStringValue.

private String getStringValue(BValue bValue) {
    String bValueString;
    if (bValue instanceof BValueType || bValue instanceof BXML || bValue instanceof BJSON) {
        bValueString = bValue.stringValue();
    } else if (bValue instanceof BNewArray) {
        BNewArray bArray = (BNewArray) bValue;
        bValueString = "Array[" + bArray.size() + "] ";
        bValueString = bValueString + bArray.stringValue();
    } else if (bValue instanceof BMap) {
        BMap bmap = (BMap) bValue;
        bValueString = "Map[" + bmap.size() + "] ";
        bValueString = bValueString + bmap.stringValue();
    } else if (bValue instanceof BStruct) {
        BStruct bStruct = (BStruct) bValue;
        bValueString = "struct " + bStruct.getType().getName() + " ";
        bValueString = bValueString + bStruct.stringValue();
    } else {
        bValueString = "<Complex_Value>";
    }
    return bValueString;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValueType(org.ballerinalang.model.values.BValueType) BNewArray(org.ballerinalang.model.values.BNewArray) BMap(org.ballerinalang.model.values.BMap) BXML(org.ballerinalang.model.values.BXML) BJSON(org.ballerinalang.model.values.BJSON)

Example 9 with BNewArray

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

the class Equals method isEqual.

/**
 * Deep equal or strict equal implementation for any value type.
 *
 * @param lhsValue The value on the left side.
 * @param rhsValue The value on the right side.
 * @return True if values are equal, else false.
 */
@SuppressWarnings("rawtypes")
private boolean isEqual(BValue lhsValue, BValue rhsValue) {
    if (null == lhsValue && null == rhsValue) {
        return true;
    }
    if (null == lhsValue || null == rhsValue) {
        return false;
    }
    // Required for any == any.
    if (lhsValue.getType().getTag() != rhsValue.getType().getTag()) {
        return false;
    }
    if (null != lhsValue.getType().getPackagePath() && null != rhsValue.getType().getPackagePath() && !lhsValue.getType().getPackagePath().equals(rhsValue.getType().getPackagePath())) {
        return false;
    }
    if (null != lhsValue.getType().getName() && null != rhsValue.getType().getName() && !lhsValue.getType().getName().equals(rhsValue.getType().getName())) {
        return false;
    }
    // Handling JSON.
    if (lhsValue instanceof BJSON && rhsValue instanceof BJSON) {
        JsonNode lhsJSON = ((BJSON) lhsValue).value();
        JsonNode rhsJSON = ((BJSON) rhsValue).value();
        return isEqual(lhsJSON, rhsJSON);
    }
    switch(lhsValue.getType().getTag()) {
        case TypeTags.STRING_TAG:
        case TypeTags.INT_TAG:
        case TypeTags.FLOAT_TAG:
        case TypeTags.BOOLEAN_TAG:
        case TypeTags.TYPEDESC_TAG:
            BRefType lhsRef = (BRefType) lhsValue;
            BRefType rhsRef = (BRefType) rhsValue;
            if (null == lhsRef.value() && null == rhsRef.value()) {
                return true;
            }
            if (null == lhsRef.value() || null == rhsRef.value()) {
                return false;
            }
            return lhsRef.value().equals(rhsRef.value());
        case TypeTags.STRUCT_TAG:
            BStructType lhsStructType = (BStructType) lhsValue.getType();
            BStructType rhsStructType = (BStructType) rhsValue.getType();
            if (!Arrays.equals(lhsStructType.getFieldTypeCount(), rhsStructType.getFieldTypeCount())) {
                return false;
            }
            return isEqual((BStruct) lhsValue, (BStruct) rhsValue, lhsStructType);
        case TypeTags.MAP_TAG:
            return isEqual((BMap) lhsValue, (BMap) rhsValue);
        case TypeTags.ARRAY_TAG:
        case TypeTags.ANY_TAG:
            // TODO: This block should ideally be in the ARRAY_TAG case only. Not ANY_TAG. #4505.
            if (lhsValue instanceof BNewArray) {
                BNewArray lhsArray = (BNewArray) lhsValue;
                BNewArray rhsArray = (BNewArray) rhsValue;
                if (lhsArray.size() != rhsArray.size()) {
                    return false;
                }
                for (int i = 0; i < lhsArray.size(); i++) {
                    if (!isEqual(lhsArray.getBValue(i), rhsArray.getBValue(i))) {
                        return false;
                    }
                }
            }
            break;
        case TypeTags.XML_TAG:
            // TODO: https://www.pixelstech.net/article/1453272563-Canonicalize-XML-in-Java
            break;
        default:
            return false;
    }
    return true;
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BStructType(org.ballerinalang.model.types.BStructType) BNewArray(org.ballerinalang.model.values.BNewArray) JsonNode(org.ballerinalang.model.util.JsonNode) BJSON(org.ballerinalang.model.values.BJSON)

Aggregations

BNewArray (org.ballerinalang.model.values.BNewArray)9 BJSON (org.ballerinalang.model.values.BJSON)7 BValue (org.ballerinalang.model.values.BValue)6 BStruct (org.ballerinalang.model.values.BStruct)5 BMap (org.ballerinalang.model.values.BMap)4 BString (org.ballerinalang.model.values.BString)4 ArrayList (java.util.ArrayList)2 BStructType (org.ballerinalang.model.types.BStructType)2 BIterator (org.ballerinalang.model.values.BIterator)2 BRefType (org.ballerinalang.model.values.BRefType)2 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)2 List (java.util.List)1 Value (org.ballerinalang.connector.api.Value)1 BType (org.ballerinalang.model.types.BType)1 JsonNode (org.ballerinalang.model.util.JsonNode)1 BFloat (org.ballerinalang.model.values.BFloat)1 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)1 BTypeDescValue (org.ballerinalang.model.values.BTypeDescValue)1 BValueType (org.ballerinalang.model.values.BValueType)1 BXML (org.ballerinalang.model.values.BXML)1